📄 gridsimcore.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: GridSimCore.java,v 1.13 2006/03/09 05:56:31 anthony Exp $ */package gridsim;import eduni.simjava.*;import gridsim.net.*;import gridsim.util.*;import java.util.Collection;/** * Since GridSim version 3.0, this is the overall class of GridSim package, * that must be extended by other GridSim * entities. It inherits event management and threaded entity features from * the {@link eduni.simjava.Sim_entity} class. This class adds networking and * event delivery features, which allow synchronous or asynchronous * communication for service access or delivery. * <p> * All classes that extend this class can implement a method called * {@link eduni.simjava.Sim_entity#body()}, which is automatically invoked * when a simulation runs, since it is expected to be * responsible for simulating entity behavior. In addition, * {@link eduni.simjava.Sim_entity#body()} method is * the place to receive incoming events. * <p> * The entities that extend this class can be instantiated <i>with</i> or * <i>without</i> networked I/O ports. A networked GridSim entity gains * communication capability via the objects of GridSim's I/O entity classes, * {@link gridsim.net.Input} and {@link gridsim.net.Output} classes. Each I/O * entity will have a unique name assuming each GridSim entity that the user * creates has a unique name. For example, a resource entity with the name * <b>Resource2</b>, * will have an input entity whose name is prefixed with <b>Input_</b>, * making the input entity's full name <b>Input_Resource2</b>, which is * expected to be unique. <b>Input_Resource2</b> entity handles all incoming * events. A resource has an output entity whose name is prefixed with * <b>Output_</b>, e.g. <b>Output_Resource2</b>. <b>Output_Resource2</b> * entity handles all outgoing events. * <p> * The I/O entities are concurrent entities, but they * are visible within the GridSim entity and are able to communicate with * other GridSim entities by sending messages. * <p> * There are two ways to send a message or object to an entity, i.e. with or * without I/O port. Below shows the differences: * <ul> * <li>using {@link #send(int, double, int, Object)}, * {@link #send(int, double, int)}, {@link #send(String,double,int)}, * or {@link #send(String, double, int, Object)} method. * <p> * These methods will send an event directly to the destination entity * <b>without</b> using I/O port. Regardless how big the size of the * Object data is, <br> * <i>event arrival time = current simulation time + delay time</i> * <p> * For example: <tt>send(destID, delay, tag, obj)</tt>. <br> * On the destinated entity, by creating a Sim_event object: * <ul> * <li> Sim_event.get_data() is obj of type Object. NOTE: * Object can be a generic type, such as IO_data, Gridlet, * String, etc. * <li> Sim_event.get_dest() is destID of type int. * <li> Sim_event.get_src() is sender ID of type int. NOTE: * sender ID is the entity ID that uses/calls this method. * <li> Sim_event.get_tag() is tag of type int. * <li> Sim_event.get_scheduled_by() is sender ID of type int. * </ul> * <br> * * <li>using {@link #send(Sim_port, double, int, Object)} or * {@link #send(Sim_port, double, int)} method. * <p> * These methods will send an event to the destination entity * <b>with</b> I/O port. The size of the Object data sent is taken * into a consideration for determining the event arrival time. * <p> * For example: <tt>send(ouputPort, delay, tag, obj)</tt> where * obj <b>MUST</b> be of type <tt>IO_data</tt>. <br> * On the destinated entity, by creating a Sim_event object: * <ul> * <li> Sim_event.get_data() is (IO_data) obj.getData() of type * Object. NOTE: * Object can be a generic type, such as Gridlet, String, * Integer, etc. * <li> Sim_event.get_dest() is (IO_data) obj.getDestID() of * type int. * <li> Sim_event.get_src() is an entity ID of type int. * <b>NOTE:</b> entity ID is not from the sender ID, but from * <tt>Input_xxx</tt>, where xxx = destinated entity name. * For example, to send to <tt>Resource2</tt>, the entity ID * is an ID of <tt>Input_Resource2</tt>. * <li> Sim_event.get_tag() is tag of type int. * <li> Sim_event.get_scheduled_by() is an entity ID of type int. * Same explanation of Sim_event.get_src(). * </ul> * <br> * <b>NOTE:</b> When sending using I/O port, object must be of type * <tt>IO_data</tt>, otherwise <tt>ClassCastException</tt> will be * reported on {@link gridsim.net.Output#body()} method. * * </ul> * <p> * Since GridSim 3.1, a network extension has been incorporated into * this simulation infrastructure. To make use of this, you need to create a * resource entity <b>only</b> using {@link #GridSimCore(String, Link)} * constructors. * Then you need to attach this entity into the overall network topology, i.e. * connecting this entity to a router, etc. See the examples provided * in the package for more details. * <p> * Another important feature of a network extension is the ability to * ping to a particular entity by using {@link #ping(int, int)} or * {@link #pingBlockingCall(int, int)} overloading methods. * <p> * However, there are few conditions to be met: * <ul> * <li> only work for entities which are connected through a network topology * using the network extension entities, such as {@link gridsim.net.Link} * and {@link gridsim.net.Router}. * <li> pinging to itself is permitted but the <tt>Round Trip Time</tt> is * always 0 (zero). * <li> <b>most importantly</b>, you need to implement the below source code * for the incoming ping request to work. This is required <b>only</b> for * an entity that extends from {@link gridsim.GridSim} or * {@link gridsim.GridSimCore}. <br> * NOTE: {@link gridsim.GridResource} class is not effected as it * already has the below code. <br><br><br> * * <code> * ... // other code <br> * <br> * public void body() { <br> * Sim_event ev = new Sim_event(); <br> * while (Sim_system.running()) { <br> * super.sim_get_next(ev); <br> <br> * // Entity's behaviour for handling ping request <br> * if (ev.get_tag() == GridSimTags.INFOPKT_SUBMIT) { <br> * processPingRequest(ev); <br> * continue; // skip the rest <br> * } <br> <br> * ... // other code for handling other incoming requests <br> * } <br> * } <br> * <br> * private void processPingRequest(Sim_event ev) { <br> * InfoPacket pkt = (InfoPacket) ev.get_data(); <br> * pkt.setTag(GridSimTags.INFOPKT_RETURN); <br> * pkt.setDestID( pkt.getSrcID() ); <br> * <br> * // sends back to the sender <br> * super.send(super.output, GridSimTags.SCHEDULE_NOW, * GridSimTags.INFOPKT_RETURN, * new IO_data(pkt,pkt.getSize(),pkt.getSrcID()) ); <br> * } <br> * * </code> * </ul> * * @author Manzur Murshed and Rajkumar Buyya * @author Anthony Sulistio (re-written this class) * @since GridSim Toolkit 3.0 * @see eduni.simjava.Sim_entity * @see gridsim.net.Output * @see gridsim.net.Input * @invariant $none */public class GridSimCore extends Sim_entity{ private boolean networkedFlag_; // true, if networked entity, other false // false means NOT invoked private boolean terminateIOEntitiesFlag_ = false; // If this GridSim uses Network extensions, then the link that is joined to // this entity. private Link link_; /** Reading data received via input port */ protected Sim_port input; /** Sending data via output port to external entities */ protected Sim_port output; // Output port but only for a network extension. private Output out_ = null; /** * Allocates a new GridSim object * <b>without</b> NETWORK communication channels: "input" and * "output" Sim_port. In summary, this object has <tt>NO</tt> * network communication or bandwidth speed. * @param name the name to be associated with this entity (as * required by Sim_entity class from simjava package) * @throws Exception This happens when creating this entity before * initializing GridSim package or this entity name is * <tt>null</tt> or empty * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @see eduni.simjava.Sim_entity * @pre name != null * @post $none */ protected GridSimCore(String name) throws Exception { super(name); networkedFlag_ = false; input = null; output = null; link_ = null; } /** * Allocates a new GridSim object * <b>with</b> NETWORK communication channels: "input" and * "output" Sim_port. In addition, this method will create <tt>Input</tt> * and <tt>Output</tt> object. * <p> * However, this is the old approach using one-to-all connection where you * can not specify a network topology and there is no wired link from * this entity to others. Use {@link #GridSimCore(String, Link)} instead. * * @param name the name to be associated with this entity (as * required by Sim_entity class from simjava package) * @param baudRate network communication or bandwidth speed * @throws Exception This happens when creating this entity before * initializing GridSim package or this entity name is * <tt>null</tt> or empty * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @see eduni.simjava.Sim_entity * @see gridsim.net.Input * @see gridsim.net.Output * @pre name != null * @pre baudRate > 0.0 * @post $none */ protected GridSimCore(String name, double baudRate) throws Exception { super(name); networkedFlag_ = true; link_ = null; input = new Sim_port("input"); output = new Sim_port("output"); super.add_port(input); super.add_port(output); // Every GridSim entity with network has its own input/output channels. // Connect this entity "input" port to its input buffer "in_port" new Input("Input_" + name, baudRate); Sim_system.link_ports(name, "input", "Input_" + name, "input_buffer"); new Output("Output_" + name, baudRate); Sim_system.link_ports(name, "output", "Output_"+name, "output_buffer"); } /** * Allocates a new GridSim object * <b>with</b> NETWORK communication channels: "input" and * "output" Sim_port. In addition, this method will create <tt>Input</tt> * and <tt>Output</tt> object. * <p> * You need to manually create the network topology, i.e. connecting * this entity to a router/other entity. * * @param name the name to be associated with this entity (as * required by Sim_entity class from simjava package) * @param link the link that this GridSim entity will use to communicate with other GridSim or Network entities. * @throws Exception This happens when creating this entity before * initializing GridSim package or this entity name is * <tt>null</tt> or empty * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @see eduni.simjava.Sim_entity * @see gridsim.net.Input * @see gridsim.net.Output * @pre name != null * @pre link != null * @post $none */ protected GridSimCore(String name, Link link) throws Exception { super(name); networkedFlag_ = true; link_ = link; input = new Sim_port("input"); output = new Sim_port("output"); super.add_port(input); super.add_port(output); // Every GridSim entity with network has its own input/output channels. // Connect this entity "input" port to its input buffer "in_port" Input in = new Input("Input_" + name, link_.getBaudRate()); in.addLink(link_); Sim_system.link_ports(name, "input", "Input_" + name, "input_buffer"); out_ = new Output("Output_" + name, link_.getBaudRate()); out_.addLink(link_); Sim_system.link_ports(name, "output", "Output_"+name, "output_buffer"); } /** * Returns the Link that connects this entity to other entities if Network * Extensions are being used. * @return a Link object * @pre $none * @post $none */ public Link getLink() { return link_; } /** * Sets the background traffic generator for this entity. * <p> * When simulation starts, the Output 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 || out_ == null) { return false; } return out_.setBackgroundTraffic(gen); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -