📄 output.java
字号:
/* * Title: GridSim Toolkit * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation * of Parallel and Distributed Systems such as Clusters and Grids * Licence: GPL - http://www.gnu.org/copyleft/gpl.html * * $Id: Output.java,v 1.7 2005/09/02 04:12:04 anthony Exp $ */package gridsim.net;import gridsim.*;import gridsim.net.*;import gridsim.util.*;import eduni.simjava.*;import java.util.*;/** * GridSim Output defines a port through which a simulation entity sends * data to the simulated network. * <p> * It maintains an event queue to serialize * the data-out-flow and delivers to the destination entity. * It works along with Input entity to simulate network * communication delay. Simultaneous outputs can be modeled by using multiple * instances of this class * * @author Manzur Murshed and Rajkumar Buyya * @since GridSim Toolkit 1.0 * @invariant $none */public class Output extends Sim_entity{ private Sim_port outPort_; // output port private Link link_; // a link to this output entity private double baudRate_; // baud rate of this entity private final int SIZE = 8; // 1 byte in bits private int pktID_; // packet ID counter private Vector packetList_; // store a list of packets private Random random_; // selects to which junk packets go to private TrafficGenerator gen_; // background traffic generator private ArrayList list_; // list of resources + user entities private boolean hasStarted_; // a flag for background traffic has started private static final int BITS = 8; // 1 byte = 8 bits /** * Allocates a new Output object * @param name the name of this object * @param baudRate the communication speed * @throws NullPointerException This happens when creating this entity * before initializing GridSim package or this entity name * is <tt>null</tt> or empty * @pre name != null * @pre baudRate >= 0.0 * @post $none */ public Output(String name, double baudRate) throws NullPointerException { super(name); this.baudRate_ = baudRate; link_ = null; packetList_ = null; pktID_ = 0; outPort_ = new Sim_port("output_buffer"); super.add_port(outPort_); // for sending background traffic gen_ = null; list_ = null; random_ = null; hasStarted_ = false; } /** * Sets the background traffic generator for this entity. * <p> * When simulation starts, this entity will automatically sends junk * packets to resource entities. * @param gen a background traffic generator * @return <tt>true</tt> if successful, <tt>false</tt> otherwise * @pre gen != null * @post $none */ public boolean setBackgroundTraffic(TrafficGenerator gen) { if (gen == null) { return false; } gen_ = gen; if (list_ == null) { list_ = new ArrayList(); } return true; } /** * Sets the background traffic generator for this entity. * <p> * When simulation starts, this entity will automatically sends junk * packets to resource entities and other entities. <br> * NOTE: Sending background traffic to itself is not supported. * * @param gen a background traffic generator * @param userName a collection of user entity name (in String object). * @return <tt>true</tt> if successful, <tt>false</tt> otherwise * @pre gen != null * @pre userName != null * @post $none */ public boolean setBackgroundTraffic(TrafficGenerator gen, Collection userName) { if (gen == null || userName == null) { return false; } boolean flag = true; try { gen_ = gen; if (list_ == null) { list_ = new ArrayList(); } // iterates through each list to check whether it is a valid // entity name or not Iterator it = userName.iterator(); int id = -1; while( it.hasNext() ) { String name = (String) it.next(); // check whether it is sending to itself id = GridSim.getEntityId("Output_" + name); if (id == super.get_id()) { System.out.println(super.get_name() + ".setBackgroundTraffic(): Warning - can not send " + "junk packets to itself."); continue; } // get the ID of other entity id = GridSim.getEntityId(name); if (id > 0) { Integer obj = new Integer(id); list_.add(obj); } // ignore for invalid entity else { System.out.println(super.get_name() + ".setBackgroundTraffic(): Warning - invalid entity " + "name for \"" + name + "\"."); } } } catch(Exception e) { flag = false; } return flag; } /** * Sets this entity's link. This should be used only if the network * extensions are being used. * * @param link the link to which this Output entity should send data * @pre link != null * @post $none */ public void addLink(Link link) { this.link_ = link; packetList_ = new Vector(); } /** * Gets the baud rate * @return the baud rate * @deprecated As of GridSim 2.1, replaced by {@link #getBaudRate()} * @pre $none * @post $result >= 0.0 */ public double GetBaudRate() { return this.getBaudRate(); } /** * Gets the baud rate * @return the baud rate * @pre $none * @post $result >= 0.0 */ public double getBaudRate() { return baudRate_; } /** * Gets the I/O real number based on a given value * @param value the specified value * @return real number * @deprecated As of GridSim 2.1, replaced by {@link #realIO(double)} * @pre $none * @post $result >= 0.0 */ public double real_io(double value) { return this.realIO(value); } /** * Gets the I/O real number based on a given value * @param value the specified value * @return real number * @pre $none * @post $result >= 0.0 */ public double realIO(double value) { return GridSimRandom.realIO(value); } /** * A method that gets one process event at one time until the end * of a simulation, then delivers an event to the entity (its parent) * @pre $none * @post $none */ public void body() { // find out ids for entities that are not part of simulation network // topology, such as GIS, GridSimShutdown and GridStatistics int gisID = GridSim.getGridInfoServiceEntityId(); int statID = GridSim.getGridStatisticsEntityId(); int shutdownID = GridSim.getGridSimShutdownEntityId(); // start generating some junk packets or background traffic startBackgroundTraffic(); // Process incoming events while ( Sim_system.running() ) { Sim_event ev = new Sim_event(); super.sim_get_next(ev); // get the next event in the queue // if the simulation finishes then exit the loop if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { break; } // handle different types of incoming events switch ( ev.get_tag() ) { case GridSimTags.SEND_PACKET: sendPacket(); break; // submit ping() request case GridSimTags.INFOPKT_SUBMIT: sendInfoPacket(ev); break; // replying ping() request from another entity case GridSimTags.INFOPKT_RETURN: returnInfoPacket(ev); break; // activate background traffic case GridSimTags.JUNK_PKT: generateBackgroundTraffic(); break; default: defaultSend(ev, gisID, statID, shutdownID); break; } } } /** * Generates few junk packets at the given interval * @pre $none * @post $none */ private synchronized void generateBackgroundTraffic() { // get the next inter-arrival time for these junk packets long time = gen_.getNextPacketTime(); // get the sending pattern int pattern = gen_.getPattern(); // for initial start-up, get the list of all resources first if (hasStarted_ == false) { // get list of resource IDs from GIS LinkedList resList = GridSim.getGridResourceList(); // if the list is empty then schedule the next time if (resList == null && list_.size() == 0) { super.sim_schedule(super.get_id(), time, GridSimTags.JUNK_PKT); return; } hasStarted_ = true; list_.addAll(resList); // add resource IDs into the current list // sets the sending pattern if (pattern == TrafficGenerator.SEND_ONE_ONLY && random_ == null) { random_ = new Random(); } } // get the required info for generating this background traffic long size = gen_.getNextPacketSize(); // packet size long freq = gen_.getNextPacketFreq(); // packet freq int type = gen_.getServiceType(); // packet type int tag = GridSimTags.JUNK_PKT; // packet tag // we need to packetsize the data, all packets are sent with size MTU. // only the last packet contains the data, the receiver should // throw away all other packets int MTU = link_.getMTU(); int numPackets = (int) Math.ceil( size / (MTU * 1.0) ); /********* // DEBUG info System.out.println(); System.out.println(super.get_name() + ": START GENERATE BG traffic... at time "+ GridSim.clock()); System.out.println(super.get_name() + ": NEXT background traffic will start at " + time); System.out.println(super.get_name() + " num PACKETS = " + numPackets + ", freq = " + freq); *********/ int i = 0; int destId = -1; // send to one of the entity using uniform distribution if (pattern == TrafficGenerator.SEND_ONE_ONLY) { int index = random_.nextInt( list_.size() ); destId = ((Integer) list_.get(index)).intValue(); /********* // DEBUG info System.out.println(super.get_name() + ": Destination id = " +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -