📄 physic.cpp
字号:
#include "h/physic.h"#include <string.h>//define the sting that define the host displayDefine_Module(Physic);cNeighbour::cNeighbour(){};cNeighbour::~cNeighbour(){};void Physic::initialize(){ hosts.setName("hosts"); nbList.setName("nblist"); hosts.takeOwnership(false); // FIXME this is a terrible hack to prevent crash on exit because of double deletion --Andras nbList.takeOwnership(false); cModule *parent = parentModule(); cModule *mod; char str[90]; //posX = (int)parent->par("x"); //posY = (int)parent->par("y");
GetPos(parentModule()->id(), posX, posY); numHost = (int)parent->par("numHost"); power = (double) par("txPower"); rxThreshold = (double) par("rxThreshold");
range = sqrt(power/rxThreshold); delay = &par("channelDelay"); error = &par("channelError"); dataRate = &par("channelDatarate"); gatesNum = 0; nbCount = 0; msgWithErr =0; //show the node on the map //sprintf(str,displayS,posX,posY,"red");
sprintf(str, displayS, posX, posY, IDLE_COLOR); parentModule()->setDisplayString(0,str,true); //fullfill the hosts vector detectNeighbours();}void Physic::handleMessage(cMessage* msg){ d("\t--- physic ---"); char tmpstr[128]; int i, msgkind , destid;
bool blDraw;
msgkind = msg->kind();
if(msg->hasPar("dest"))
destid = (int)msg->par("dest");
else
destid = -1;
//send the messages to the neighbours
//range = sqrt(power/rxThreshold);
//updateConnections();
if(msg->arrivedOn("fromMac")) {
d("msg from Mac");
if(msgkind == MSG_JOIN)
blDraw = true;
else
blDraw = false;
updateConnections(blDraw,destid);
//sprintf(tmpstr, "physic: message from Mac range [%f]\n", range); //send the message to all the neighbouts broadcast(msg); if( msg!= NULL ) delete msg; } else { //arrived from outside the module d("msg from outside"); if(msg->hasBitError()) { d("received message with errors...discarding!"); msgWithErr++; delete msg; } else { d("got message from "<<msg->par("source")); send(msg,"toMac"); // FIXME it is illegal to send a msg object and keep referencing it!!! -Andras } }}void Physic::broadcast(cMessage*msg){ cNeighbour* n = NULL;
char tmpstr[128];
d("broadcast Out");
if(msg->hasPar("physic"))
msg->par("physic") = id();
else
msg->addPar("physic") = id();
sprintf(tmpstr, "[%d] Physic->bcast:: 消息类型 [%d]\n", parentModule()->id(), msg->kind );
Log(tmpstr);
for(int i =0; i < nbList.items() ;i++) { if(nbList[i]) { //a new copy of message has to be made cMessage* m = new cMessage(*msg); n = (cNeighbour*) nbList[i];
send(m,n->gateIndex);
sprintf(tmpstr, "physic:bcast[%d]->[%d]\n", parentModule()->id(), n->mobHost);
Log(tmpstr);
} }}void Physic::detectNeighbours(){ //pointer to the modules that are to be scanned cModule* mod; //stores the index returned by cArray.add() int pos = 0;
char tmpstr[128];
memset(tmpstr, 0x00, sizeof(tmpstr));
d("detectNeigh");
for(int i=1; i<= simulation.lastModuleId(); i++) { //scan the simulation module vector mod = (cModule*)simulation.module(i); if( strcmp(mod->name(), "physic") == 0) { cNeighbour *n = new cNeighbour;
n->ph = mod->id();
//remember its parent module too n->mobHost = mod->parentModule()->id(); n->gateIndex = -1;//not connected
n->degree = 0; //连通度为0 pos = hosts.add(n); if(i == id()) myI = pos; } }
//sprintf(tmpstr, "physic->查询节点myi[%d] and id(%d)\n", myI, parentModule()->id()); }
void Physic::detectNB()
{
cNeighbour* n;
cNeighbour* nl;
int i, j, count, band;
bool bl=false;
char tmpstr[128];
for(i=0;i<hosts.items();i++)
{
n = (cNeighbour*)hosts[i];
bl = false;
if( (n->ph != id()) && (isReachable(id(),n->ph, band)))
{
//查找是否已经插入nbList了
count = nbList.items();
sprintf(tmpstr, "[%d] detectNB 发现范围之内节点[%d] 现在度 [%d]\n", parentModule()->id(), n->mobHost, nbList.items(), count);
Log(tmpstr);
//for(i=0;i<nbList.items();i++)
for(j=0;j<count;j++)
{
nl = (cNeighbour*)nbList[j];
sprintf(tmpstr,"[%d]是否已经加入列表了::已经加入的节点[%d]\n", n->mobHost, nl->mobHost);
Log(tmpstr);
if(n->mobHost == nl->mobHost)
{
bl=true;
break;
} //如果发现了, 直接跳出来就可以了
}
if(!bl) //如果没有发现则加入
//if(nbList.find(n)==-1) //如果没有发现则加入
{
n->status = UNCLUSTERED;
n->degree = -1;
n->gateIndex = -1;
nbList.add(n);
sprintf(tmpstr,"&&&&&[%d]detectNB [%d]新加入现在邻接点个数[%d]\n", parentModule()->id(), n->mobHost, nbList.items());
Log(tmpstr);
}
}
}//end of for
//sprintf(tmpstr,"[%d]邻节点个数 [%d]\n", parentModule()->id(), nbCount);
//Log(tmpstr);
}void Physic::updateConnections(bool blDraw, int destid){ d("updateConnections"); cNeighbour* n; for(int i=0; i< numHost ; i++) { //check the link avoiding to analyze itself if ( ( i != myI) && checkConnection(i, blDraw, destid)){ n = (cNeighbour*)hosts[i]; d("link changed between this host and:"<<n->mobHost); //make the other node do the check if (n->mobHost < parentModule()->id()) { d("notify the disconnection to the remote node-"); Physic* remote = (Physic*)simulation.module(n->ph); // the index of this host on the remote hosts vector remote->checkConnection( myI, blDraw, destid); } } }}//returns true if the connection has changed statebool Physic::checkConnection(int i, bool blDraw, int destid){ d("checkConnection"); cNeighbour* n = (cNeighbour*)hosts[i];
int band;
char tmpstr[128]; //avoid the parent to this module parent
if( (n->ph != id()) && isReachable(id(),n->ph, band))
{
//if it is not known
if (nbList.find(n) == -1 )
{
nbList.add(n);
nbCount++;
d("connecting to"<<n->mobHost);
if((n->mobHost == destid)&&blDraw)
{
sprintf(tmpstr,"%%%%nbList中[%d]--Join-->[%d],draw black line\n", parentModule()->id(), n->mobHost);
Log(tmpstr);
n->gateIndex= connectModules(n->ph,n->mobHost, true);
}
else
{
sprintf(tmpstr,"%%%%[%d]--not dest or join msg-->[%d],draw gray line\n", parentModule()->id(), n->mobHost);
Log(tmpstr);
n->gateIndex= connectModules(n->ph,n->mobHost, false);
}
}
else //因为我在系统初始化时就取的邻接点信息,但是没有建立连接,所以现在建立
{
//if(n->gateIndex == -1)
// {
if((n->mobHost == destid)&&blDraw)
{
sprintf(tmpstr,"&&&&[%d]-join ->[%d],draw black line\n", parentModule()->id(), n->mobHost);
Log(tmpstr);
n->gateIndex= connectModules(n->ph,n->mobHost, true);
}
else
{
sprintf(tmpstr,"&&&&[%d]--not dest or join msg-->[%d],draw gray line\n", parentModule()->id(), n->mobHost);
Log(tmpstr);
n->gateIndex= connectModules(n->ph,n->mobHost, false);
}
// }
} // end of if ...else..
return true;
}
else
{
//check if it was connected
int a = nbList.find(n); d("host not reachable "<<n->mobHost); if(a!= -1) { d("diconnect from neighbour:"<<n->mobHost); cNeighbour* tmp; //remove do not delete the object from the //memory! the list stores only a pointer to the //original host object. nbList.remove(a); //avoid holes in the array if( (nbCount>1) && (a!=nbCount-1)) { tmp = (cNeighbour*)nbList.remove(nbCount-1); nbList.addAt(a,tmp); } nbCount--; n->gateIndex = -1; //disconnect disconnectFrom(n->ph,n->mobHost); return true; } else { d("but "<<n->ph<<" was not connected"); return false; } }}int Physic::connectModules(int rPhysic,int rMod, bool blDraw){ d("connectModules"); //a->b->c->d int a,b,c,d;
char tmpstr[128]; //pointer to the remote physical layer Physic *ph = (Physic*)simulation.module(rPhysic); //pointer to the remote host object cModule* mod = (cModule*) simulation.module(rMod); //create the gates and the connection whithin these modules setUpConn('O',a,b); //do the same for the remote node. Note that 'c' will be used later for the conn //between the two mobile hosts. ph->setUpConn('I',d,c);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -