Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // This file is part of an OMNeT++/OMNEST simulation example.
- //
- // Copyright (C) 2003 Ahmet Sekercioglu
- // Copyright (C) 2003-2015 Andras Varga
- //
- // This file is distributed WITHOUT ANY WARRANTY. See the file
- // `license' for details on this and other legal matters.
- //
- #include <string.h>
- #include <omnetpp.h>
- using namespace omnetpp;
- enum MessageType {
- C , D, ED
- };
- /**
- * Derive the Txc1 class from cSimpleModule. In the Tictoc1 network,
- * both the `tic' and `toc' modules are Txc1 objects, created by OMNeT++
- * at the beginning of the simulation.
- */
- class Txc1 : public cSimpleModule
- {
- protected:
- // The following redefined virtual function holds the algorithm.
- virtual void initialize() override;
- virtual void handleMessage(cMessage *msg) override;
- };
- // The module class needs to be registered with OMNeT++
- Define_Module(Txc1);
- void Txc1::initialize()
- {
- // Initialize is called at the beginning of the simulation.
- // To bootstrap the tic-toc-tic-toc process, one of the modules needs
- // to send the first message. Let this be `tic'.
- enum MessageType type;
- // Am I Tic or Toc?
- if (strcmp("tic", getName()) == 0) {
- // create and send first message on gate "out". "tictocMsg" is an
- // arbitrary string which will be the name of the message object.
- cMessage *msg = new cMessage("tictocMsg", (short)(type = C));
- simtime_t delay = par("delayTime");
- sendDelayed(msg, delay, "out");
- cMessage *msg2 = new cMessage("tictocMsg2", (short)(type = D));
- delay = par("delayTime");
- sendDelayed(msg2, delay, "out");
- // type = ED;
- // cMessage *msg3 = new cMessage("tictocMsg3", (short)type);
- // sendDelayed(msg3, 2, "out");
- }
- }
- void Txc1::handleMessage(cMessage *msg)
- {
- // The handleMessage() method is called whenever a message arrives
- // at the module. Here, we just send it to the other module, through
- // gate `out'. Because both `tic' and `toc' does the same, the message
- // will bounce between the two.
- simtime_t delay = par("delayTime");
- // enum MessageType type;
- // if (msg->getKind() == MessageType::C) {
- // sendDelayed(msg, delay, "out");
- // } else if (msg->getKind() == MessageType::D) {
- // sendDelayed(msg, delay, "out");
- // } else { // ED
- // sendDelayed(msg, delay, "out");
- // }
- sendDelayed(msg, delay, "out");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement