📄 wirelesschannelsimple.cc
字号:
#include "Gear_packet_m.h"#include "Phy_Packet_m.h"#include "WirelessChannelSimple.h"Define_Module_Like( WirelessChannelSimple, WirelessChannelModule )WirelessChannelSimple::WirelessChannelSimple(const char *name, cModule *parentModule, unsigned stacksize) : WirelessChannelBase(name, parentModule, stacksize){//printf("\n\t In constructor for WirelessChannelSimple\n");}void WirelessChannelSimple::initialize(){// printf("\n\t In WirelessChannelSimple:initialize."); ev << "\n\t In WirelessChannelSimple:initialize."; m_iState = INITIALIZED; printf("The Date is===%ld \n", time(NULL));/* for(int i=0;i< ((int)par("numOfSensorNodes"));i++) { neighArray[i][1]= 0; }*/}void WirelessChannelSimple::finish(){ printf("The Date is===%ld", time(NULL));// for(int i=0;i<(int)(par("numOfSensorNodes"));i++)// printf(" The neighbor count for nodeId %d is %d\n",i,neighArray[i][0]);}void WirelessChannelSimple::handleMessage(cMessage *pMsg){ //printf("\n\t In WirelessChannelSimple::handleMessage"); ev << "\n\t In WirelessChannelSimple::handleMessage"; cGate* arrivalGate = pMsg->arrivalGate(); //printf("GATE INDEX %d\n",arrivalGate->index()); const int fromNODELayerGate = gate("fromTopLayer")->id(); const int fromWirelessChanGate = gate("fromBottomLayer")->id(); sPhy_Header* hdr=&((cPhy_Packet*)pMsg)->getHdr(); //printf(" fromNODELayer %d \n",fromNODELayerGate); //Header* hdr=&((GearPacket*)cMsg)->getHdr(); int NodeId=hdr->SourceNodeAddress; //int iMsgKind = cMsg->kind(); int iTargetXPos = pMsg->par("xPos"); int iTargetYPos = pMsg->par("yPos"); int iRadioPower = pMsg->par("RadioPower"); cArray ListOfReachableNeighbors; int iSuccess = getReachableNeighbors(iTargetXPos, iTargetYPos, NodeId, iRadioPower, &ListOfReachableNeighbors); for (int iNeighborIndex = 0; iNeighborIndex < ListOfReachableNeighbors.items(); iNeighborIndex++) { // printf("\n\t Wchan: Inside for loop for %d", NodeId); //if((NodeId==6))//||(NodeId==4)) // { //printf(" Neighbor of nodeId %d is %d \n ",NodeId,((NodeInfo*)ListOfReachableNeighbors.get(iNeighborIndex))->GetNodeId()); //sprintf(s,"Neighbhors of NodeId %d are %d ",NodeId,((NodeInfo*)ListOfReachableNeighbors.get(iNeighborIndex))->GetNodeId()); // recordScalar(s,111111); // } cMessage *copy = (cMessage *) pMsg->dup(); //copy->setKind(iMsgKind); int gateid =((NodeInfo*)ListOfReachableNeighbors.get(iNeighborIndex))->GetWirelessChannelGateId(); Heuristics *h= new Heuristics(); double dist = h->Distance(iTargetXPos,iTargetYPos,((NodeInfo*)ListOfReachableNeighbors.get(iNeighborIndex))->GetXPos(),((NodeInfo*)ListOfReachableNeighbors.get(iNeighborIndex))->GetYPos()); double propagationDelay=dist/SPEED_OF_LIGHT; //printf(" %f propagation delay between %d and %d is %.20f %d\n",simTime(),NodeId,((NodeInfo*)ListOfReachableNeighbors.get(iNeighborIndex))->GetNodeId(),propagationDelay,gateid); sendDelayed(copy,propagationDelay, gateid); //printf(" msg sent to %d \n",gateid); // send(copy,gateid); } delete pMsg; // delete &ListOfReachableNeighbors; // break;// }/* default: { //printf("\n\t Message of invalid type received."); break; }*/// } //return;}int WirelessChannelSimple::getReachableNeighbors(const int targetXPos, const int targetYPos,const int NodeId, const int iRadioPower, cArray *pReachableNodes){int xmin,xmax,ymin,ymax;// printf("\n\t WirelessChannelSimple::getReachableNeighbors, xpos=%d, ypos=%d, iRadioPower=%d.", targetXPos, targetYPos, iRadioPower); cModule *pNetwork = parentModule(); cPar parListOfNodes = pNetwork->par("ListOfNodes"); cArray *pListOfNodes = (cArray*)parListOfNodes.objectValue(); char s[50]; sprintf(s,"%d",NodeId); int index=pListOfNodes->find(s); NodeInfo* pNodeInfo=(NodeInfo*)pListOfNodes->get(index); int xCoOrd=pNodeInfo->GetXPos(); xmin =pNodeInfo->GetXPos() - iRadioPower; xmax = pNodeInfo->GetXPos() + iRadioPower; ymin = pNodeInfo->GetYPos() - iRadioPower; ymax = pNodeInfo->GetYPos() + iRadioPower; pNodeInfo=(NodeInfo*)pListOfNodes->get(index-1); //printf("index= %d \n",index); // printf("\n\t WirelessChannelSimple::getReachableNeighbors, Neighbour count: %d.", pListOfNodes->items()); for (int iNodeIndex = index-1 ; iNodeIndex >= 0 && pNodeInfo->GetXPos()>=xmin; iNodeIndex--) { if( true != pListOfNodes->exist(iNodeIndex) ) { if(iNodeIndex-1>=0) { pNodeInfo=(NodeInfo*)pListOfNodes->get(iNodeIndex-1); continue; } } if(pNodeInfo->GetYPos() >=ymin &&pNodeInfo->GetYPos()<=ymax) { NodeInfo *pNeighborToAdd = new NodeInfo(pNodeInfo); pReachableNodes->add(pNeighborToAdd); //printf("NodeId %d added\n",iNodeIndex); } if(iNodeIndex-1>=0) pNodeInfo=(NodeInfo*)pListOfNodes->get(iNodeIndex-1); } pNodeInfo=(NodeInfo*)pListOfNodes->get(index+1); for (int iNodeIndex = index+1 ; iNodeIndex < pListOfNodes->items()&&pNodeInfo->GetXPos()<=xmax; iNodeIndex++) { if( true != pListOfNodes->exist(iNodeIndex) ) { if(iNodeIndex+1< pListOfNodes->items()) { pNodeInfo=(NodeInfo*)pListOfNodes->get(iNodeIndex+1); continue; } } if(pNodeInfo->GetYPos() >=ymin &&pNodeInfo->GetYPos()<=ymax) { NodeInfo *pNeighborToAdd = new NodeInfo(pNodeInfo); pReachableNodes->add(pNeighborToAdd); //printf("NodeId %d added\n",iNodeIndex); } if(iNodeIndex+1 < pListOfNodes->items()) pNodeInfo=(NodeInfo*)pListOfNodes->get(iNodeIndex+1); } // Check if the node still exists in the network return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -