📄 application.cc
字号:
#include "application.h"//实现分布式应用 泛洪算法
void application::initialize()
{
last_seen_id=-1;
msgcount=0;
//设置 零 node点广播信息 自消息为10s 调度一次
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函数处理 若不是 结束模拟
{
if(strcmp(msg->name(),"FLOOD")==0)
{
processFlood(msg);
return;
}
ev<<"unknown message received from another node.\n";
endSimulation();
}
case M_SELF: //若是自消息 产生新消息 设置类型为 m—highlow 并且追加flood—id=last seen id 将此消息通过 //lowergate-out发送出去
{
cMessage *msgnew=new cMessage("FLOOD");//若是自消息 产生新消息
msgnew->setKind(M_HIGHLOW);
msgnew->addPar("flood_id")=++last_seen_id;
send(msgnew,"lowergate_out");
//更新时间
UPDATECOLOR(msgcount+2);
//跟新信息计数器
msgcount++;
//再次设置计时器 ,自消息设置
scheduleAt(simTime()+GENERATIONDELAY,msg);
break;
}
default:
ev<<"unknown message received\n";
endSimulation();//看不懂哦!!
}
}
void application::processFLood(cMessage *msg)//处理泛宏消息
{
if(ID==0)
{
delete msg;
return;
}//若是发送节点,删除
//判断此信息 之前是否收到过,若是删除不转发 若不是 先设置消息类型为m-highlow 然后通过lowergate-out们发送出去
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);//换色
//跟新信息计数器
msgcount++;
last_seen_id=flood_id;//?????????????
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -