📄 manager.cpp
字号:
//-------------------------------------------------------------------// file name: manager.cpp// // - contains the implementation of manager class////-------------------------------------------------------------------#include "manager.h"#include <string>#include <fstream>void manager::initialize(){ 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=-100,-100"); int dist = 200; // generate randomly the initial positions of the nodes nodepos pos[NNODES]; 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("o=grey;"); gate2->setDisplayString("o=grey;"); // connect the reverse link gate1 = mod1->gate("out",cnt1); gate2 = mod2->gate("in",cnt2); gate1->setTo(gate2); gate2->setFrom(gate1); gate1->setDisplayString("o=grey;"); gate2->setDisplayString("o=grey;"); } } } // 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; }}
void manager::handleMessage(cMessage *msg)
{
delete msg;
}
void manager::finish()
{
topo();
}
void manager::topo()
{
int nhead=0;
int i,j;
ev<<endl<<endl;
//输出簇头节点及其簇内节点
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<<" other nodes: ";
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;
}
}
//输出黑色节点间共同的灰色节点
/* 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 number of head nodes is :"<<nhead<<endl<<endl;
ev<<"the whole simulation time is : "<<simTime()<<endl<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -