#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include "tictoc12_m.h"
class Txc12 : public cSimpleModule
{
private:
long numSent;
long numReceived;
cLongHistogram hopCountStats;
cOutVector hopCountVector;
protected:
virtual TicTocMsg12 *generateMessage();
virtual void forwardMessage(TicTocMsg12 *msg);
virtual void initialize();
virtual void handleMessage(cMessage *msg);
virtual void finish();
};
Define_Module(Txc12);
void Txc12::initialize()
{
numSent = 0;
numReceived = 0;
WATCH(numSent);
WATCH(numReceived);
hopCountStats.setName("hopCountStats");
hopCountStats.setRangeAutoUpper(0, 10, 1.5);
hopCountVector.setName("HopCount");
if (index()==0)
{
TicTocMsg12 *msg = generateMessage();
scheduleAt(0.0, msg);
}
}
void Txc12::handleMessage(cMessage *msg)
{
TicTocMsg12 *ttmsg = check_and_cast<TicTocMsg12 *>(msg);
if (ttmsg->getDestination()==index())
{
int hopcount = ttmsg->getHopCount();
ev << "Message " << ttmsg << " arrived after " << hopcount << " hops.\n";
bubble("ARRIVED, starting new one!");
numReceived++;
hopCountVector.record(hopcount);
hopCountStats.collect(hopcount);
delete ttmsg;
ev << "Generating another message: ";
TicTocMsg12 *newmsg = generateMessage();
ev << newmsg << endl;
forwardMessage(newmsg);
numSent++;
}
else
{
forwardMessage(ttmsg);
}
}
TicTocMsg12 *Txc12::generateMessage()
{
int src = index();
int n = size();
int dest = intuniform(0,n-2);
if (dest>=src) dest++;
char msgname[20];
sprintf(msgname, "tic-%d-to-%d", src, dest);
TicTocMsg12 *msg = new TicTocMsg12(msgname);
msg->setSource(src);
msg->setDestination(dest);
return msg;
}
void Txc12::forwardMessage(TicTocMsg12 *msg)
{
msg->setHopCount(msg->getHopCount()+1);
int n = gate("out")->size();
int k = intuniform(0,n-1);
ev << "Forwarding message " << msg << " on port out[" << k << "]\n";
send(msg, "out", k);
}
void Txc12::finish()
{
ev << "Sent: " << numSent << endl;
ev << "Received: " << numReceived << endl;
ev << "Hop count, min: " << hopCountStats.min() << endl;
ev << "Hop count, max: " << hopCountStats.max() << endl;
ev << "Hop count, mean: " << hopCountStats.mean() << endl;
ev << "Hop count, stddev: " << hopCountStats.stddev() << endl;
recordScalar("#sent", numSent);
recordScalar("#received", numReceived);
hopCountStats.recordScalar("hop count");
}