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

📄 node.cpp

📁 实现了无线传感器网络中的一个路由协议算法。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	
	//TRACE( "Node %d send Power data,remain energy:%f\n",  NodeId, RemainEnergy );
    //Draw.DrawListView (NodeId,SEND,(LPVOID *)pMsg);
	MsgCenter.SendMsg(pMsg,NodeId);
}

void CNode::SendRelayData(CMsg *pMsg)
{  
	if ( RelayFlag==TRUE ) {
		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;
	}
	
	//TRACE( "Node %d send Relay data,remain energy:%f\n",  NodeId, RemainEnergy );
	//Draw.DrawListView (NodeId,RELAY,(LPVOID *)pMsg);
	MsgCenter.SendMsg(pMsg,NodeId);
}



void CNode::DesposeMessage(CMsg *pMsg)
{
    if ( pMsg == NULL) return;
    //TRACEMSG(pMsg);
	//Draw.DrawListView (NodeId,RECEIVE,(LPVOID *)pMsg);

	switch ( Status ) {
	case -1://节点尚未初始化
		switch (pMsg->Type) {
		case 2: //信息是Sink发出,告知Node的带状环号,Data为带状环号。
			SetxPos(( CDataSinkToNode * )(pMsg->Data) );
			DELETE_OBJECT(pMsg);
			break;
		case 3://信息是Sink发出,告知Node的楔型区号,Data为楔型区号。
			SetyPos(( CDataSinkToNode * )(pMsg->Data) );
			DELETE_OBJECT(pMsg);
			break;
		case 4://信息是Sink发出,告知Node进入争权状态,Data数据无意义。
            DELETE_OBJECT(pMsg);

			EnterCriticalSection(&Locked);
			Status = 2;
			LeaveCriticalSection(&Locked);

			//while( FALSE == CollectDataTimer.SetMyTimer(5000,CONTINUE) );
			//TRACE("Node %d Create CollectDataTimer OK!\n",NodeId);
			CollectDataTimer.SetMyTimer(5000,CONTINUE);
			SendPowerData();
			GetPowerTimer.SetMyTimer(7000,ONCE);
			//while( FALSE == GetPowerTimer.SetMyTimer(5000,ONCE) );
			//TRACE("Node %d Create GetPowerTimer OK!\n",NodeId);
			break;
		default:
			MsgBuff.AddMsg(pMsg);
		}
		break;
		case 0: //节点处于正常工作状态 
			switch ( pMsg->Type ) {
			case 4://信息是Node发出,告知Node进入争权状态,Data数据无意义。	
				if ( !QuitRelayStrain ) {
					EnterCriticalSection(&Locked);
					Status = 2;
					LeaveCriticalSection(&Locked);
					Draw.DrawListView (NodeId,RECEIVE,(LPVOID *)pMsg);
					SendPowerData();
					//GetPowerTimer.ContinueTimer();
					GetPowerTimer.SetMyTimer(7000,ONCE);
				}
				DELETE_OBJECT(pMsg);
				break;
			case 5://信息是Sink发出,告知Node采集数据回传给Sink,Data数据无意义。
				DELETE_OBJECT(pMsg);
				SendCollectData(COMMAND);
				break;
			case 6: //信息是Node发出,告知Node自己已退出数据转发链,Data数据无意义
				if (pMsg->Origination == Location ) 
					QuitRelayStrain = TRUE;
				else
					RelayFlag = FALSE;
				DELETE_OBJECT(pMsg);
				break;
				//********************************************************
				//定时器时间到达处理
			case 100:                 //争权时间到
				DELETE_OBJECT(pMsg);
				break;
			case 101:
				DELETE_OBJECT(pMsg);
				SendCollectData();    //定时采集时间到
				break;
				//********************************************************
			default:
				DELETE_OBJECT(pMsg);
			}
			break;
			case 1://节点处于转发数据状态
				switch (pMsg->Type) {
				case 0:
					SendRelayData(pMsg); 
					break;
				case 1:
					ComparePower(pMsg);
					//TRACE("Node %d comparePower data!,remain energy:%f\n",NodeId,RemainEnergy);	
					DELETE_OBJECT(pMsg);
					break;
				case 4://信息是Node发出,告知Node进入争权状态,Data数据无意义。
					//TRACEMSG(pMsg);
					DELETE_OBJECT(pMsg);
					if ( !QuitRelayStrain ) {
						EnterCriticalSection(&Locked);
						Status = 2;
						LeaveCriticalSection(&Locked);
						
						SendPowerData();
						//GetPowerTimer.ContinueTimer();
						GetPowerTimer.SetMyTimer(7000,ONCE);
					}
					break;
				case 5://信息是Sink发出,告知Node采集数据回传给Sink,Data数据无意义。
					DELETE_OBJECT(pMsg);
					SendCollectData(COMMAND);
					break;
				case 6: //信息是Node发出,告知Node自己已退出数据转发链,Data数据无意义
					if (pMsg->Origination == Location )
						QuitRelayStrain = TRUE;
					else {
						RelayFlag = FALSE;
					}
					DELETE_OBJECT(pMsg);
					break;
					//********************************************************
				case 101:
					DELETE_OBJECT(pMsg);
					SendCollectData();    //定时采集时间到
					break;
					//********************************************************
				default:
					DELETE_OBJECT(pMsg);
				}
				break;
				case 2://节点处于争权状态
					switch (pMsg->Type) {
					case 0:
						//TRACE("Node %d Received:",NodeId);
						//TRACEMSG(pMsg);
						MsgBuff.AddMsg (pMsg);
						break;
					case 1:
						ComparePower(pMsg);
						//TRACE("Node %d comparePower data!,remain energy:%f\n",NodeId,RemainEnergy);
						DELETE_OBJECT(pMsg);
						break;
					case 4://信息是Node发出,告知Node进入争权状态,Data数据无意义。
						DELETE_OBJECT(pMsg);
						break;
						//********************************************************
						//定时器时间到达处理
					case 100:                 //争权时间到
						DELETE_OBJECT(pMsg);
						//TRACE( "Node %d has get power msg!\n",NodeId);
						
						EnterCriticalSection(&Locked);
						
						Status = 1; 							
						if ( RemainEnergy < DeadLine ) {//DeadLine ) {
 							MsgCenter.SendMsg (Location.x + 1,Location.y,Location.x,Location.y,6,NULL,NodeId );
							MsgCenter.SendMsg (Location.x,Location.y,Location.x,Location.y,6,NULL,NodeId );
							Status = 0;
							QuitRelayStrain = TRUE;
							//TRACE( "Area(%d,%d) has no transdata power!\n", Location.x,Location.y  );
						}else{ 
                            ChangePowerLine = RemainEnergy * 3 / 5;
						}
						
						LeaveCriticalSection(&Locked);
						break;
					case 101:
						DELETE_OBJECT(pMsg);
						//SendCollectData();    //定时采集时间到
						break;
						//********************************************************
					default:
						MsgBuff.AddMsg (pMsg);
					}
					default:;//default status
		}//end switch
}

