Data and communication

rplpatel22
Project_1.cc

/* * Project_1.cc * * Created on: Oct 22, 2019 * Author: patel203 */ #include <stdio.h> #include <string.h> #include <omnetpp.h> using namespace omnetpp; class Error : public cSimpleModule { protected: virtual void forwardMessage(cMessage *msg); virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; }; Define_Module(Error); void Error::initialize() { if (getIndex() == 0) { // Boot the process scheduling the initial message as a self-message. char msgname[20]; sprintf(msgname, "Rip-%d", getIndex()); cMessage *msg = new cMessage(msgname); scheduleAt(0.0, msg); } } void Error::handleMessage(cMessage *msg) { if (getIndex() == 3) { // Message arrived. EV << "Message " << msg << " arrived.\n"; delete msg; } else { // We need to forward the message. forwardMessage(msg); } } void Error::forwardMessage(cMessage *msg) { // In this example, we just pick a random gate to send it on. // We draw a random number between 0 and the size of gate `gate[]'. int n = gateSize("gate"); int k = intuniform(0, n-1); EV << "Forwarding message " << msg << " on gate[" << k << "]\n"; // $o and $i suffix is used to identify the input/output part of a two way gate send(msg, "gate$o", k); cMessage *copy = (cMessage *)msg->dup(); send(copy, "gate$o", k); }