四色算法的改进算法
源代码在线查看: application.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; \
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; \
send(msg,"lowergate_out"); \
}
void application::initialize()
{
// isreq=-1;
nhops=-1;
/*if (ID==0) // node 0 starts the algorithm
{
isreq=1;
parentModule()->par("TYPE")=1;
SEND_BLACK_REQ_NEW;
}*/
}
void application::finish()
{
}
void application::handleMessage(cMessage *msg)
{
msg->addPar("senderPX")=this->parentModule()->par("PX");
msg->addPar("senderPY")=this->parentModule()->par("PY");
switch(msg->kind())
{
case M_LOWHIGH:
{
// ev if(strcmp(msg->name(),"BlackRequest")==0)
{
processBlackRequest(msg);
break;
}
if(strcmp(msg->name(),"GreyRequest")==0)
{
processGreyRequest(msg);
break;
}
ev endSimulation();
}
case M_SELF:
{
if(strcmp(msg->name(),"Start")==0)
{
// ev parentModule()->par("ISREQ")=1;
parentModule()->par("TYPE")=1;
UPDATECOLOR(7);
SEND_BLACK_REQ_NEW;
}
}
break;
default:
ev endSimulation();
}
}
void application::processBlackRequest(cMessage *msg)
{
int isreq=parentModule()->par("ISREQ");
if(isreq==1||isreq==0)
{
delete msg;
return;
}
else if (isreq==-1)
{
UPDATECOLOR(3);
isreq=0;
parentModule()->par("TYPE")=0;
double timeout=(double)msg->par("timeout");
// ev double time=parentModule()->par("TOTALTIME");
scheduleStart(simTime()+time);
SEND_GREY_REQ_NEW;
delete msg;
}
}
void application::processGreyRequest(cMessage *msg)
{
int isreq=parentModule()->par("ISREQ");
if(isreq==-1)
{
int a=parentModule()->par("TYPE");
if(a==2)
{
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 ;
}
}