// // This file is part of an OMNeT++/OMNEST simulation example. // // Copyright (C) 2003 Ahmet Sekercioglu // Copyright (C) 2003-2008 Andras Varga // // This file is distributed WITHOUT ANY WARRANTY. See the file // `license' for details on this and other legal matters. // #include <stdio.h> #include <string.h> #include <omnetpp.h> // Include a generated file: the header file created from tictoc13.msg. // It contains the definition of the TictocMsg10 class, derived from // cMessage. #include "tictoc13_m.h" class Txc13 : public cSimpleModule { protected: virtual TicTocMsg13 *generateMessage(); virtual void forwardMessage(TicTocMsg13 *msg); virtual void initialize(); virtual void handleMessage(cMessage *msg); }; Define_Module(Txc13); void Txc13::initialize() { // Module 0 sends the first message if (getIndex()==0) { // Boot the process scheduling the initial message as a self-message. TicTocMsg13 *msg = generateMessage(); scheduleAt(0.0, msg); } } void Txc13::handleMessage(cMessage *msg) { TicTocMsg13 *ttmsg = check_and_cast<TicTocMsg13 *>(msg); if (ttmsg->getDestination()==getIndex()) { // Message arrived. EV << "Message " << ttmsg << " arrived after " << ttmsg->getHopCount() << " hops.\n"; bubble("ARRIVED, starting new one!"); delete ttmsg; // Generate another one. EV << "Generating another message: "; TicTocMsg13 *newmsg = generateMessage(); EV << newmsg << endl; forwardMessage(newmsg); } else { // We need to forward the message. forwardMessage(ttmsg); } } TicTocMsg13 *Txc13::generateMessage() { // Produce source and destination addresses. int src = getIndex(); // our module index int n = size(); // module vector size int dest = intuniform(0,n-2); if (dest>=src) dest++; char msgname[20]; sprintf(msgname, "tic-%d-to-%d", src, dest); // Create message object and set source and destination field. TicTocMsg13 *msg = new TicTocMsg13(msgname); msg->setSource(src); msg->setDestination(dest); return msg; } void Txc13::forwardMessage(TicTocMsg13 *msg) { // Increment hop count. msg->setHopCount(msg->getHopCount()+1); // Same routing as before: random gate. int n = gateSize("gate"); int k = intuniform(0,n-1); EV << "Forwarding message " << msg << " on gate[" << k << "]\n"; send(msg, "gate$o", k); }