📄 bcast.cpp
字号:
// bcast.cpp: implementation of the BCAST class.
//
//////////////////////////////////////////////////////////////////////
#include "h/bcast.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#include "h/bcast.h"
#include "h/Costants.h"
#define max(a,b) (a>b ? a : b)
Define_Module_Like(BCAST,Routing);
/* waiting PKT 实现*/
WaitingPkt::WaitingPkt(){};
WaitingPkt::~WaitingPkt(){};
//****************** costructors and destructors of secondary objects ****************
BCAST::~BCAST(){}
void BCAST::initialize()
{
d("The route use BCAST protocol ");
//let some vars to be editable from the TkEnv environment
// WATCH(sequenceNumber);
// WATCH(statistics.sentCtrlPkt);
//schedule the first message tho initialize the send hello chain
// helloEvent = new cMessage("sendHello",MSG_HELLO,0,P_SEND_HELLO);
// scheduleAt(simTime()+0.5, helloEvent);
}
void BCAST::handleMessage(cMessage *msg)
{
cMessage *reply;
/* 如果消息来自应用层,则将其广播到所有的节点去*/
d("HANDLE message routine");
if (msg->arrivedOn("fromApp") )
{
d("messasge arrived from app");
reply =generateBCASTmsg(msg);
broadcast(reply);
delete msg;
}/*否则消息是来自Mac层,将其转发到上层*/
else
{
d("Message arrived from Mac");
send(msg, "toApp");
}
}
void BCAST::finish()
{
//I will write on a file instead of usa recordScalar() beacuse of a bug in this
//function that rewrites the file on each run
FILE* fout = fopen("collectedData.dat","a");
dd("Hosts number..........."<< (int)parentModule()->par("numHost"));
dd("Sent control pakets...."<<statistics.sentCtrlPkt);
dd("Sent data pakets......."<<statistics.sentDataPkt);
dd("Delivered data pakets.."<<statistics.deliveredDataMsg);
fprintf(fout,"BCAST HostHosts number...........%d\n",(int)parentModule()->par("numHost"));
fprintf(fout,"BCAST.....\n");
/*
if(statistics.deliveredDataMsg > 0)
dd("Hops Avarage..........."<<statistics.hopsSum / statistics.deliveredDataMsg );
PartialStat* cell;
fprintf(fout,"Hosts number:............... %d\n",(int)parentModule()->par("numHost"));
fprintf(fout,"Sent control pakets......... %d\n",statistics.sentCtrlPkt);
fprintf(fout,"Sent data pakets............ %d\n",statistics.sentDataPkt);
fprintf(fout,"Delivered data pakets....... %d\n",statistics.deliveredDataMsg);
for(int i=0; i<= statistics.maxHop; i++)
{
cell = (PartialStat*) statistics.hopsV[i];
if(cell)
{
fprintf(fout,"Per-Hop throughput misured... %d %.0f\n",i, cell->throughSum/cell->samples);
dd("Per-Hop throughput misured..."<<i<<" "<<cell->throughSum / cell->samples);
fprintf(fout,"Per-Hop latency misured..... %d %.6f\n",i, cell->latencySum / cell->samples);
dd("Per-Hop latency misured....."<<i<<" "<<cell->latencySum / cell->samples);
}
}
if(statistics.deliveredDataMsg > 0)
fprintf(fout,"Hops Avarage................ %d\n",statistics.hopsSum / statistics.deliveredDataMsg);
*/
fclose(fout);
}
/* use broadcast message */
void BCAST::broadcast(cMessage* msg)
{
if(msg !=NULL)
{
int ttl;
d("send to mac:"<<msg->name()<<" "<<msg->kind());
ttl = (int) msg->par("ttl")-1;
if( ttl >= 0 )
{
msg->par("ttl") = ttl;
msg->par("hopCount") = 1+ (int)msg->par("hopCount");
//add the source parmeter that is common to all the messages
if(msg->hasPar("source"))
msg->par("source") = (int)parentModule()->id();
else
msg->addPar("source") = (int)parentModule()->id();
send(msg,"toMac"); // FIXME it is illegal to send a msg object and keep referencing it!!! -Andras
}
else
{
d("ttl espired! the msg will not be sent:");
delete msg;
}
}
}
cMessage* BCAST::copyMessage(cMessage* msg)
{
//copy the data within the msg oject
cMessage* newMsg = new cMessage(*msg);
return newMsg;
d("copy");
}
cMessage* BCAST::generateBCASTmsg(cMessage* msg)
{
// cMessage* reply = new cMessage(msg->, msg->msgkind, CTRL_PKT_SIZE, PRO);
cMessage* reply = new cMessage(*msg);
//reply = copyMessage(msg);
d("generate BCAST Msg");
reply->addPar("seqNumS") = sequenceNumber;
reply->addPar("hopCount") = 0;
//ttl is not needed due to the nature
//of the message that is never retrasmitted
reply->addPar("ttl") = 1;
reply->addPar("mac") = BROADCAST;
return reply;
}
PartialStat::PartialStat(double lat, double th)
{
latencySum = lat;
throughSum = th;
samples = 1;
};
PartialStat::~PartialStat(){};
Statistics::Statistics()
{
hopsSum = 0;
deliveredDataMsg = 0;
sendDataMsg = 0;
sentCtrlPkt =0;
sentDataPkt =0;
maxHop =0;
}
Statistics::~Statistics(){};
void Statistics::collect(cMessage* msg, double now)
{
double latency = now - (double) msg->par("sendingTime");
int i = (int)msg->par("hopCount");
maxHop = max(maxHop, i);
//if the vector cell is not empty
PartialStat* cell = (PartialStat*)hopsV[i];
if(cell)
{
cell->latencySum += latency;
cell->throughSum += msg->length() / latency;
cell->samples ++;
}
else
{
PartialStat* cell = new PartialStat(latency, msg->length() / latency);
hopsV.addAt(i,cell) ;
}
hopsSum += i;
deliveredDataMsg++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -