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

📄 sink.cpp

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

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

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CDraw Draw;
extern CMsgCenter MsgCenter;
/////////////////////////////////////////////////////////////////////////////
// CSink

IMPLEMENT_DYNCREATE(CSink, CWinThread)

BOOL CSink::SinkEnd = FALSE;

CSink::CSink()
{
   //pHead = NULL;
   //pTail = NULL;
   InitializeCriticalSection(&Locked);
   SinkId=-1;
}

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

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

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

BEGIN_MESSAGE_MAP(CSink, CWinThread)
	//{{AFX_MSG_MAP(CSink)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSink message handlers

UINT CSink::SinkWorkProc(LPVOID pParam)
{ 
    CSink *pSink = (CSink *)pParam;
    if (pSink == NULL || !pSink->IsKindOf(RUNTIME_CLASS(CSink)))  return 1;   // if pObject is not valid
 
	while ( 1 ) {
        if ( CSink::SinkEnd ) break;
		CMsg *pMsg = pSink->MsgBuff.GetMsg();
	    pSink->DesposeMessage(pMsg);
		pSink->Action();  
		Sleep(200);
    }

	MsgCenter.DeleteSinkStrain(pSink);
	DELETE_OBJECT(pSink);

	for (int i=0;i<=BONDERY;i++)
		for (int j=0;j<=8;j++) DeleteCriticalSection(&(MsgCenter.Locked[i][j]));

	DYNAMIC_2D_ARRAY_DELETE(MsgCenter.pNodeStrain,BONDERY + 1,9);
    DYNAMIC_2D_ARRAY_DELETE(MsgCenter.Locked,BONDERY + 1,9);
	CSimSensorView *pView = (CSimSensorView *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveView();
	MessageBox(pView->m_hWnd,"所有传感器都已结束工作了!","提示",MB_OK);
	g_NodeSet = false;
    return 0;   // thread completed successfully
}

//DEL CMsg * CSink::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 	return  pMsg;
//DEL }

//DEL void CSink::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 = pTail->pNext;
//DEL 	}else {
//DEL 		pHead = pMsgStrain;
//DEL 		pTail = pMsgStrain;
//DEL 	}
//DEL 
//DEL 	LeaveCriticalSection(&Locked);
//DEL }

void CSink::SetNodeXPos()
{

	for (int i=1;i<=BONDERY;i++) {
		CDataSinkToNode *Data = new CDataSinkToNode;
		Data->Coordinates.ValueX.X = i;
		Data->Coordinates.ValueX.Distance = (float)i * RADIS; 
        Data->Coordinates.ValueX.DeadLine = 0;//g_NodeEnergy - g_NodeEnergy * (float)(i-1) / BONDERY;
		MsgCenter.SendMsg(-1,-1,0,0,2,(unsigned char *)Data);  
	}
	
}

void CSink::SetNodeYPos()
{
	for (int i=1;i<=8;i++) {
		CDataSinkToNode *Data = new CDataSinkToNode;
		Data->Coordinates.ValueY.Y = i;
		Data->Coordinates.ValueY.Angle = i * ( PAI/4 );	
		MsgCenter.SendMsg(-1,-1,0,0,3,(unsigned char *)Data);	
	}
}

void CSink::DesposeMessage(CMsg *pMsg)
{
	if ( pMsg == NULL) return;
    Draw.DrawListView (SinkId,RECEIVE,(LPVOID *)pMsg);
	switch ( pMsg->Type ) {
	case 0:
		//TRACE("Sink Received ");
	    //TRACEMSG(pMsg); 
		//CRuntimeClass(CDispMsgView)
		//DELETE_OBJECT(pMsg);
        //pDoc->MsgBuff.AddMsg(pMsg);
        
		DELETE_OBJECT(pMsg);

		//pView->GetDC()->TextOut(0,0,"hehe");
		break;
		/*case 1: 
		delete pMsg;
		break;
		case 2: 	 
		TRACE( "received one data from (%d,%d)\n", pMsg->Origination.x,pMsg->Origination.y );
		delete pMsg;
		break;
		/*case 3:// 暂停Node节点采集数据
		delete pMsg;
		CNode::StopCollectData = false;
		break;
		case 4:// 启动Node节点采集数据
		delete pMsg;
		CNode::StopCollectData = true;
		break;
		/*	case 5:;//信息是Sink发出,告知Node采集数据回传给Sink,Data数据无意义。
		break;
		case 6:;//信息是Node发出,告知Node自己已退出数据转发链,Data数据无意义 
		break;*/
	case 50:
		DELETE_OBJECT(pMsg);
		SetNodeXPos();
		SetNodeYPos();
		MsgCenter.SendMsg(-1,-1,0,0,4);
		//TRACE("Sink Send Msg:4\n");
		break;
	default:
		DELETE_OBJECT(pMsg);
	}
}

void CSink::Action()
{
	/*if (g_NodeSet) {
		CSimSensorView *pView = (CSimSensorView *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveView();
		CDC *pDC = pView->GetDC();
		Draw.DrawCoordinates(pDC);
		Draw.DrawAllNodes(pDC); 
		pView->ReleaseDC(pDC); 
		//MsgCenter.PrintAreaNode(0,1);
	}*/

	if ( g_NodeSet ) {
		EnterCriticalSection(&(MsgCenter.Locked[0][1]));
		CNodeStrain *pStrain = MsgCenter.pNodeStrain[0][1];
		LeaveCriticalSection(&(MsgCenter.Locked[0][1]));
		if (pStrain==NULL) {
			while ( !MsgBuff.IsEmpty() ) {
				CMsg *pMsg = MsgBuff.GetMsg();
                Draw.DrawListView (SinkId,RECEIVE,(LPVOID *)pMsg);
			}
			Draw.DrawListView (SinkId,DEAD,NULL);
			SinkEnd = true;  
		}

	}
}

//DEL void CSink::DumpMsg()
//DEL {
//DEL 	EnterCriticalSection(&Locked);
//DEL 
//DEL     CMsg *pMsg;
//DEL 	while ((pMsg = GetMsg())!=NULL) DELETE_OBJECT(pMsg);
//DEL 
//DEL 	LeaveCriticalSection(&Locked);
//DEL 
//DEL }

⌨️ 快捷键说明

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