CloudSim源代码之SimEntity.docx
《CloudSim源代码之SimEntity.docx》由会员分享,可在线阅读,更多相关《CloudSim源代码之SimEntity.docx(20页珍藏版)》请在冰豆网上搜索。
CloudSim源代码之SimEntity
packageorg.cloudbus.cloudsim.core;
importorg.cloudbus.cloudsim.Log;
importorg.cloudbus.cloudsim.NetworkTopology;
importorg.cloudbus.cloudsim.core.predicates.Predicate;
/**仿真实体
*Thisclassrepresentsasimulationentity.Anentityhandleseventsandcan
*sendeventstootherentities.Whenthisclassisextended,thereareafew
*methodsthatneedtobeimplemented:
*
*
- 【{@link#startEntity()}】isinvokedbythe{@linkSimulation}classwhen
*thesimulationisstarted.Thismethodshouldberesponsibleforstartingthe
*entityup.
*
- 【{@link#processEvent(SimEvent)}】isinvokedbythe{@linkSimulation}
*classwheneverthereisaneventinthedeferredqueue,whichneedstobe
*processedbytheentity.
*
- 【{@link#shutdownEntity()}】isinvokedbythe{@linkSimulation}beforethe
*simulationfinishes.Ifyouwanttosavedatainlogfilesthisisthemethod
*inwhichthecorrespondingcodewouldbeplaced.
*
*
*@authorMarcosDiasdeAssuncao
*@sinceCloudSimToolkit1.0
*/
publicabstractclassSimEntityimplementsCloneable{
/**Thename.名称*/
privateStringname;
/**Theid.编号*/
privateintid;
/**Thebufferforselectedincomingevents.选定将来临的事件缓冲*/
privateSimEventevbuf;
/**Theentity'scurrentstate.实体现在状态*/
privateintstate;
/**【创建新实体】
*Createsanewentity.
*
*@paramnamethenametobeassociatedwiththisentity
*/
publicSimEntity(Stringname){
if(name.indexOf("")!
=-1){
thrownewIllegalArgumentException(
"Entitynamescan'tcontainspaces.");
}
this.name=name;
id=-1;
state=RUNNABLE;//运行
CloudSim.addEntity(this);
}
/**实体名
*Getthenameofthisentity.
*
*@returnTheentity'sname
*/
publicStringgetName(){
returnname;
}
/**分配给实体的ID号
*Gettheuniqueidnumberassignedtothisentity.
*
*@returnTheidnumber
*/
publicintgetId(){
returnid;
}
//Theschedulefunctions【调度函数】
/**
*Sendaneventtoanotherentitybyidnumber,withdata.Notethatthe
*tag9999
isreserved.
*
*@paramdest【目的实体ID】Theuniqueidnumberofthedestinationentity
*@paramdelayHowlongfromthecurrentsimulationtimetheevent
*shouldbesent
*@paramtag【事件类型】Anuser-definednumberrepresentingthetypeofevent.
*@paramdata【事件传递的数据】Thedatatobesentwiththeevent.
*/
publicvoidschedule(intdest,doubledelay,inttag,Objectdata){
if(!
CloudSim.running()){
return;
}
CloudSim.send(id,dest,delay,tag,data);
}
/**【通过ID发送一事件到另一实体无数据】
*Sendaneventtoanotherentitybyidnumberandwithnodata.
*Notethatthetag9999
isreserved.
*
*@paramdestTheuniqueidnumberofthedestinationentity
*@paramdelayHowlongfromthecurrentsimulationtimetheevent
*shouldbesent
*@paramtagAnuser-definednumberrepresentingthetypeofevent.
*/
publicvoidschedule(intdest,doubledelay,inttag){
schedule(dest,delay,tag,null);//data为null
}
/**【通过给定的端口发送一事件到另一实体含数据】
*Sendaneventtoanotherentitythroughaportwithagivenname,with
*data.Notethatthetag9999
isreserved.
*
*@paramdest【端口】Thenameoftheporttosendtheeventthrough
*@paramdelayHowlongfromthecurrentsimulationtimetheevent
*shouldbesent
*@paramtagAnuser-definednumberrepresentingthetypeofevent.
*@paramdataThedatatobesentwiththeevent.
*/
publicvoidschedule(Stringdest,doubledelay,inttag,Objectdata){
schedule(CloudSim.getEntityId(dest),delay,tag,data);//注意与上面对比
}
/**【通过给定的端口发送一事件到另一实体无数据】
*Sendaneventtoanotherentitythroughaportwithagivenname,with
*nodata.Notethatthetag9999
isreserved.
*
*@paramdestThenameoftheporttosendtheeventthrough
*@paramdelayHowlongfromthecurrentsimulationtimetheeventshouldbe
*sent
*@paramtagAnuser-definednumberrepresentingthetypeofevent.
*/
publicvoidschedule(Stringdest,doubledelay,inttag){
schedule(dest,delay,tag,null);
}
/**【通过ID发送一事件给另一实体含数据】
*Sendaneventtoanotherentitybyidnumber,withdata.Notethatthe
*tag9999
isreserved.
*
*@paramdestTheuniqueidnumberofthedestinationentity
*@paramtagAnuser-definednumberrepresentingthetypeofevent.
*@paramdataThedatatobesentwiththeevent.
*/
publicvoidscheduleNow(intdest,inttag,Objectdata){
schedule(dest,0,tag,data);//scheduleNowdelay=0
}
/**【通过ID发送一事件给另一实体无数据】
*Sendaneventtoanotherentitybyidnumberandwithnodata.
*Notethatthetag9999
isreserved.
*
*@paramdestTheuniqueidnumberofthedestinationentity
*@paramtagAnuser-definednumberrepresentingthetypeofevent.
*/
publicvoidscheduleNow(intdest,inttag){
schedule(dest,0,tag,null);
}
/**【通过给定的端口发送一事件到另一实体含数据】
*Sendaneventtoanotherentitythroughaportwithagivenname,with
*data.Notethatthetag9999
isreserved.
*
*@paramdestThenameoftheporttosendtheeventthrough
*@paramtagAnuser-definednumberrepresentingthetypeofevent.
*@paramdataThedatatobesentwiththeevent.
*/
publicvoidscheduleNow(Stringdest,inttag,Objectdata){
schedule(CloudSim.getEntityId(dest),0,tag,data);
}
/**【通过给定的端口发送一事件到另一实体不含数据】
*Sendaneventtoanotherentitythroughaportwithagivenname,with
*nodata.Notethatthetag9999
isreserved.
*
*@paramdestThenameoftheporttosendtheeventthrough
*@paramtagAnuser-definednumberrepresentingthetypeofevent.
*/
publicvoidscheduleNow(Stringdest,inttag){
schedule(dest,0,tag,null);
}
/**【设置实体一段时间闲置】
*Settheentitytobeinactiveforatimeperiod.
*
*@paramdelaythetimeperiodforwhichtheentitywillbeinactive
*/
publicvoidpause(doubledelay){
if(delay<0){
thrownewIllegalArgumentException("Negativedelaysupplied.");
}
if(!
CloudSim.running()){
return;
}
CloudSim.pause(id,delay);
}
/**【统计实体延期队列中多少个事件匹配等待】
*Counthowmanyeventsmatchingapredicatearewaitingintheentity's
*deferredqueue.
*
*@parampTheeventselectionpredicate
*
*@returnThecountofmatchingevents
*/
publicintnumEventsWaiting(Predicatep){
returnCloudSim.waiting(id,p);
}
/**【统计实体延期队列中多少个事件等待】
*Counthowmanyeventsarewaitingintheentity'sdeferredqueue.
*
*@returnThecountofevents
*/
publicintnumEventsWaiting(){
returnCloudSim.waiting(id,CloudSim.SIM_ANY);
}
/**抽出第一个事件
*Extractthefirsteventmatchingapredicatewaitingintheentity's
*deferredqueue.
*
*@parampTheeventselectionpredicate
*
*@returnthesimulationevent
*/
publicSimEventselectEvent(Predicatep){
if(!
CloudSim.running()){
returnnull;
}
returnCloudSim.select(id,p);
}
/**取消第一个匹配等待事件
*Cancelthefirsteventmatchingapredicatewaitingintheentity's
*futurequeue.
*
*@parampTheeventselectionpredicate
*
*@returnThenumberofeventscancelled(0or1)
*/
publicSimEventcancelEvent(Predicatep){
if(!
CloudSim.running()){
returnnull;
}
returnCloudSim.cancel(id,p);
}
/**延期队列第一个匹配事件
*Getthefirsteventmatchingapredicatefromthedeferredqueue,orif
*nonematch,waitforamatchingeventtoarrive.
*
*@parampThepredicatetomatch
*
*@returnthesimulationevent
*/
publicSimEventgetNextEvent(Predicatep){
if(!
CloudSim.running()){
returnnull;
}
if(numEventsWaiting(p)>0){
returnselectEvent(p);
}
returnnull;
}
/**等待匹配事件
*Waitforaneventmatchingaspecificpredicate.Thismethoddoesnot
*checktheentity'sdeferredqueue.
*
*@parampThepredicatetomatch
*/
publicvoidwaitForEvent(Predicatep){
if(!
CloudSim.running()){
return;
}
CloudSim.wait(id,p);
this.state=WAITING;
}
/**等候在实体延期队列中的第一个事件
*Getthefirsteventwaitingintheentity'sdeferredqueue,orifthere
*arenone,waitforaneventtoarrive.
*
*@returnthesimulationevent
*/
publicSimEventgetNextEvent(){
returngetNextEvent(CloudSim.SIM_ANY);
}
/**【仿真开始】
*Thismethodisinvokedbythe{@linkSimulation}classwhen
*thesimulationisstarted.Thismethodshouldberesponsibleforstartingthe
*entityup.
*/
publicabstractvoidstartEntity();
/**【处理事件】
*Thismethodisinvokedbythe{@linkSimulation}
*classwheneverthereisaneventinthedeferredqueue,whichneedstobe
*processedbytheentity.
*
*@paramevtheeventtobeprocessedbytheentity
*/
publicabstractvoidprocessEvent(SimEventev);
/**【仿真结束】
*Thismethodisinvokedbythe{@linkSimulation}beforethe
*simulationfinishes.Ifyouwanttosavedatainlogfilesthisisthemethod
*inwhichthecorrespondingcodewouldbeplaced.
*/
publicabstractvoidshutdownEntity();
publicvoidrun(){
SimEventev=evbuf!
=null?
evbuf:
getNextEvent();
while(ev!
=null){
processEvent(ev);
if(state!
=RUNNABLE){
break;
}
ev=getNextEvent();
}
evbuf=null;
}
/**【克隆实体】
*Getacloneoftheentity.Thisisusedwhenindependentreplications
*havebeenspecifiedasanoutputanalysismethod.Clonesorbackupsof
*theentitiesaremadeinthebeginningofthesimulationinorderto
*resettheentitiesforeachsubsequentreplication.Thismethodshould
*notbecalledbytheuser.
*
*@returnAcloneoftheentity
*
*@throwsCloneNotSupportedExceptiontheclonenotsupportedexception
*/
@Override
protectedfinalObjectclone()throwsCloneNo