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

📄 msgcenter.cpp

📁 实现了无线传感器网络中的一个路由协议算法。
💻 CPP
字号:
// MsgCenter.cpp: implementation of the CMsgCenter class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SimSensor.h"
#include "MainFrm.h"
#include "SimSensorView.h"
#include "MsgCenter.h"
#include "Sink.h"

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

extern CMsgCenter MsgCenter;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
long Line=0,Width=0;
CMsgCenter::CMsgCenter()
{
   pNodeStrain=NULL;
   Locked = NULL;
}

CMsgCenter::~CMsgCenter()
{

}


void CMsgCenter::SendMsg(int Dx, int Dy, int Sx, int Sy, int Type, unsigned char *Data,int NodeId)
{

	CMsg *pMsg = new CMsg;
	pMsg->Destination.x = Dx;
	pMsg->Destination.y = Dy;
	pMsg->Origination.x = Sx;
	pMsg->Origination.y = Sy;
	pMsg->Type = Type;
	pMsg->Data =Data;

	SendMsg(pMsg,NodeId);
}

void CMsgCenter::SendMsg(CMsg *pMsg,int NodeId)
{
    CNodeStrain *pStrain;

    if ( (pMsg->Destination.x==-1) && (pMsg->Destination.y==-1) ) {
		 EnterCriticalSection(&(MsgCenter.Locked[0][1]));
		 pStrain = pNodeStrain[0][1];
	}else {
		if ( pMsg->Destination.x==0 ) {
			pMsg->Destination.y = 0;
            EnterCriticalSection(&(MsgCenter.Locked[0][0]));
			pStrain = pNodeStrain[0][0];           
		}else {
			if ( pMsg->Destination.x > BONDERY ) {
			    TRACE("Node is out of bondary\n");
				DELETE_OBJECT(pMsg);
				return;
			}
            EnterCriticalSection(&(MsgCenter.Locked[pMsg->Destination.x][pMsg->Destination.y]));
			pStrain = pNodeStrain[pMsg->Destination.x][pMsg->Destination.y];
		}
	}

	while ( pStrain!=NULL ) {
        if ( pMsg->Destination.x==0 ) { 
			if ( pStrain->pNode!=NULL ) {
				if ( NodeId != ((CSink *)(pStrain->pNode))->SinkId ) {
					CMsg *pNewMsg = new CMsg;
					pNewMsg->Destination = pMsg->Destination;
					pNewMsg->Origination = pMsg->Origination;
					pNewMsg->Type = pMsg->Type;
					pNewMsg->Data = pMsg->Data;
					((CSink *)(pStrain->pNode))->MsgBuff.AddMsg(pNewMsg);		
				}
			}
		}else {
			if ( pStrain->pNode!=NULL ) {
				if ( NodeId != pStrain->pNode->NodeId ) {
					CMsg *pNewMsg = new CMsg;
					pNewMsg->Destination = pMsg->Destination;
					pNewMsg->Origination = pMsg->Origination;
					pNewMsg->Type = pMsg->Type;
					pNewMsg->Data = pMsg->Data;			
					pStrain->pNode->MsgBuff.AddMsg(pNewMsg);
				}
			}
		}
		pStrain = pStrain->pNext;
		
	}

	if ( (pMsg->Destination.x==-1) && (pMsg->Destination.y==-1) ) 
		LeaveCriticalSection(&(MsgCenter.Locked[0][1]));
	else 
		LeaveCriticalSection(&(MsgCenter.Locked[pMsg->Destination.x][pMsg->Destination.y]));

	DELETE_OBJECT(pMsg);
}

void CMsgCenter::InsertNodeStrain(CNode *pNode,int Ax,int Ay)
{
	CNodeStrain *pNewStrain = new CNodeStrain;
	pNewStrain->pNode = pNode;

	EnterCriticalSection(&(MsgCenter.Locked[Ax][Ay]));

	pNewStrain->pNext = pNodeStrain[Ax][Ay]; 
	pNodeStrain[Ax][Ay] = pNewStrain;

	LeaveCriticalSection(&(MsgCenter.Locked[Ax][Ay]));

}

void CMsgCenter::InsertNodeStrain(CNode *pNode, CPosition Area)
{
    InsertNodeStrain(pNode,Area.x,Area.y);
}

