#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include "tictoc10_m.h"
class Txc10 : public cSimpleModule
{
protected:
virtual TicTocMsg10 *generateMessage();
virtual void forwardMessage(TicTocMsg10 *msg);
virtual void initialize();
virtual void handleMessage(cMessage *msg);
};
Define_Module(Txc10);
void Txc10::initialize()
{
if (index()==0)
{
TicTocMsg10 *msg = generateMessage();
scheduleAt(0.0, msg);
}
}
void Txc10::handleMessage(cMessage *msg)
{
TicTocMsg10 *ttmsg = check_and_cast<TicTocMsg10 *>(msg);
if (ttmsg->getDestination()==index())
{
ev << "Message " << ttmsg << " arrived after " << ttmsg->getHopCount() << " hops.\n";
bubble("ARRIVED, starting new one!");
delete ttmsg;
ev << "Generating another message: ";
TicTocMsg10 *newmsg = generateMessage();
ev << newmsg << endl;
forwardMessage(newmsg);
}
else
{
forwardMessage(ttmsg);
}
}
TicTocMsg10 *Txc10::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);
TicTocMsg10 *msg = new TicTocMsg10(msgname);
msg->setSource(src);
msg->setDestination(dest);
return msg;
}
void Txc10::forwardMessage(TicTocMsg10 *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);
}