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

📄 node.cpp

📁 实现了无线传感器网络中的一个路由协议算法。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Node.cpp : implementation file
//

#include "stdafx.h"
#include "Math.h"
#include "MainFrm.h"
#include "SimSensorDoc.h"
#include "SimSensorView.h"
#include "SimSensor.h"
#include "Node.h"
#include "Draw.h"
#include "MsgCenter.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CDraw Draw;
extern CMsgCenter MsgCenter;

/////////////////////////////////////////////////////////////////////////////
// CNode

IMPLEMENT_DYNCREATE(CNode, CWinThread)

BOOL CNode::AllStopCollectData = FALSE;
BOOL CNode::NodeEnd = FALSE;
CNode::CNode()
{
   Location.x=-1;
   Location.y=-1;
   RemainEnergy=50;
   DeadLine = 20;
   ChangePowerLine=RemainEnergy * 0.6;
   Status=-1;
   DistanceRandomNum=-1;
   Radis=RADIS;
   RelayFlag=TRUE;

   //pHead = NULL;
   //pTail = NULL;
   Distance=-1;
   Angle = -1;
   NodeId = -1;
   QuitRelayStrain = FALSE;
       
   CollectDataTimer.BandNode((LPVOID)this); 
   GetPowerTimer.BandNode((LPVOID)this); 
   
   InitializeCriticalSection(&Locked);
}

CNode::CNode(double Energy):RemainEnergy(Energy)
{  
   Location.x=-1;
   Location.y=-1;
   //RemainEnergy=50;
   DeadLine = 20;
   ChangePowerLine=RemainEnergy * 0.6;
   Status=-1;
   DistanceRandomNum=-1;
   Radis=RADIS;
   RelayFlag=TRUE;

   //pHead = NULL;
   //pTail = NULL;
   Distance=-1;
   Angle = -1;
   NodeId = -1;
   QuitRelayStrain = FALSE;
    
   CollectDataTimer.BandNode((LPVOID)this); 
   GetPowerTimer.BandNode((LPVOID)this); 
   
   InitializeCriticalSection(&Locked);
}

CNode::~CNode()
{
	//DumpMsg();
	DeleteCriticalSection(&Locked);
}

BOOL CNode::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CNode::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CNode, CWinThread)
	//{{AFX_MSG_MAP(CNode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNode message handlers

UINT CNode::NodeWorkProc(LPVOID pParam)
{
    
    CNode *pNode = (CNode *)pParam;

    if (pNode == NULL || !pNode->IsKindOf(RUNTIME_CLASS(CNode)))  return 1;   // if pObject is not valid
 
	while ( 1 ) {
	    if ( pNode->Dead() ) break;
		if ( CNode::NodeEnd ) break;
	    CMsg *pMsg = pNode->MsgBuff.GetMsg ();
	    pNode->DesposeMessage(pMsg);
		pNode->Action();
		Sleep(200);
	}
    Draw.DrawListView (pNode->NodeId,DEAD,NULL);
    MsgCenter.DeleteNodeStrain(pNode);
    while(!pNode->CollectDataTimer.EndTimer()) Sleep(10);
	while(!pNode->GetPowerTimer.EndTimer()) Sleep(10); 
    DELETE_OBJECT(pNode);

	//TRACE( "Node %d dead\n", pNode->NodeId);	
    return 0;   // thread completed successfully
}

void CNode::SetxPos(CDataSinkToNode *Data)
{
	if ( Data->Coordinates.ValueX.Distance > Distance ) { 
		if ( Location.x == -1 ) { 
			Location.x = Data->Coordinates.ValueX.X;
			Radis = Data->Coordinates.ValueX.Distance;
			DeadLine = Data->Coordinates.ValueX.DeadLine;
			//[a, b]
			//rand() % (b-a+1) + a
			//srand( (unsigned)time( NULL ) );

			srand((unsigned)NodeId);
			DistanceRandomNum = rand();

            if ( (Location.x>0) && (Location.y>0) ) 
				MsgCenter.InsertNodeStrain( this,Location );
		}
	}
					
}

void CNode::SetyPos(CDataSinkToNode *Data)
{
	if ( Data->Coordinates.ValueY.Angle > Angle ) { 
		if ( Location.y == -1 ) { 
			Location.y = Data->Coordinates.ValueY.Y;
			if ( (Location.x>0) && (Location.y>0) )
				MsgCenter.InsertNodeStrain( this,Location );
		}
	}
	
}

//DEL CMsg * CNode::GetMsg()
//DEL {
//DEL 	EnterCriticalSection(&Locked);
//DEL 
//DEL 	if ( pHead == NULL) {
//DEL 	    LeaveCriticalSection(&Locked);		
//DEL 		return NULL;
//DEL 	}
//DEL 
//DEL 	CMsgStrain *pMsgStrain = pHead;
//DEL 	pHead = pHead->pNext; 
//DEL 	if ( pHead==NULL ) pTail=NULL;
//DEL     CMsg * pMsg = pMsgStrain->Message;
//DEL 
//DEL     LeaveCriticalSection(&Locked);
//DEL 
//DEL 	delete  pMsgStrain;
//DEL     pMsgStrain = NULL;
//DEL 	return pMsg;
//DEL }


