Untitled
unknown
plain_text
a year ago
3.6 kB
8
Indexable
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include "tictoc17_m.h"
using namespace omnetpp;
class Txc17 : public cSimpleModule
{
private:
simsignal_t arrivalSignal;
cMessage *timerEvent; // Self-message for periodic triggering
double messageInterval; // Time interval between messages
protected:
virtual TicTocMsg17 *generateMessage();
virtual void forwardMessage(TicTocMsg17 *msg);
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
virtual void finish() override; // Clean-up method
};
Define_Module(Txc17);
void Txc17::initialize()
{
arrivalSignal = registerSignal("arrival");
messageInterval = par("messageInterval"); // Fetch interval from a parameter
// Create a self-message to trigger periodic message generation
timerEvent = new cMessage("timerEvent");
// Module 0 sends the first message and starts the periodic process
if (getIndex() == 0) {
// Schedule the first self-message to trigger message generation
scheduleAt(simTime() + messageInterval, timerEvent);
}
}
void Txc17::handleMessage(cMessage *msg)
{
// If the received message is the timer event, generate and send a new message
if (msg == timerEvent) {
TicTocMsg17 *newmsg = generateMessage();
EV << "Generating periodic message: " << newmsg << endl;
forwardMessage(newmsg);
// Schedule the next self-message to generate the next message after the interval
scheduleAt(simTime() + messageInterval, timerEvent);
}
else {
// Otherwise, it's a normal TicTocMsg17 message that needs to be forwarded
TicTocMsg17 *ttmsg = check_and_cast<TicTocMsg17 *>(msg);
if (ttmsg->getDestination() == getIndex()) {
// Message arrived
int hopcount = ttmsg->getHopCount();
// Emit signal and update GUI
emit(arrivalSignal, hopcount);
if (hasGUI()) {
char label[50];
snprintf(label, sizeof(label), "last hopCount = %d", hopcount);
cCanvas *canvas = getParentModule()->getCanvas();
cTextFigure *textFigure = check_and_cast<cTextFigure*>(canvas->getFigure("lasthopcount"));
textFigure->setText(label);
}
EV << "Message " << ttmsg << " arrived after " << hopcount << " hops.\n";
bubble("ARRIVED!");
delete ttmsg;
}
else {
// Forward the message
forwardMessage(ttmsg);
}
}
}
TicTocMsg17 *Txc17::generateMessage()
{
// Produce source and destination addresses.
int src = getIndex();
int n = getVectorSize();
int dest = intuniform(0, n-2);
if (dest >= src)
dest++;
char msgname[20];
snprintf(msgname, sizeof(msgname), "tic-%d-to-%d", src, dest);
// Create message object and set source and destination field.
TicTocMsg17 *msg = new TicTocMsg17(msgname);
msg->setSource(src);
msg->setDestination(dest);
return msg;
}
void Txc17::forwardMessage(TicTocMsg17 *msg)
{
// Increment hop count
msg->setHopCount(msg->getHopCount() + 1);
// Random gate routing
int n = gateSize("gate");
int k = intuniform(0, n - 1);
EV << "Forwarding message " << msg << " on gate[" << k << "]\n";
send(msg, "gate$o", k);
}
void Txc17::finish()
{
// Clean up the self-message
cancelAndDelete(timerEvent);
}
Editor is loading...
Leave a Comment