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

📄 application.cpp

📁 topdisc算法的omnet仿真.平台:omnet+vc 6.0
💻 CPP
字号:
//-------------------------------------------------------------------//  file name: application.cpp// //    - contains the implementation of application class////-------------------------------------------------------------------#include "application.h"// some constant definitions// message definitions
#define SEND_BLACK_REQ_NEW		{							\
	cMessage *msg = new cMessage("BlackRequest");			\
	msg->setKind(M_HIGHLOW);								\
	msg->addPar("senderPX")=this->parentModule()->par("PX");\
	msg->addPar("senderPY")=this->parentModule()->par("PY");\
	msg->addPar("senderId")=ID;								\
	msg->addPar("nhops")=0;									\
	msg->addPar("preSenderId")=ID;							\
	send(msg,"lowergate_out");								\
}

#define SEND_GREY_REQ_NEW		{							\
	cMessage *msg = new cMessage("GreyRequest");			\
	msg->addPar("senderPX")=this->parentModule()->par("PX");\
	msg->addPar("senderPY")=this->parentModule()->par("PY");\
	msg->setKind(M_HIGHLOW);								\
	msg->addPar("senderId")=ID;								\
	msg->addPar("nhops")=nhops;								\
	msg->addPar("preSenderId")=preSenderId;					\
	send(msg,"lowergate_out");								\
} 
/*
#define SEND_GREY_REQ_DELAY    	{							\
	cMessage *msg = new cMessage("GreyRequest");			\
	msg->addPar("senderPX")=this->parentModule()->par("PX");\
	msg->addPar("senderPY")=this->parentModule()->par("PY");\
	msg->setKind(M_HIGHLOW);								\
	msg->addPar("senderId")=ID;								\
	msg->addPar("nhops")=nhops;								\
	msg->addPar("preSenderId")=preSenderId;					\
	sendDelayed(msg,time,"lowergate_out");								\
} */void application::initialize(){	isreq=-1;
	for(int i=0;i<NNODES;i++)
		neibor[i]=-1;	if (ID==0)		// node 0 starts the algorithm	{
		isreq=1;
		parentModule()->par("TYPE")=1;
		SEND_BLACK_REQ_NEW;	}}void application::finish(){
	int type=parentModule()->par("TYPE");
	if(type==1)
	{
		for(int i=0;i<NNODES;i++)
			if(neibor[i]!=-1)
				ev<<"node  "<<ID<<"  connect node  "<<i<<"  with node  "<<neibor[i]<<endl;
	}}void application::handleMessage(cMessage *msg){
	nhops=msg->par("nhops");
	preSenderId=msg->par("preSenderId");
	msg->addPar("senderPX")=this->parentModule()->par("PX");
	msg->addPar("senderPY")=this->parentModule()->par("PY");
	switch(msg->kind())	{	case M_LOWHIGH:		{			if(strcmp(msg->name(),"BlackRequest")==0)
			{
				processBlackRequest(msg);
				break;				
			}

			if(strcmp(msg->name(),"GreyRequest")==0)
			{
				processGreyRequest(msg);
				break;
			}
			ev << "unknown message received from another node.\n";			endSimulation();		}
	default:		ev << "unknown message received\n";		endSimulation();	}}
void application::processBlackRequest(cMessage *msg)
{
	if(isreq==1||isreq==0)
	{
		if(nhops==1)
			neibor[preSenderId]=msg->par("senderId");


		delete msg;
		return;
	}

	else if (isreq==-1)
	{
			UPDATECOLOR(3);
			isreq=0;
			parentModule()->par("TYPE")=0;

			double time=(double)msg->par("timeout");
			nhops=nhops+1;

			SEND_GREY_REQ_NEW;
			delete msg;
	}
}

void application::processGreyRequest(cMessage *msg)
{
	if(isreq==-1)
	{
		int a=parentModule()->par("TYPE");                            
		if(a==2)
		{
			nhops+=1;
			UPDATECOLOR(3);
			isreq=0;
			parentModule()->par("TYPE")=0;
			SEND_GREY_REQ_NEW;
			delete msg;
		}
		else 
		{
			if(nhops==1)
				neibor[preSenderId]=msg->par("senderId");

			UPDATECOLOR(7);
			isreq=1;
			parentModule()->par("TYPE")=1;
			SEND_BLACK_REQ_NEW;
			delete msg;
		}
	}

	else 
	{
		delete msg;
		return ;
	}
}

⌨️ 快捷键说明

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