//DEL void CNode::InsertMsg(CMsg *pMsg)
//DEL {	
//DEL 	CMsgStrain *pMsgStrain = new CMsgStrain;
//DEL 	pMsgStrain->Message = pMsg;
//DEL 
//DEL 	EnterCriticalSection(&Locked);
//DEL     
//DEL 	if (pHead!=NULL) {
//DEL 		pMsgStrain->pNext = pHead;
//DEL 		pHead = pMsgStrain;
//DEL 	}else {
//DEL         pHead = pMsgStrain;
//DEL      	pTail = pMsgStrain;	
//DEL 	}
//DEL 
//DEL 	LeaveCriticalSection(&Locked);
//DEL }

//DEL void CNode::AddMsg(CMsg *pMsg)
//DEL {
//DEL 	CMsgStrain *pMsgStrain = new CMsgStrain;
//DEL 	pMsgStrain->Message = pMsg;
//DEL 
//DEL     EnterCriticalSection(&Locked);
//DEL 
//DEL 	if ( pTail!=NULL ) {
//DEL 		pTail->pNext = pMsgStrain;
//DEL 		pTail = pMsgStrain;
//DEL 	}else {
//DEL 		pHead = pMsgStrain;
//DEL 		pTail = pMsgStrain;
//DEL 	}
//DEL 
//DEL 	LeaveCriticalSection(&Locked);
//DEL }

void CNode::ComparePower(CMsg *pMsg)
{
	if ( NULL == pMsg ) return;
	double EnergyDif;
	int DistRandDif,NodeRandDif;
	srand((unsigned)NodeId);

	EnergyDif   = ((CDataGetTransMsgPower *)(pMsg->Data))->Energy - RemainEnergy;
    NodeRandDif = ((CDataGetTransMsgPower *)(pMsg->Data))->RandomNum - rand();
	DistRandDif = ((CDataGetTransMsgPower *)(pMsg->Data))->DistanceRandomNum - DistanceRandomNum ;

    if ( EnergyDif < 0 ) return;//{TRACE( "NodeId %d win,EnergyDif is %d\n",NodeId,EnergyDif );return;}
    else if ( EnergyDif == 0 ) {
		      if ( NodeRandDif < 0 ) return;//{TRACE( "NodeId %d win,NodeRandDif is %d\n",NodeId,NodeRandDif );return;}
			  else if ( NodeRandDif == 0 ) {
				       if ( DistRandDif <= 0 ) return;//{TRACE( "NodeId %d win,DistRandDif is %d\n",NodeId,DistRandDif );return;}
			  }
	}
	EnterCriticalSection(&Locked);
    Status = 0;
	LeaveCriticalSection(&Locked);
	//TRACE( "NodeId %d fault\n",NodeId);
}


void CNode::SendCollectData(BOOL Type)
{
	
	if ( (COMMAND == Type) || (!AllStopCollectData) ) {
		CDataTransToSink *Data = new CDataTransToSink;
		srand((unsigned)NodeId);
		Data->Data = rand();
		
		CMsg *pMsg = new CMsg;
		
		if ( RelayFlag==TRUE ) {
			pMsg->Destination = Location;
			pMsg->Destination.x--;
			if ( pMsg->Destination.x==0 ) pMsg->Destination.y = 0;
			RemainEnergy = RemainEnergy - RADIS*0.1;
		}else {
			pMsg->Destination.x = 0;
			pMsg->Destination.y = 0;
			RemainEnergy = RemainEnergy - Radis *0.1 * Radis*0.1;
		}
		
		pMsg->Origination = Location;
		pMsg->Type = 0;
		pMsg->Data = (unsigned char *)Data;
		//TRACE("Node %d SendCollectData ",NodeId);
		//TRACEMSG(pMsg);
        //Draw.DrawListView (NodeId,SEND,(LPVOID *)pMsg);
		MsgCenter.SendMsg(pMsg,NodeId);
		//TRACE( "Node %d send collect data,remain energy:%f\n",  NodeId, RemainEnergy );
	}
 }

void CNode::SendPowerData()
{	
	CDataGetTransMsgPower *Data = new CDataGetTransMsgPower; 
	srand((unsigned)NodeId);
	Data->Energy = RemainEnergy;
	Data->RandomNum = rand();
	Data->DistanceRandomNum = DistanceRandomNum ;	
	CMsg *pMsg = new CMsg;
	pMsg->Destination = Location;
	pMsg->Origination = Location;
	pMsg->Type = 1;
	pMsg->Data = (unsigned char *)Data;
	//MsgCenter.SendMsg(Location.x,Location.y,Location.x,Location.y,1,(unsigned char *)Data,NodeId);

⌨️ 快捷键说明

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