📄 manager.cpp
字号:
//-------------------------------------------------------------------
// 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;
// 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=350,350,exact");
//int dist = 200;
// generate randomly the initial positions of the nodes
for (i=0;i<16;i++)
{
pos[i].x=100*(i/4);
pos[i].y=100*(i%4);
// 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);
//head[i][j]=1;
cModule *mod = this->parentModule()->submodule("snode",i);
// update each node data structure
mod->par("PX") = pos[i].x;
mod->par("PY") = pos[i].y;
mod->par("id") =i;
//int thisgx=choosegird(pos[i].x);
// int id=choosenum(i);
//int x=pos[i].x;
//int y= pos[i].y;
//int thisgy=choosegird(pos[i].y);
//mod->par("GX") = thisgx;
//mod->par("GY") = thisgy;
//energy=(int)mod->par("ENERGY");
for(j=0;j<16;j++)
{
if((i==j+4)&&((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y)<30000))
{cModule *mod2;
mod2 = this->parentModule()->submodule("snode",j);
cGate *gate6 = NULL;
cGate *gate7 = NULL;
int cnt1,cnt2;
for (cnt1=0;cnt1<NNODES;cnt1++)
{
gate6 = mod->gate("in",cnt1);
if (!gate6->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++)
{
gate7 = mod2->gate("out",cnt2);
if (!gate7->isConnected())
break;
}
if (cnt2==NNODES)
{
ev << "error: maximum connectivity reached (2).\n";
endSimulation();
}
// connect the gates
gate6->setFrom(gate7);
gate7->setTo(gate6);
gate6->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate7->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
// connect the reverse link
gate6 = mod->gate("out",cnt1);
gate7 = mod2->gate("in",cnt2);//110
gate6->setTo(gate7);
gate7->setFrom(gate6);
gate6->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate7->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
// delete msg;
//break;
}
if((i==j+3)&&((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y)<25000))
{
cModule *mod2;
mod2 = this->parentModule()->submodule("snode",j);
cGate *gate11 = NULL;
cGate *gate12 = NULL;
int cnt1,cnt2;
for (cnt1=0;cnt1<NNODES;cnt1++)
{
gate11 = mod->gate("in",cnt1);
if (!gate11->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++)
{
gate12 = mod2->gate("out",cnt2);
if (!gate12->isConnected())
break;
}
if (cnt2==NNODES)
{
ev << "error: maximum connectivity reached (2).\n";
endSimulation();
}
// connect the gates
gate11->setFrom(gate12);
gate12->setTo(gate11);
gate11->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate12->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
// connect the reverse link
gate11 = mod->gate("out",cnt1);
gate12 = mod2->gate("in",cnt2);//110
gate11->setTo(gate12);
gate12->setFrom(gate11);
gate11->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate12->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
}
if((i==j+5)&&((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y)<25000))
{
cModule *mod2;
mod2 = this->parentModule()->submodule("snode",j);
cGate *gate1 = NULL;
cGate *gate2 = NULL;
int cnt1,cnt2;
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);//110
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);
}
if((i==j+1)&&((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y)<25000))
{
cModule *mod2;
mod2 = this->parentModule()->submodule("snode",j);
cGate *gate3 = NULL;
cGate *gate4 = NULL;
int cnt1,cnt2;
for (cnt1=0;cnt1<NNODES;cnt1++)
{
gate3 = mod->gate("in",cnt1);
if (!gate3->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++)
{
gate4 = mod2->gate("out",cnt2);
if (!gate4->isConnected())
break;
}
if (cnt2==NNODES)
{
ev << "error: maximum connectivity reached (2).\n";
endSimulation();
}
// connect the gates
gate3->setFrom(gate4);
gate4->setTo(gate3);
gate3->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate4->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
// connect the reverse link
gate3 = mod->gate("out",cnt1);
gate4 = mod2->gate("in",cnt2);//110
gate3->setTo(gate4);
gate4->setFrom(gate3);
gate3->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate4->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
}
}
}
int source=(int)this->par("IPs");
int destination=(int)this->par("IPd");
//int destination=(int)this->parentModule()->par("IPd");
cModule *mod = this->parentModule()->submodule("snode",source);
cGate *gate13=mod->gate("in",source);
cGate *gate14=this->gate("out");
gate14->disconnect();
gate13->setFrom(gate14);
gate14->setTo(gate13);
gate13->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate14->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
//cMessage *msgnew=new cMessage("Start");
//msgnew->setKind(M_SELF);
/*cMessage *msg=new cMessage("Start");
msg->setKind(M_SELF);
msg->addPar("destination")=this->par("IPd");
scheduleAt(simTime()+GENERATEDELAY,msg);*/
cMessage *msg=new cMessage("Start");
msg->setKind(M_SELF);
msg->addPar("IPd")=destination;
scheduleAt(simTime()+GENERATEDELAY,msg);
//send(msg,"out");
}
void manager::handleMessage(cMessage *msg)
//void manager::activity()
{
//cMessage *msg=receive();
switch(msg->kind())
{
case M_SELF:
{
int i,j;
//topo();
if(numofrun==0)
break;
else
{
// sendgb();
/* int source=(int)this->par("IPs");
//int destination=(int)this->parentModule()->par("IPd");
cModule *mod = this->parentModule()->submodule("snode",source);
cGate *gate13=mod->gate("in",source);
cGate *gate14=this->gate("out");
gate14->disconnect();
gate13->setFrom(gate14);
gate14->setTo(gate13);
gate13->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
gate14->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);*/
cMessage *msgnew=new cMessage("Start");
msgnew->setKind(M_SELF);
msgnew->addPar("IPd")=msg->par("IPd");
//msgnew->addPar("senderPY")=0;
send(msgnew,"out");
// gate14->disconnect();
//delete msg;
}
}
--numofrun;
scheduleAt(simTime()+GENERATEDELAY,msg);
break;
default:
ev<<"manager: unkown message kind is received,the simulation ended!"<<endl;
endSimulation();
delete msg;
}
}
void manager::finish()
{
//topo();
}
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()
{
/*int nhead=0;
int i,j;
//输出簇头节点及其簇内节点
for(i=0;i<NNODES;i++)
{
cModule *mod=parentModule()->submodule("snode",i);
int amhead=mod->par("ISHEAD");
if(amhead==1)
{
int amaddred=mod->par("ISADDRED");
int id=mod->index();
int address=mod->par("MYADDR");
ev<<"cluster head node:"<<i<<" "<<" isaddred:"<<amaddred<<" address is:"<<address<<endl;
ev<<"the other nodes of the cluster(grid): ";
ev<<endl;
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();
int mod1head=mod1->par("ISHEAD");
int mod1addred=mod1->par("ISADDRED");
int addresss=(int)mod1->par("MYADDR");
if(mod1head!=1)
ev<<" "<<id<<" "<<"isaddred:"<<mod1addred<<" "<<"address is:"<<addresss;
ev<<endl;
}
}
ev<<endl;
ev<<endl;
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -