/* * Title: CloudSim Toolkit * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds * Licence: GPL - http://www.gnu.org/copyleft/gpl.html * * Copyright (c) 2009-2010, The University of Melbourne, Australia */package org.cloudbus.cloudsim.core;/** * 這個類代表一個模擬事件,可以在各個實體之間傳遞,實現cloneable,和comparable * 介面分別需要實現clone()和comparetor()方法,前者用於複製整個對象產生一個 * 副本,後者用於比較,可用於list中對象按關鍵字排序。 * This class represents a simulation event which is passed between * the entities in the simulation. * * @authorCostas Simatos * * @seeSimulation * @seeSimEntity */public class SimEvent implements Cloneable, Comparable<SimEvent> {/* * 8個欄位: * etype:內部事件類型,定義了4中事件類型ENULL,SEND,HOLD_DONE,CREATE * time:事件應該發生的時間 * endWaitingTime:時間從future Queue中移出的時間,即事件發送時間 * entSrc:事件發送實體的ID * entDst:事件目的地實體的ID * tag:使用者定義事件的類型 * data:事件攜帶的資料 * serial:未知 */private final int etype; // internal event typeprivate final double time; // time at which event should occurprivate double endWaitingTime; // time that the event was removed from the// queue for serviceprivate int entSrc; // id of entity who scheduled eventprivate int entDst; // id of entity event will be sent toprivate final int tag; // the user defined type of the eventprivate final Object data; // any data the event is carryingprivate long serial = -1;// Internal event typespublic static final int ENULL = 0;public static final int SEND = 1;public static final int HOLD_DONE = 2;public static final int CREATE = 3;/** * Create a blank event. */public SimEvent() {etype = ENULL;this.time = -1L;endWaitingTime = -1.0;entSrc = -1;entDst = -1;this.tag = -1;data = null;}// ------------------- PACKAGE LEVEL METHODS --------------------------//實體不知道事件發送時間endwaitingtime。SimEvent(int evtype, double time, int src, int dest, int tag, Object edata) {etype = evtype;this.time = time;entSrc = src;entDst = dest;this.tag = tag;data = edata;}SimEvent(int evtype, double time, int src) {etype = evtype;this.time = time;entSrc = src;entDst = -1;this.tag = -1;data = null;}protected void setSerial(long serial) {this.serial = serial;}// Used to set the time at which this event finished waiting in the event// queue. This is used for statistical purposes.用於統計目的protected void setEndWaitingTime(double end_waiting_time) {this.endWaitingTime = end_waiting_time;}@Overridepublic String toString() {return "Event tag = " + tag + " source = "+ CloudSim.getEntity(this.entSrc).getName()+ " destination = "+ CloudSim.getEntity(this.entDst).getName();}// The internal typepublic int getType() {return etype;}// ------------------- PUBLIC METHODS --------------------------/** * @see Comparable#compareTo(Object) * 事件發生時間比較,用於Queue排序,時間早的放到Queue前面 */@Overridepublic int compareTo(SimEvent event) {if (event == null) {return 1;} else if (time < event.time) {return -1;} else if (time > event.time) {return 1;} else if (serial < event.serial) {return -1;} else if (this == event) {return 0;} else {return 1;}}/** * Get the unique id number of the entity which received this event. * @return the id number */public int getDestination() {return entDst;}/** * Get the unique id number of the entity which scheduled this event. * @return the id number */public int getSource() {return entSrc;}/** * Get the simulation time that this event was scheduled. * @return The simulation time */public double eventTime() {return time;}/** * Get the simulation time that this event was removed from the queue for * service. * @return The simulation time */public double endWaitingTime() {return endWaitingTime;}/** * Get the user-defined tag of this event * @return The tag */public int type() {return tag;}/** * Get the unique id number of the entity which scheduled this event. * @return the id number */public int scheduledBy() {return entSrc;}/** * Get the user-defined tag of this event. * @return The tag */public int getTag() {return tag;}/** * Get the data passed in this event. * @return A reference to the data */public Object getData() {return data;}/** * Create an exact copy of this event. * @return The event's copy */@Overridepublic Object clone() {return new SimEvent(etype, time, entSrc, entDst, tag, data);}/** * Set the source entity of this event. * @param s The unique id number of the entity */public void setSource(int s) {entSrc = s;}/** * Set the destination entity of this event. * @param d The unique id number of the entity */public void setDestination(int d) {entDst = d;}}