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

📄 application.cc

📁 拓扑查找算法的omnet仿真实现
💻 CC
字号:
//-------------------------------------------------------------------//  file name: application.cpp// //    - contains the implementation of application class////-------------------------------------------------------------------#include "application.h"// some constant definitions#define REPLY_DELAY 0.1// message definitions#define SEND_SELF_STARTUP(delay)  {				\	cMessage *msg = new cMessage("StartUp");	\	msg->setKind(M_SELF);						\	scheduleAt(simTime()+delay,msg);			\}#define SEND_TOPOLOGY_REQ_NEW	{						\	cMessage *msg = new cMessage("TopologyRequest");	\	msg->setKind(M_HIGHLOW);							\	msg->addPar("lasthop") = ID;						\	msg->addPar("hops") = 0;							\	send(msg,"lowergate_out");\	UPDATECOLOR(3);		\}#define SEND_TOPOLOGY_REQ		{							\	cMessage *msg = new cMessage("TopologyRequest");		\	msg->setKind(M_HIGHLOW);								\	msg->addPar("lasthop") = ID;							\	msg->addPar("hops") = nhops;							\	send(msg,"lowergate_out");								\	UPDATECOLOR(3); \}#define SEND_TOPOLOGY_REPLY_NEW	{						\	cMessage *msg = new cMessage("TopologyReply");		\	msg->setKind(M_HIGHLOW);							\	InitDataStruct();									\	msg->addPar("datastruct") = (void *)data;			\	msg->addPar("hops") = nhops;						\	send(msg,"lowergate_out");							\	UPDATECOLOR(5);\}#define SET_SELF_TIMER_REPLY	{					\	cMessage *msg = new cMessage("ReplyTimer");		\	msg->setKind(M_SELF);							\	msgtimerreply = msg;							\	scheduleAt(simTime()+REPLY_DELAY,msg);			\}void application::initialize(){	seen_req = false;	nbrlist_nm1.clear();	nbrlist_n.clear();	nbrlist_np1.clear();	int nhops = -1;	seen_req = false;	msgtimerreply = NULL;	for (int i=0;i<NNODES*NNODES;i++)			data[i] = false;	if (ID==0)		// node 0 starts the algorithm	{		SEND_SELF_STARTUP(10);		seen_req = true;		nhops = 0;	}}void application::finish(){	if (ID==0)	{		// print the topology		for (int cnt1=0;cnt1<NNODES ;cnt1++)		{			bool first = true;			for (int cnt2=cnt1;cnt2<NNODES;cnt2++)			{				if (data[cnt1*NNODES+cnt2])				{					if (first)					{						ev << cnt1 << " : ";						first = false;					}					ev << cnt2 << " ";				}			}			if (!first)				ev << "\n";		}	}}void application::handleMessage(cMessage *msg){	switch(msg->kind())	{	case M_LOWHIGH:		{			if (strcmp(msg->name(),"TopologyRequest")==0)			{				processTopologyRequest(msg);				break;			}			if (strcmp(msg->name(),"TopologyReply")==0)			{				processTopologyReply(msg);				break;			}			ev << "unknown message received from another node.\n";			endSimulation();		}	case M_SELF:		{						if (strcmp(msg->name(),"StartUp")==0)			{				SEND_TOPOLOGY_REQ_NEW;				delete msg;				break;			}			if (strcmp(msg->name(),"ReplyTimer")==0)			{				// this guy should send back the collected data				SEND_TOPOLOGY_REPLY_NEW;				//ev << "node #" << ID << " is network end.\n";				delete msg;				break;			}			ev << "unknown self message received\n";			endSimulation();		}	default:		ev << "unknown message received\n";		endSimulation();	}}void application::processTopologyRequest(cMessage *msg){	// have we seen this message before ?	if (seen_req)		// already seen it	{		// collect info		if ((int)msg->par("hops")<nhops)			nbrlist_nm1.push_back((int)msg->par("lasthop"));		if ((int)msg->par("hops")==nhops)			nbrlist_n.push_back((int)msg->par("lasthop"));		if ((int)msg->par("hops")>nhops)		{			nbrlist_np1.push_back((int)msg->par("lasthop"));			// cancel also the reply timer if set			if (msgtimerreply!=NULL)			{				delete cancelEvent(msgtimerreply);				msgtimerreply = NULL;			}		}		delete msg;		return;	}	else	{		seen_req = true;		nhops = (int)msg->par("hops") + 1;		nbrlist_nm1.push_back((int)msg->par("lasthop"));				// forward topology request		SEND_TOPOLOGY_REQ;		// setup timer for route reply		SET_SELF_TIMER_REPLY;	}}void application::processTopologyReply(cMessage *msg){	// are we the source?	if (ID==0)	{		UpdateDataStruct(msg);				delete msg;		return;	}	// we are not the data source	if ((int)msg->par("hops")>nhops)		// forward it	{		msg->par("datastruct") = (void *)AddDataStruct(msg);		msg->par("hops") = nhops;		msg->setKind(M_HIGHLOW);		send(msg,"lowergate_out");		return;	}	else	{		delete msg;		return;	}}void application::InitDataStruct(){	int n = 0,pos = 0;	std::list<int>::iterator i;	for (int cnt=0;cnt<NNODES*NNODES;cnt++)		data[cnt] = false;		// data struct	for (i=nbrlist_nm1.begin();i!=nbrlist_nm1.end();i++)	{		data[NNODES * (*i) + ID] = true;		data[NNODES * ID + (*i)] = true;	}		for (i=nbrlist_n.begin();i!=nbrlist_n.end();i++)	{		data[NNODES * (*i) + ID] = true;		data[NNODES * ID + (*i)] = true;	}}void application::UpdateDataStruct(cMessage *msg){	// just add the two pieces of information	bool *tmp = (bool *)(void *)msg->par("datastruct");	for (int cnt=0;cnt<NNODES*NNODES;cnt++)		data[cnt] = data[cnt] | tmp[cnt];}void * application::AddDataStruct(cMessage *msg){	bool *res = new bool[NNODES * NNODES];	bool *tmp = (bool *)(void *)msg->par("datastruct");	InitDataStruct();	for (int cnt=0;cnt<NNODES*NNODES;cnt++)		res[cnt] = data[cnt] | tmp[cnt];	return (void *)res;}

⌨️ 快捷键说明

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