void CNode::Action()
{
	EnterCriticalSection(&Locked);
	
	if ( QuitRelayStrain ) {
		if (Status!=0) Status = 0;
	}else {
		if ( Status ==1 ) {
			if ( (RemainEnergy <  ChangePowerLine) ) {	
				Status = 2;
				MsgCenter.SendMsg (Location.x,Location.y,Location.x,Location.y,4,NULL,NodeId);
				SendPowerData();
				GetPowerTimer.SetMyTimer(7000,ONCE);
				//TRACE( "Area(%d,%d) has start to get transdata power!\n",  Location.x, Location.y  );
			}
		}
	}

	//if ( Status > 0 ) 
	//	TRACE( "Node:%d,Status:%d,Energy:%f\n",NodeId,Status,RemainEnergy );

	LeaveCriticalSection(&Locked);
	
}




//DEL void CNode::Draw()
//DEL {
//DEL     CSimSensorView *pView = (CSimSensorView *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveView();
//DEL  	CDC *pDC = pView->GetDC();
//DEL     CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
//DEL 	CBrush *pOldBrush=pDC->SelectObject(pBrush);
//DEL     CPoint x,y,ptReDrawX,ptReDrawY;
//DEL 	CString Str;
//DEL 	double NodeCenterX,NodeCenterY;
//DEL 
//DEL 	CPen Pen(0,0,RGB(255,0,0));
//DEL 	CPen *pOldPen;
//DEL 	Str.Format("%d",(int)RemainEnergy);
//DEL 	NodeCenterX = g_ViewCenterX + RATOS*Distance*cos(Angle);
//DEL 	NodeCenterY = g_ViewCenterY + RATOS*Distance*sin(Angle);
//DEL 
//DEL 	x.x = (long)(NodeCenterX - 4); 
//DEL 	x.y = (long)(NodeCenterY - 4);
//DEL 	y.x = (long)(NodeCenterX + 4);
//DEL 	y.y = (long)(NodeCenterY + 4);
//DEL 	
//DEL 	if ( Status == 1 ) 
//DEL 	    pOldPen = pDC->SelectObject(&Pen);			
//DEL  
//DEL 	pView->InvalidateRect(CRect(x,y));
//DEL 	pDC->Ellipse(CRect(x,y));
//DEL 
//DEL 	CFont font;
//DEL 	font.CreatePointFont(70,"宋体",pDC);
//DEL 	CFont *pOldFont=pDC->SelectObject(&font);
//DEL     
//DEL     ptReDrawX.x = x.x + 9;
//DEL 	ptReDrawX.y = x.y;
//DEL 	ptReDrawY.x = y.x + 12;
//DEL 	ptReDrawY.y = y.y;
//DEL 
//DEL     pView->InvalidateRect(CRect(ptReDrawX,ptReDrawY));
//DEL 	pDC->TextOut(x.x+9,x.y,Str);
//DEL 
//DEL 	if ( Status == 1 ) 
//DEL 	    pDC->SelectObject(pOldPen);
//DEL     pDC->SelectObject(pOldFont);
//DEL 	pDC->SelectObject(pOldBrush);
//DEL }


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

BOOL CNode::Dead()
{
	EnterCriticalSection(&Locked);
    if ( RemainEnergy <= 0 ) {//当节点发送数据前有能量,发送后能量就耗尽,此时应通知区域其他节点挣权
		if ( Status != 0 ) {
			MsgCenter.SendMsg (Location.x,Location.y,Location.x,Location.y,4,NULL,NodeId);
			MsgCenter.SendMsg (Location.x+1,Location.y,Location.x,Location.y,6,NULL,NodeId);		
		}
		LeaveCriticalSection(&Locked);
		return TRUE;	
	}
	LeaveCriticalSection(&Locked);
	return FALSE;	
}

⌨️ 快捷键说明

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