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

📄 application.cpp

📁 OMNET++仿真三色算法的源码,三色算法是无线传感器中一个典型的分簇算法
💻 CPP
字号:
#include "h/application.h"Define_Module(Application);//applicaton sends data to a mobile host chosen//randomly among the numHost device.//the actual address of the chosen host is defined by//the routing module,that can gain the number accessing//the hosts arry in the physic module.void Application::initialize(){	//store parameters for a faster access
	char tmpstr[512];
	FILE* fout = NULL;

	memset(tmpstr, 0x00, sizeof(tmpstr));

	rate = (int) par("rate");	pktSize = (int) par("pktSize");	hostNum = (int) par("hostNum");	active = (int) par("active");	burstInterval = (simtime_t) par("burstInterval");

	id = parentModule()->id()-1;
	candidates.setName("Candidates");
	candidates.takeOwnership(false);
	
	d("active = "<<active);	physic = (Physic*)parentModule()->submodule("physic");
	
	//posX = physic->posX;
	//posY = physic->posY;

	physic->getPos(posX, posY);

	//initialize the work variables	pktNum = 0;
	dest = 0;
	status = IDLE;
	
	//let's go
	
	CandEvent = genCandMsg();
	if (active)
	{ 	  //scheduleAt(simTime()+2+exponential(1),new cMessage("newAppBurst"));
		scheduleAt(simTime()+2+exponential(IDLE_TIMEOUT), CandEvent);
	//	sprintf(tmpstr, "App Host Active: ID : %d, rate: %d, pktSize: %d, hostNum: %d, active: %d\n",
	//			id, rate, pktSize, hostNum, active);

	//	Log(tmpstr);
	}}void Application::handleMessage(cMessage* msg){	int i,m;
	char displaystr[90], tmpstr[256];
	d("APPLication");

	sprintf(tmpstr,"Application HandleMsg Got Msg: %d", msg->kind());

	Log(tmpstr);

	if (msg->arrivedOn("in"))	/* 如果消息是从路由层过来的,则进行相应的接收处理*/
	{
		d("msg arrived from Route");
		Log("msg arrived from Route\n");

		switch(msg->kind())
			{
				case MSG_CAND:
					handleCand(msg);
					delete msg;					
				break;

				case MSG_HEAD:
					if(CandEvent != NULL)
					{
						if (CandEvent->isScheduled())
							cancelEvent(CandEvent);
					}					
					//handleHead(msg);
					delete msg;
				break;
				case MSG_CONFLICT:
					
					sprintf(tmpstr,"节点 %d :收到的冲突消息暂没有处理\n", id); 
					Log(tmpstr);

				break;
				case MSG_JOIN:

					sprintf(tmpstr,"节点 %d :收到的JOIN消息暂处理\n", id);
					Log(tmpstr);

				break;
				default:
					d("msgkindError");
					sprintf(tmpstr, "节点 %d :收到的未知消息%d暂处理\n", id, msg->kind());
					Log(tmpstr);

				break;
			}

	} //if ends
	else    /*如果消息是 APP自己发送的,则这是系统启动消息,则根据当前状态进行处理,如果仍然是IDLE,则发送CAND消息*/
	{
	
		Log("msg arrived from Self\n");
		switch(msg->kind())
			{
				case MSG_CAND:
						send(CandEvent, "out");
					/*已经是候选节点改变系统的颜色   */
						sprintf(displaystr, displayS, posX, posY, CAND_COLOR);
						parentModule()->setDisplayString(0,displaystr,true);		

						/* 已经发出了候选消息,则生成HEAD 消息准备发送 */
						//HeadEvent = genHeadMsg();
						//scheduleAt(simTime()+2+exponential(CAND_TIMEOUT), HeadEvent);
						break;
				case MSG_HEAD:
						send(HeadEvent, "out");
						sprintf(displaystr, displayS, posX, posY, HEAD_COLOR);
						parentModule()->setDisplayString(0,displaystr,true);
						break;

				case MSG_JOIN:
						send(JoinEvent,"out");

						sprintf(displaystr, displayS, posX, posY, IBAND_COLOR);
						parentModule()->setDisplayString(0,displaystr, true);
				default:
						sprintf(tmpstr, "收到自己发出的消息  %d\n", msg->kind());
						Log(tmpstr);
						break;
		} //end of switch		
	} //else end}void Application::finish(){d("Applicarion says bye");}

/* 生成Cand消息 */

cMessage* Application::genCandMsg()
{
 d("genCandMsg");

 cMessage* msg_cand = new cMessage("Candiate", MSG_CAND, CTRL_PKT_SIZE, P_CAND);

 //cMessage * reply = new("Candiate", CAND_MSG);
 
 return msg_cand;
}

/* 生成 Head 消息*/
cMessage* Application::genHeadMsg()
{
	d("genHeadMsg");

	cMessage * msg_head = new cMessage("Head", MSG_HEAD, CTRL_PKT_SIZE, P_HEAD);
	
	return msg_head;
}

/* 生成冲突消息 */
cMessage* Application::genConflictMsg()
{
	d("genConflictMsg");

	cMessage *msg_conflict = new cMessage("Conflict", MSG_CONFLICT, CTRL_PKT_SIZE, P_CONFLICT);

	return msg_conflict;
}
/* 生成 JOIN 消息 */

cMessage* Application::genJoinMsg()
{
	d("genJoinMsg");

	cMessage * msg_join = new cMessage("Join", MSG_JOIN, CTRL_PKT_SIZE, P_JOIN);

	return msg_join;
}

/* 处理 候选消息 */
void Application::handleCand(cMessage* msg)
{
	char displaystr[90], tmpstr[256];
	int fmod, fph;
	int i;

	//fmod  = msg->frommod();
	d("handleHello");
	switch(status)
	{
		case IDLE:
			status = I_BAND;
			//candidates.add(msg->arrivalModuleId);						
			
			sprintf(displaystr, displayS,posX, posY, IBAND_COLOR);			
			parentModule()->setDisplayString(0,displaystr,true);
			
			break;
		case CAND:
			status = CAND;
			
			if(CandEvent != NULL)
			{
				sprintf(tmpstr,"[%d] handCand event not null1\n", parentModule()->id());
				Log(tmpstr);
				if (CandEvent->isScheduled())
				 cancelEvent(CandEvent);

				sprintf(tmpstr,"[%d] handCand event not null2\n", parentModule()->id());
				Log(tmpstr);
			}
			break;
		case HEAD:			
			/*需要发送冲突消息*/		
			//ConflictEvent = genConflictMsg();

			//nsend(ConflictEvent, "out");
			break;
		case I_BAND:
			/*如果已经在某个节点IBAND */
			d("I_BAND");
			break;
		case O_BAND:
			/*如果已经在某个节点IBAND */
			d("O_BAND");
			break;
		default:
			d("ErrorStatus");	
	}
}
/* 处理头 通知消息*/
void Application::handleHead(cMessage *msg)
{
	char displaystr[90], tmpstr[256];
	int fmod, fph;
		

	//fmod  = msg->frommod();
	d("handleHello");
	switch(status)
	{
		case IDLE:
			status = I_BAND;
			//candidates.add(msg->arrivalModuleId);						
			
			sprintf(displaystr, displayS,posX, posY, IBAND_COLOR);			
			parentModule()->setDisplayString(0,displaystr,true);

			JoinEvent= genJoinMsg();
			scheduleAt(simTime()+2+exponential(TIMEOUT), JoinEvent);
			
			break;
		case CAND:
			status = CAND;
			
			if(CandEvent != NULL)
			{
				if (CandEvent->isScheduled())
				 cancelEvent(CandEvent);
			}
			break;
		case HEAD:			
			/*需要发送冲突消息*/		
			ConflictEvent = genConflictMsg();
			sprintf(tmpstr, "节点%d 已是头节点 收到头信息 %d , \n", id, msg->kind() );
			Log(tmpstr);

			//send(ConflictEvent, "out");
			break;
		case I_BAND:
			/*如果已经在某个节点IBAND */
			d("I_BAND");
			break;
		case O_BAND:
			/*如果已经在某个节点IBAND */
			d("O_BAND");
			break;
		default:
			d("ErrorStatus");	
	}
}

/* 处理 冲突消息 */
void Application::handleConflict(cMessage *msg)
{
	d("HandleConflict");

	switch(status)
	{
		case IDLE:
			delete msg;
			break;
		case CAND:
			status = LOCK;
			break;
		case HEAD:
			/*需要发送冲突消息*/		
			ConflictEvent = genConflictMsg();

			send(ConflictEvent, "toRoute");
			break;
		case I_BAND:
			/*如果已经在某个节点IBAND */
			d("I_BAND");
			break;
		case O_BAND:
			/*如果已经在某个节点IBAND */
			d("O_BAND");
			break;
		default:
			d("ErrorStatus");	
	}
}
/* 处理加入消息*/
void Application::handleJoin(cMessage *msg)
{

}

/* 处理心跳消息*/
void Application::handleHello(cMessage* msg)
{
	d("handleHello");
}

⌨️ 快捷键说明

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