📄 manager.cpp
字号:
//-------------------------------------------------------------------
// file name: manager.cpp
//
// - contains the implementation of manager class
//
//-------------------------------------------------------------------
#include "manager.h"
#include <string>
#include <fstream>
#define GENERATEDELAY 300
void manager::initialize()
{
numofrun=10;
int i,j;
// check out the parameters in the .ini file and in the .h file
if ((int)this->parentModule()->par("NNODES")!=NNODES)
{
ev << "manager::initialize() error: parameter NNODES is different in .ini and in .h files\n";
endSimulation();
}
// hide the manager from the display
setDisplayString("p=30,30,exact");
int dist = 200;
// generate randomly the initial positions of the nodes
for (i=0;i<NNODES;i++)
{
pos[i].x = intrand(SPACEX);
pos[i].y = intrand(SPACEY);
// display the node on the screen
char tempstring[30];
sprintf(tempstring,"p=%d,%d,exact;i=snode_%d_%d",pos[i].x+MX,pos[i].y+MY,1,2);
this->parentModule()->submodule("snode",i)->setDisplayString(tempstring);
// connect this module to whomever is in transmission range
for (j=0;j<i;j++)
{
// if the two nodes can hear each other then connect them
if (dist*dist>=((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x) + (pos[i].y-pos[j].y)*(pos[i].y-pos[j].y)))
{
cModule *mod1,*mod2;
mod1 = this->parentModule()->submodule("snode",i);
mod2 = this->parentModule()->submodule("snode",j);
cGate *gate1 = NULL;
cGate *gate2 = NULL;
int cnt1,cnt2;
// find the first empty gate on source
for (cnt1=0;cnt1<NNODES;cnt1++)
{
gate1 = mod1->gate("in",cnt1);
if (!gate1->isConnected())
break;
}
if (cnt1==NNODES)
{
ev << "error: maximum connectivity reached (1).\n";
endSimulation();
}
// find the first empty gate on destination
for (cnt2=0;cnt2<NNODES;cnt2++)
{
gate2 = mod2->gate("out",cnt2);
if (!gate2->isConnected())
break;
}
if (cnt2==NNODES)
{
ev << "error: maximum connectivity reached (2).\n";
endSimulation();
}
// connect the gates
gate1->setFrom(gate2);
gate2->setTo(gate1);
gate1->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate2->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
// connect the reverse link
gate1 = mod1->gate("out",cnt1);
gate2 = mod2->gate("in",cnt2);
gate1->setTo(gate2);
gate2->setFrom(gate1);
gate1->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate2->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
}
}
}
// update each node data structure
for (i=0;i<NNODES;i++)
{
cModule *mod = this->parentModule()->submodule("snode",i);
mod->par("PX") = pos[i].x;
mod->par("PY") = pos[i].y;
}
cMessage *msg=new cMessage("Start");
msg->setKind(M_SELF);
scheduleAt(simTime()+GENERATEDELAY,msg);
}
void manager::activity()
{
while(1)
{
cMessage *msg=receive();
switch(msg->kind())
{
case M_SELF:
{
cMessage *msgnew=new cMessage("Start");
msgnew->setKind(M_SELF);
topo();
if(numofrun==-1)
break;
choosehead();
send(msgnew,"out");
--numofrun;
scheduleAt(simTime()+GENERATEDELAY,msg);
}
break;
default:
ev<<"manager: unkown message kind is received,the simulation ended!"<<endl;
endSimulation();
delete msg;
}
}
}
void manager::finish()
{
}
void manager::choosehead()
{
ev<<endl<<endl<<"round "<<10-numofrun;
int i,k;
double maxtime=0;
for(i=0;i<NNODES;i++)
{
char tempstring[30];
sprintf(tempstring,"p=%d,%d,exact;i=snode_%d_%d",pos[i].x+MX,pos[i].y+MY,1,2);
this->parentModule()->submodule("snode",i)->setDisplayString(tempstring);
}
for(i=0;i<NNODES;i++)
{
cModule *mod=this->parentModule()->submodule("snode",i);
double time=mod->par("TOTALTIME");
int type=mod->par("TYPE");
mod->par("TYPE")=-1;
mod->par("ISREQ")=-1;
if(time<=0)
{
ev<<endl<<" energy of node "<<i<<" is exhaused , so discards it!"<<endl;
for(int j=0;j<NNODES;j++)
{
cGate *gate1=mod->gate("out",j);
if(gate1==NULL)
continue;
gate1->disconnect();
mod->par("TYPE")=2;
}
int x=mod->par("PX");
int y=mod->par("PY");
char tempstring[30];
sprintf(tempstring,"p=%d,%d,exact;i=snode_%d_%d",x+MX,y+MY,9,2);
mod->setDisplayString(tempstring);
}
if(maxtime<time)
{
k=i;
maxtime=time;
}
}
// ev<<"the new head is "<<k<<endl;
cModule *mod=this->parentModule()->submodule("snode",k);
// double time=mod->par("TOTALTIME");
// mod->par("TOTALTIME")=time-GENERATEDELAY;
cGate *gate1=mod->gate("in",NNODES);
cGate *gate2=this->gate("out");
gate2->disconnect();
gate1->setFrom(gate2);
gate2->setTo(gate1);
// gate1->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
// gate2->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
}
void manager::topo()
{
int nhead=0;
int i,j;
//输出簇头节点及其簇内节点
/* for(i=0;i<NNODES;i++)
{
cModule *mod=parentModule()->submodule("snode",i);
int type=mod->par("TYPE");
if(type==1)
{
nhead++;
ev<<"cluster head node:"<<i<<endl;
ev<<" the other nodes of the cluster: ";
for(j=0;j<NNODES;j++)
{
cGate *gate=mod->gate("out",j);
if(gate==NULL)
continue;
if(gate->toGate()!=NULL)
{
cGate *gate1=gate->toGate();
cModule *mod1=gate1->ownerModule();
int id=mod1->index();
ev<<id<<" ";
}
}
ev<<endl<<endl;
}
}
ev<<"the number of head nodes is :"<<nhead<<endl<<endl;
//输出黑色节点间共同的灰色节点
for(i=0;i<NNODES;i++)
{
cModule *mod=parentModule()->submodule("snode",i);
int type=mod->par("TYPE");
if(type==0)
{
int neibor[NNODES];
for(int k=0;k<NNODES;k++)
neibor[k]=0;
int headneib=0;
for(j=0;j<NNODES;j++)
{
cGate *gate=mod->gate("out",j);
if(gate==NULL)
continue;
if(gate->toGate()!=NULL)
{
cGate *tmpgate=gate->toGate();
cModule *tmpmod=tmpgate->ownerModule();
int type1=tmpmod->par("TYPE");
if(type1==1)
{
int id=tmpmod->index();
neibor[id]=1;
headneib++;
}
}
}
if(headneib>=2)
{
ev<<"node "<<i<<" connect head node : ";
for(int k=0;k<NNODES;k++)
if(neibor[k]==1)
ev<<k<<" ";
ev<<endl;
}
}
}*/
ev<<endl<<endl<<"the head nodes are : ";
for(i=0;i<NNODES;i++)
{
cModule *mod=parentModule()->submodule("snode",i);
int type=mod->par("TYPE");
if(type==1)
ev<<i<<" ";
}
ev<<endl<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -