/*
* 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;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.NetworkTopology;
import org.cloudbus.cloudsim.core.predicates.Predicate;
/**
* simEntity类继承Cloneable接口,必须实现clone()方法,可以使此类按字段复制,产生一
* 个对象副本
* 这个类代表仿真实体,一个实体可以处理事件,也可以发送事件到其他实体。当这个类被继承,
* 有一些方法需要实现
* startEntity()方法被请求,当仿真开始的时候,这个方法负责启动实体。
* processEvent()被请求,当defered队列中还有事件时。
* 当仿真结束前,需要关闭实体.
* 方法总结:
* 1、发送事件,有cloudsim组建成simevent,调用futureQueue方法加入future queue队列中
* 2、队列中发往本实体满足谓词p的事件个数,谓词p用来对事件限制
* 3、队列中等待发往本实体的事件个数
* 4、选择第一个满足src(此目的地为实体本身)和谓词p条件的事件,并把它从队列中移走
* 5、在future队列中寻找第一个满足条件的事件,从中移走即取消此事件
* 6、选择下一个事件
* 7、三个核心方法,分别是实体开始,处理事件,实体销毁
* 8、实体线程,一直查找事件并处理事件
* 9、clone对象
* 10、向目标实体发送事件delay差brite矩阵获得。
* 11、获得两实体之间传送事件延迟,从birte矩阵中获得
* 可认为包括三种方法:发送事件,查找事件,处理事件
* This class represents a simulation entity. An entity handles events and can
* send events to other entities. When this class is extended, there are a few
* methods that need to be implemented:
* <ul>
* <li> {@link #startEntity()} is invoked by the {@link Simulation} class when
* the simulation is started. This method should be responsible for starting the
* entity up.
* <li> {@link #processEvent(SimEvent)} is invoked by the {@link Simulation}
* class whenever there is an event in the deferred queue, which needs to be
* processed by the entity.
* <li> {@link #shutdownEntity()} is invoked by the {@link Simulation} before the
* simulation finishes. If you want to save data in log files this is the method
* in which the corresponding code would be placed.
* </ul>
* @author Marcos Dias de Assuncao
* @since CloudSim Toolkit 1.0
*/
public abstract class SimEntity implements Cloneable {
/**
* 实体字段:
* 1、名字name
* 2、实体的id
* 3、从defer Queue中的事件evbuf
* 4、实体现在的状态state:RUNNABLE,WAITING,HOLDING,FINISHED
*/
/** The name. */
private String name;
/** The id. */
private int id;
/** The buffer for selected incoming events. */
private SimEvent evbuf;
/** The entity's current state. */
private int state;
/**
* Creates a new entity.
*
* @param name the name to be associated with this entity
*/
public SimEntity(String name) {
//名字不可以包含空格
if (name.indexOf(" ") != -1) {
throw new IllegalArgumentException(
"Entity names can't contain spaces.");
}
this.name = name;
id = -1; //默认id为-1
state = RUNNABLE; //实体创建后的状态为运行状态
CloudSim.addEntity(this); //实体添加到cloudsim的实体列表中
}
/**
* Get the name of this entity.
*
* @return The entity's name
*/
public String getName() {
return name;
}
/**
* Get the unique id number assigned to this entity.
*
* @return The id number
*/
public int getId() {
return id;
}
// The schedule functions
/**
* 发送事件,有cloudsim组建成simevent,调用futureQueue方法加入future queue队列中
* Send an event to another entity by id number, with data. Note that the
* tag <code>9999</code> is reserved.
* @param dest The unique id number of the destination entity
* @param delay How long from the current simulation time the event
* should be sent
* @param tag An user-defined number representing the type of event.
* @param data The data to be sent with the event.
*/
public void schedule(int dest, double delay, int tag, Object data) {
if (!CloudSim.running()) {
return;
}
CloudSim.send(id, dest, delay, tag, data);
}
/**
* Send an event to another entity by id number and with <b>no</b> data.
* Note that the tag <code>9999</code> is reserved.
*
* @param dest The unique id number of the destination entity
* @param delay How long from the current simulation time the event
* should be sent
* @param tag An user-defined number representing the type of event.
*/
public void schedule(int dest, double delay, int tag) {
schedule(dest, delay, tag, null);
}
/**
* Send an event to another entity through a port with a given name, with
* data. Note that the tag <code>9999</code> is reserved.
*
* @param dest The name of the port to send the event through
* @param delay How long from the current simulation time the event
* should be sent
* @param tag An user-defined number representing the type of event.
* @param data The data to be sent with the event.
*