📄 manager.cpp.bak
字号:
//-------------------------------------------------------------------
// file name: manager.cpp
//
// - contains the implementation of manager class
//
//-------------------------------------------------------------------
#include "manager.h"
#include <string>
#include <fstream>
#define GENERATEDELAY 1
void manager::initialize()
{
numofrun=1;
int i,j;
int a,b;
int energy;
for(int ii=0;ii<GX;ii++)
for(int jj=0;jj<GY;jj++)
{
head[ii][jj]=-1;
henergy[ii][jj]=0;
}
// 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);
cModule *mod = this->parentModule()->submodule("snode",i);
// update each node data structure
mod->par("PX") = pos[i].x;
mod->par("PY") = pos[i].y;
int thisgx=choosegird(pos[i].x);
int thisgy=choosegird(pos[i].y);
mod->par("GX") = thisgx;
mod->par("GY") = thisgy;
energy=(int)mod->par("ENERGY");
if(energy>=henergy[thisgx][thisgy])
{
henergy[thisgx][thisgy]=energy;
head[thisgx][thisgy]=i;
}
// connect the nodes in the same grid
for (j=0;j<i;j++)
{
cModule *mod2;
mod2 = this->parentModule()->submodule("snode",j);
int mod2gx=(int)mod2->par("GX");
int mod2gy=(int)mod2->par("GY");
//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)))
if(thisgx==mod2gx&&thisgy==mod2gy)
{
cGate *gate1 = NULL;
cGate *gate2 = NULL;
int cnt1,cnt2;
// find the first empty gate on source
for (cnt1=0;cnt1<NNODES;cnt1++)
{
gate1 = mod->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 = mod->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);
}
}////////////////////////connect the nodes in the same gird
}
//connect the clusterhead node which are in the range
for(i=0;i<GX;i++)
for(j=0;j<GY;j++)
{
if(head[i][j]!=-1)
{
cModule *moda = this->parentModule()->submodule("snode",head[i][j]);
for(a=0;a<=i;a++)
{
if(a<i)
{
for(b=0;b<GY;b++)
{
if(head[a][b]!=-1)
{
cModule *modb;
modb = this->parentModule()->submodule("snode",head[a][b]);
if (dist*dist>=((pos[head[i][j]].x-pos[head[a][b]].x)*(pos[head[i][j]].x-pos[head[a][b]].x)+(pos[head[i][j]].y-pos[head[a][b]].y)*(pos[head[i][j]].y-pos[head[a][b]].y)))
{
cGate *gate1 = NULL;
cGate *gate2 = NULL;
int cnt1,cnt2;
// find the first empty gate on source
for (cnt1=0;cnt1<NNODES;cnt1++)
{
gate1 = moda->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 = modb->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 = moda->gate("out",cnt1);
gate2 = modb->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);
}
}
}
}
else if(a==i)
{
for(b=0;b<j;b++)
{
if(head[a][b]!=-1)
{
cModule *modb;
modb = this->parentModule()->submodule("snode",head[a][b]);
if (dist*dist>=((pos[head[i][j]].x-pos[head[a][b]].x)*(pos[head[i][j]].x-pos[head[a][b]].x)+(pos[head[i][j]].y-pos[head[a][b]].y)*(pos[head[i][j]].y-pos[head[a][b]].y)))
{
cGate *gate1 = NULL;
cGate *gate2 = NULL;
int cnt1,cnt2;
// find the first empty gate on source
for (cnt1=0;cnt1<NNODES;cnt1++)
{
gate1 = moda->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 = modb->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 = moda->gate("out",cnt1);
gate2 = modb->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);
}
}
}
}
}
}
}
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:
{
int i,j;
//topo();
if(numofrun==-1)
break;
for(i=0;i<GX;i++)
for(j=0;j<GY;j++)
{
if(head[i][j]!=-1)
{
cModule *mod = this->parentModule()->submodule("snode",head[i][j]);
cGate *gate1=mod->gate("in",NNODES);
cGate *gate2=this->gate("out");
gate2->disconnect();
gate1->setFrom(gate2);
gate2->setTo(gate1);
cMessage *msgnew=new cMessage("Start");
msgnew->setKind(M_SELF);
send(msgnew,"out");
gate2->disconnect();
}
}
--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()
{
}
int manager::choosegird(int p)
{
int result=0;
double flag=0.0;
if(p%GSIZE==0)
{
flag=dblrand();
if(flag>=0.5)
result=p/GSIZE+1;
}
else
result=p/GSIZE;
return result;
}
void manager::topo()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -