📄 layer0.cpp.bak
字号:
//-------------------------------------------------------------------
// file name: layer0.cpp
//
// - contains the implementation of layer0 class
//
//-------------------------------------------------------------------
#include "layer0.h"
#include <cQueue.h>
#include <string.h>
#include <cVisitor.h>
#include <cObject.h>
void layer0::initialize()
{
/* for(int i=0;i<NNODES;i++)
data[i]=0;
*/
}
void layer0::finish()
{
}
void layer0::activity()
{
// main loop
while(1)
{
cMessage *msg=receive();
double time=parentModule()->par("ENERGY");
switch (msg->kind())
{
case M_NODE: // message from another node
{
// ev<<"receive message from "<<msg->par("senderId")<<endl;
parentModule()->par("ENERGY")=time-1;
int senderPX=msg->par("senderPX");
int senderPY=msg->par("senderPY");
int senderGX=msg->par("senderGX");
int senderGY=msg->par("senderGY");
int arrivalGX=this->parentModule()->par("GX");
int arrivalGY=this->parentModule()->par("GY");
int arrivalPX=this->parentModule()->par("PX");
int arrivalPY=this->parentModule()->par("PY");
//compute the nodes's distance
int dis=(senderPX-arrivalPX)*(senderPX-arrivalPX)+(senderPY-arrivalPY)*(senderPY-arrivalPY);
msg->addPar("distance")=dis;
if(senderGX==arrivalGX&&senderGY==arrivalGY)
{
msg->setKind(M_LOWHIGH);
send(msg,"uppergate_out");
}
//Notice:it is not necessary if only for addressing
else
{
int amcluster=(int)this->parentModule()->par("ISHEAD");
if(amcluster==1)
{
msg->setKind(M_LOWHIGH);
send(msg,"uppergate_out");
}
else
{delete msg;}
}
}
break;
case M_HIGHLOW: // message from a higher layer
{
// ev<<"receive message from application"<<endl;
msg->setKind(M_NODE);
int nummsg=0;
// duplicate the message and send it through all the gates
for (int i=0;i<NNODES;i++)
{
cGate *tmpgate = parentModule()->gate("out",i);
if (tmpgate==NULL)
continue;
// if gate is connected, send a message through it
if (tmpgate->toGate()!=NULL)
{
cMessage *tmpmsg = new cMessage(*msg);
send(tmpmsg,"lowergate_out",i);
nummsg++;
/* cGate *gate=tmpgate->toGate();
cModule *mod=gate->ownerModule();
int id=mod->index();
data[id]=1;
*///no use now
// ev<<"node "<<ID<<" send message to "<<i<<endl;
}
}
parentModule()->par("ENERGY")=time-nummsg;
delete msg;
}
break;
case M_SELF:
send(msg,"uppergate_out");
break;
default: // unknown message received - bug!.
ev << "layer0::activity() error: unknow message received\n";
endSimulation();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -