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

📄 application.cc

📁 WSN仿真一例假定节点0(node[0])周期性广播消息其它节点接收到消息并转发出去
💻 CC
字号:
//-------------------------------------------------------------------//  file name: application.cpp// //    - contains the implementation of application class////-------------------------------------------------------------------#include "application.h"void application::initialize(){	// initialization for this node	last_seen_id = -1;	msgcount = 0;	// node 0 periodically broadcasts messages in the network	// self timer each 10 seconds	if (ID==0)	{		cMessage *msg = new cMessage("selfmessage");		msg->setKind(M_SELF);		scheduleAt(10,msg);	}}void application::finish(){	ev << "Node " << ID << " generated/forwarded: " << msgcount << " messages\n";}void application::handleMessage(cMessage *msg){	switch(msg->kind())	{	case M_LOWHIGH:		{			if (strcmp(msg->name(),"FLOOD")==0)			{				// process the flood message				processFlood(msg);				return;			}			ev << "unknown message received from another node.\n";			endSimulation();		}	case M_SELF:		{			// send a new message in the network			cMessage *msgnew = new cMessage("FLOOD");			msgnew->setKind(M_HIGHLOW);			msgnew->addPar("flood_id") = ++last_seen_id;			send(msgnew,"lowergate_out");						// change the color			UPDATECOLOR(msgcount+2);			// update the message counter			msgcount++;						// set the timer again			scheduleAt(simTime()+GENERATIONDELAY,msg);			break;		}	default:		ev << "unknown message received\n";		endSimulation();	}}void application::processFlood(cMessage *msg){	// are we the source? 	// (the source does not forward flood messages, it only generates them)	if (ID==0)	{		delete msg;		return;	}	// have we seen this message before?	int flood_id = (int)msg->par("flood_id");	if (flood_id <= last_seen_id)	{		// yes, discard this message		delete msg;		return;	}		// no, forward the message after small delay	msg->setKind(M_HIGHLOW);	sendDelayed(msg,RETRANSMITDELAY,"lowergate_out");	// change the color	UPDATECOLOR(flood_id+2);		// update the message counter	msgcount++;	// update the last_seen_id	last_seen_id = flood_id;}

⌨️ 快捷键说明

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