void CMsgCenter::DeleteSinkStrain(CSink *pSink)
{
	CNodeStrain *pStrain,*pFatherStrain=NULL,*pSonStrain=NULL;
  	EnterCriticalSection(&(MsgCenter.Locked[0][0]));

    pStrain = pNodeStrain[0][0];
    if ( pStrain!= NULL){
		if (pStrain->pNode == (CNode *)pSink ) {
			pNodeStrain[0][0] = pStrain->pNext;
			//TRACE("Sink:%d Area strain delete!\n",((CSink *)(pStrain->pNode))->SinkId );
			DELETE_OBJECT(pStrain);
		}else {
			pFatherStrain = pStrain;
			pSonStrain = pFatherStrain->pNext;
			while ( pSonStrain!=NULL ) {
				if ( pSonStrain->pNode == (CNode *)pSink ) break;
				pFatherStrain = pSonStrain; 
				pSonStrain = pSonStrain->pNext; 
			} 
			
			if ( pSonStrain!=NULL ) {
				pFatherStrain->pNext = pSonStrain->pNext;
				//TRACE("Sink:%d Area strain delete!\n",((CSink *)(pSonStrain->pNode))->SinkId );
				DELETE_OBJECT(pSonStrain);
			}
		}
	}
 
    LeaveCriticalSection(&(MsgCenter.Locked[0][0]));

}

void CMsgCenter::DeleteNodeStrain(CNode *pNode)
{
    CNodeStrain *pStrain,*pFatherStrain=NULL,*pSonStrain=NULL;

	EnterCriticalSection(&(MsgCenter.Locked[0][1]));
	EnterCriticalSection(&(MsgCenter.Locked[pNode->Location.x][pNode->Location.y]));

    pStrain = pNodeStrain[pNode->Location.x][pNode->Location.y];
	if ( pStrain!= NULL){
		if (pStrain->pNode == pNode) {
			pNodeStrain[pNode->Location.x][pNode->Location.y] = pStrain->pNext;
			//TRACE("Node:%d Area strain delete!\n",pStrain->pNode->NodeId);
			DELETE_OBJECT(pStrain);
		}else {
			pFatherStrain = pStrain;
			pSonStrain = pFatherStrain->pNext;
			while ( pSonStrain!=NULL ) {
				if ( pSonStrain->pNode == pNode ) break;
				pFatherStrain = pSonStrain; 
				pSonStrain = pSonStrain->pNext; 
			} 
			
			if ( pSonStrain!=NULL ) {
				pFatherStrain->pNext = pSonStrain->pNext;
				//TRACE("Node:%d Area strain delete!\n",pSonStrain->pNode->NodeId);
				DELETE_OBJECT(pSonStrain);
			}
		}
	}
	
	pFatherStrain=pSonStrain=NULL;
    pStrain = pNodeStrain[0][1];
	if ( pStrain!= NULL){
		if (pStrain->pNode == pNode) {
			pNodeStrain[0][1] = pStrain->pNext;
			//TRACE("Node:%d All strain delete!\n",pStrain->pNode->NodeId);
			DELETE_OBJECT(pStrain);
		}else {
			pFatherStrain = pStrain;
			pSonStrain = pFatherStrain->pNext;
			while ( pSonStrain!=NULL ) {
				if ( pSonStrain->pNode == pNode ) break;
				pFatherStrain = pSonStrain; 
				pSonStrain = pSonStrain->pNext; 
			} 
			if ( pSonStrain!=NULL ) {
				pFatherStrain->pNext = pSonStrain->pNext;
				//TRACE("Node:%d All strain delete!\n",pSonStrain->pNode->NodeId);
				DELETE_OBJECT(pSonStrain); 
			}
		}
	}

	LeaveCriticalSection(&(MsgCenter.Locked[pNode->Location.x][pNode->Location.y]));
	LeaveCriticalSection(&(MsgCenter.Locked[0][1]));
}

void CMsgCenter::PrintAreaNode(int Ax, int Ay)
{
	CNodeStrain *pStrain;

	EnterCriticalSection(&(MsgCenter.Locked[Ax][Ay]));
	pStrain = pNodeStrain[Ax][Ay];

	CSimSensorView *pView=(CSimSensorView *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveView();
	CDC *pDC=pView->GetDC();

    CString Str;
	TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);
	Width=0;

	if (pStrain!=NULL) {
        Str.Format ("(%d,%d):",Ax,Ay);
		pDC->TextOut (tm.tmMaxCharWidth*Width,tm.tmHeight*Line,Str);
		Width += Str.GetLength ();
	}
 
    bool Enter=false;
	while ( pStrain!=NULL ) {
		if (Ax==0 && Ay==0) {
			Str.Format("%d ",((CSink *)(pStrain->pNode))->SinkId );
            pDC->TextOut(tm.tmMaxCharWidth*Width,tm.tmHeight*Line,Str);
			Width += Str.GetLength ();
		}else {
			Str.Format("%d ",pStrain->pNode->NodeId);
			pDC->TextOut(tm.tmMaxCharWidth*Width,tm.tmHeight*Line,Str);
			Width += Str.GetLength ();
		}
		pStrain = pStrain->pNext ;
        Enter=true;
	}
	if (Enter==true) Line++;
	LeaveCriticalSection(&(MsgCenter.Locked[Ax][Ay]));
	pView->ReleaseDC (pDC);

}



⌨️ 快捷键说明

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