四色算法代码

源代码在线查看: application.cpp

软件大小: 765 K
上传用户: hcwlxhyq
关键词: 算法 代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				//-------------------------------------------------------------------				//  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("ppsenderId")=ID;							\
					msg->addPar("psenderId")=ID;							\
					send(msg,"lowergate_out");								\
				}
				
				#define SEND_DARKGREY_REQ_NEW   {							\
					cMessage *msg = new cMessage("DarkGreyRequest");		\
					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("ppsenderId")=ppsenderId;					\
					msg->addPar("psenderId")=psenderId;						\
					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("ppsenderId")=ppsenderId;					\
					msg->addPar("psenderId")=psenderId;						\
					send(msg,"lowergate_out");								\
				} 								void application::initialize()				{					isreq=-1;
				
					for(int i=0;i					{
						psender[i]=-1;
						ppsender[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						{
							if(ppsender[i]!=-1 && psender[i]!=-1 && ppsender[i]!=psender[i] && ID!=ppsenderId)
								ev						}
					}				}								void application::handleMessage(cMessage *msg)				{
					msg->addPar("senderPX")=this->parentModule()->par("PX");
					msg->addPar("senderPY")=this->parentModule()->par("PY");
				
					nhops=msg->par("nhops");
					ppsenderId=msg->par("ppsenderId");
					psenderId=msg->par("psenderId");
				
					if(nhops==0)
					{
						nhops++;
						ppsenderId=msg->par("ppsenderId");
						psenderId=ID;
					}
					else if(nhops==1)
					{
						nhops++;
						ppsenderId=msg->par("ppsenderId");
						psenderId=msg->par("psenderId");
					}
					if(nhops==2)
					{
						ppsender[ppsenderId]=psenderId;
						psender[ppsenderId]=msg->par("senderId");
					}
									switch(msg->kind())					{					case M_LOWHIGH:						{							if(strcmp(msg->name(),"BlackRequest")==0)
							{
								processBlackRequest(msg);
								break;				
							}
				
							if(strcmp(msg->name(),"GreyRequest")==0)
							{
								processGreyRequest(msg);
								break;
							}
				
							if(strcmp(msg->name(),"DarkGreyRequest")==0)
							{
								processDarkGreyRequest(msg);
								break;
							}
											ev 							endSimulation();						}									case M_SELF:						{														if (strcmp(msg->name(),"StartUp")==0)							{								SEND_BLACK_REQ_NEW;								delete msg;								break;							}											ev 							endSimulation();						}									default:						ev 						endSimulation();					}				}				
				void application::processBlackRequest(cMessage *msg)
				{
					isreq=parentModule()->par("TYPE");
				    if(isreq==1||isreq==0)
					{
						if(nhops==2)
						{
							ppsender[ppsenderId]=psenderId;
							psender[psenderId]=msg->par("senderId");
						}
						delete msg;
						return;
					}
				
					else if (isreq==-1)
					{
						UPDATECOLOR(3);
						isreq=0;
						parentModule()->par("TYPE")=0;
				
						double time=(double)msg->par("timeout");
				
						scheduleStart(simTime()+time);
						SEND_GREY_REQ_NEW;
						delete msg;
					}
				}
				
				
				void application::processGreyRequest(cMessage *msg)
				{
					isreq=parentModule()->par("TYPE");
				    if(isreq==1||isreq==0||isreq==2)
					{
						delete msg;
						return;
					}
				
					else if (isreq==-1)
					{
						UPDATECOLOR(5);
						isreq=2;
						parentModule()->par("TYPE")=2;
				
						SEND_DARKGREY_REQ_NEW;
						delete msg;
					}
				}
				
				void application::processDarkGreyRequest(cMessage *msg)
				{
					isreq=parentModule()->par("TYPE");
					if(isreq==-1)
					{
						int a=parentModule()->par("ANOTHER");                            
						if(a==1)
						{
							UPDATECOLOR(3);
							isreq=0;
							parentModule()->par("TYPE")=0;
							SEND_GREY_REQ_NEW;
							delete msg;
						}
						else 
						{
							UPDATECOLOR(7);
							isreq=1;
							parentModule()->par("TYPE")=1;
							SEND_BLACK_REQ_NEW;
							delete msg;
						}
				
					}
				
					else 
					{
						delete msg;
						return ;
					}
				}
							

相关资源