?? application.cc
字號(hào):
#include "application.h"//實(shí)現(xiàn)分布式應(yīng)用 泛洪算法
void application::initialize()
{
last_seen_id=-1;
msgcount=0;
//設(shè)置 零 node點(diǎn)廣播信息 自消息為10s 調(diào)度一次
if(ID==0)
{
cMessage *msg=new cMessage("selfmessage");
msg->setKind(M_SELF);
scheduleAt(10,msg);
}
}
void application::finish()
{
ev<<"node"<<ID<<"generated/forwarded:"<<msgcount<<"message.\n";
}
void application::handleMessage(cMessage *msg)
{
//判斷消息類型
switch(msg->kind())
{
case M_LOWHIGH: //來在低層 若消息名與flood相同 用processflood函數(shù)處理 若不是 結(jié)束模擬
{
if(strcmp(msg->name(),"FLOOD")==0)
{
processFlood(msg);
return;
}
ev<<"unknown message received from another node.\n";
endSimulation();
}
case M_SELF: //若是自消息 產(chǎn)生新消息 設(shè)置類型為 m—highlow 并且追加flood—id=last seen id 將此消息通過 //lowergate-out發(fā)送出去
{
cMessage *msgnew=new cMessage("FLOOD");//若是自消息 產(chǎn)生新消息
msgnew->setKind(M_HIGHLOW);
msgnew->addPar("flood_id")=++last_seen_id;
send(msgnew,"lowergate_out");
//更新時(shí)間
UPDATECOLOR(msgcount+2);
//跟新信息計(jì)數(shù)器
msgcount++;
//再次設(shè)置計(jì)時(shí)器 ,自消息設(shè)置
scheduleAt(simTime()+GENERATIONDELAY,msg);
break;
}
default:
ev<<"unknown message received\n";
endSimulation();//看不懂哦!!
}
}
void application::processFLood(cMessage *msg)//處理泛宏消息
{
if(ID==0)
{
delete msg;
return;
}//若是發(fā)送節(jié)點(diǎn),刪除
//判斷此信息 之前是否收到過,若是刪除不轉(zhuǎn)發(fā) 若不是 先設(shè)置消息類型為m-highlow 然后通過lowergate-out們發(fā)送出去
int flood_id=(int)msg->par("flood_id");
if(flood_id<=last_seen_id)//這是干什么哦 看不懂
{
delete msg;
return;
}
msg->setKind(M_HIGHLOW);//若不是 傳送消息
sendDelayed(msg,RETRANSMITDELAY,"lowergate_out");
UPDATECOLOR(floo_id+2);//換色
//跟新信息計(jì)數(shù)器
msgcount++;
last_seen_id=flood_id;//?????????????
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -