CloudSim源代码之SimEntity.docx

上传人:b****4 文档编号:3802917 上传时间:2022-11-25 格式:DOCX 页数:20 大小:19.87KB
下载 相关 举报
CloudSim源代码之SimEntity.docx_第1页
第1页 / 共20页
CloudSim源代码之SimEntity.docx_第2页
第2页 / 共20页
CloudSim源代码之SimEntity.docx_第3页
第3页 / 共20页
CloudSim源代码之SimEntity.docx_第4页
第4页 / 共20页
CloudSim源代码之SimEntity.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

CloudSim源代码之SimEntity.docx

《CloudSim源代码之SimEntity.docx》由会员分享,可在线阅读,更多相关《CloudSim源代码之SimEntity.docx(20页珍藏版)》请在冰豆网上搜索。

CloudSim源代码之SimEntity.docx

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

*tag9999isreserved.

*

*@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.

*Notethatthetag9999isreserved.

*

*@paramdestTheuniqueidnumberofthedestinationentity

*@paramdelayHowlongfromthecurrentsimulationtimetheevent

*shouldbesent

*@paramtagAnuser-definednumberrepresentingthetypeofevent.

*/

publicvoidschedule(intdest,doubledelay,inttag){

schedule(dest,delay,tag,null);//data为null

}

/**【通过给定的端口发送一事件到另一实体含数据】

*Sendaneventtoanotherentitythroughaportwithagivenname,with

*data.Notethatthetag9999isreserved.

*

*@paramdest【端口】Thenameoftheporttosendtheeventthrough

*@paramdelayHowlongfromthecurrentsimulationtimetheevent

*shouldbesent

*@paramtagAnuser-definednumberrepresentingthetypeofevent.

*@paramdataThedatatobesentwiththeevent.

*/

publicvoidschedule(Stringdest,doubledelay,inttag,Objectdata){

schedule(CloudSim.getEntityId(dest),delay,tag,data);//注意与上面对比

}

/**【通过给定的端口发送一事件到另一实体无数据】

*Sendaneventtoanotherentitythroughaportwithagivenname,with

*nodata.Notethatthetag9999isreserved.

*

*@paramdestThenameoftheporttosendtheeventthrough

*@paramdelayHowlongfromthecurrentsimulationtimetheeventshouldbe

*sent

*@paramtagAnuser-definednumberrepresentingthetypeofevent.

*/

publicvoidschedule(Stringdest,doubledelay,inttag){

schedule(dest,delay,tag,null);

}

/**【通过ID发送一事件给另一实体含数据】

*Sendaneventtoanotherentitybyidnumber,withdata.Notethatthe

*tag9999isreserved.

*

*@paramdestTheuniqueidnumberofthedestinationentity

*@paramtagAnuser-definednumberrepresentingthetypeofevent.

*@paramdataThedatatobesentwiththeevent.

*/

publicvoidscheduleNow(intdest,inttag,Objectdata){

schedule(dest,0,tag,data);//scheduleNowdelay=0

}

/**【通过ID发送一事件给另一实体无数据】

*Sendaneventtoanotherentitybyidnumberandwithnodata.

*Notethatthetag9999isreserved.

*

*@paramdestTheuniqueidnumberofthedestinationentity

*@paramtagAnuser-definednumberrepresentingthetypeofevent.

*/

publicvoidscheduleNow(intdest,inttag){

schedule(dest,0,tag,null);

}

/**【通过给定的端口发送一事件到另一实体含数据】

*Sendaneventtoanotherentitythroughaportwithagivenname,with

*data.Notethatthetag9999isreserved.

*

*@paramdestThenameoftheporttosendtheeventthrough

*@paramtagAnuser-definednumberrepresentingthetypeofevent.

*@paramdataThedatatobesentwiththeevent.

*/

publicvoidscheduleNow(Stringdest,inttag,Objectdata){

schedule(CloudSim.getEntityId(dest),0,tag,data);

}

/**【通过给定的端口发送一事件到另一实体不含数据】

*Sendaneventtoanotherentitythroughaportwithagivenname,with

*nodata.Notethatthetag9999isreserved.

*

*@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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 教育学心理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1