⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sensorchannelsimple.cc

📁 基于Oment++的无线传感器网络仿真
💻 CC
字号:
#include "SensorChannelSimple.h"Define_Module_Like( SensorChannelSimple, SensorChannelModule )SensorChannelSimple::SensorChannelSimple(const char *name, cModule *parentModule, unsigned stacksize) :	SensorChannelBase(name, parentModule, stacksize){//	printf("\n\t In constructor for SensorChannelSimple");}void SensorChannelSimple::initialize(){	//printf("\n\t In SensorChannelSimple:initialize.");	//ev << "\n\t In SensorChannelSimple:initialize.";	m_iState = INITIALIZED;}void SensorChannelSimple::handleMessage(cMessage *cMsg){	printf("\n\t In SensorChannelSimple::handleMessage");	//ev << "\n\t In SensorChannelSimple::handleMessage";	int iMsgKind = cMsg->kind();	switch(iMsgKind) {		case TARGET:		{			printf("\n\t message of type TARGET received in SensorChannelSimple::handleMessage()");						int iTargetXPos = cMsg->par("xPos");			int iTargetYPos = cMsg->par("yPos");			int iRadius     = par("Radius");			cArray ListOfReachableNeighbors;			int iSuccess = getReachableNeighbors(iTargetXPos, iTargetYPos, iRadius, &ListOfReachableNeighbors);			//printf("\n\t Number of reachable neighbors: %d", ListOfReachableNeighbors.items());			for (int iNeighborIndex = 0; iNeighborIndex < ListOfReachableNeighbors.items(); iNeighborIndex++)			{				//printf("\n\t Insided for loop for %d", iNeighborIndex);				cMessage *copy = (cMessage *) cMsg->dup();				int gateid =((NodeInfo*)ListOfReachableNeighbors.get(iNeighborIndex))->GetSensorChannelGateId(); 				send(copy, gateid);			}			delete cMsg;			break;		}		default:		{			//printf("\n\t Invalid message received in SensorChannelSimple::handleMessage()");			break;		}	}	return;}int SensorChannelSimple::getReachableNeighbors(const int targetXPos, const int targetYPos, const int iRadius, cArray *pReachableNodes){	//printf("\n\t SensorChannelSimple::getReachableNeighbors, xpos=%d, ypos=%d, iRadius=%d.", targetXPos, targetYPos, iRadius);	cModule *pNetwork = parentModule();	cPar parListOfNodes = pNetwork->par("ListOfNodes");	cArray *pListOfNodes = (cArray*)parListOfNodes.objectValue();//	printf("\n\t SensorChannelSimple::getReachableNeighbors, Neighbour count: %d.", pListOfNodes->items());	for (int iNodeIndex = 0 ; iNodeIndex < pListOfNodes->items() ; iNodeIndex++)	{		// Check if the node still exists in the network		if( true != pListOfNodes->exist(iNodeIndex) )			continue;				NodeInfo *pNeighbor = (NodeInfo*)pListOfNodes->get(iNodeIndex);		int iNeighborXPos = pNeighbor->GetXPos();		int iNeighborYPos = pNeighbor->GetYPos();//		printf("\n\t SensorChannelSimple::getReachableNeighbors, nxpos=%d, nypos=%d", iNeighborXPos, iNeighborYPos);		// Using the standard equation of circle with sensing radius to determine which sensor nodes will		// be affected by the target node...We assume the center of the circle to be at the Target Node		int iTerm1 = (int) pow((targetXPos-iNeighborXPos),2);		int iTerm2 = (int) pow((targetYPos-iNeighborYPos),2);		int iTerm3 = (int) pow(iRadius,2);//		printf("\n\t Term1: %d, Term2: %d, Term3: %d", iTerm1, iTerm2, iTerm3);		if ( (pow((targetXPos-iNeighborXPos),2) + pow((targetYPos-iNeighborYPos),2)) > pow(iRadius,2) )			continue;//		printf("\n\t Adding to the main neighbour list....");		// Make a copy so that we can add it in the pReachableNodes...By Default the add method of the cArray class		// stores only the pointer and does not really copy...		NodeInfo *pNeighborToAdd = new NodeInfo(pNeighbor);		pReachableNodes->add(pNeighborToAdd);		// Do NOT Delete the pNeighborToAdd pointer...The cArray by default takes the ownership of the pointers		// that are stored in it and will delete it when it goes out of scope.	}	return 1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -