Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. //
  2. // This file is part of an OMNeT++/OMNEST simulation example.
  3. //
  4. // Copyright (C) 2003 Ahmet Sekercioglu
  5. // Copyright (C) 2003-2015 Andras Varga
  6. //
  7. // This file is distributed WITHOUT ANY WARRANTY. See the file
  8. // `license' for details on this and other legal matters.
  9. //
  10.  
  11. #include <string.h>
  12. #include <omnetpp.h>
  13.  
  14. using namespace omnetpp;
  15.  
  16. enum MessageType {
  17.     C , D, ED
  18. };
  19.  
  20. /**
  21.  * Derive the Txc1 class from cSimpleModule. In the Tictoc1 network,
  22.  * both the `tic' and `toc' modules are Txc1 objects, created by OMNeT++
  23.  * at the beginning of the simulation.
  24.  */
  25. class Txc1 : public cSimpleModule
  26. {
  27.   protected:
  28.     // The following redefined virtual function holds the algorithm.
  29.     virtual void initialize() override;
  30.     virtual void handleMessage(cMessage *msg) override;
  31. };
  32.  
  33. // The module class needs to be registered with OMNeT++
  34. Define_Module(Txc1);
  35.  
  36. void Txc1::initialize()
  37. {
  38.     // Initialize is called at the beginning of the simulation.
  39.     // To bootstrap the tic-toc-tic-toc process, one of the modules needs
  40.     // to send the first message. Let this be `tic'.
  41.  
  42.     enum MessageType type;
  43.     // Am I Tic or Toc?
  44.     if (strcmp("tic", getName()) == 0) {
  45.         // create and send first message on gate "out". "tictocMsg" is an
  46.         // arbitrary string which will be the name of the message object.
  47.         cMessage *msg = new cMessage("tictocMsg", (short)(type = C));
  48.         simtime_t delay = par("delayTime");
  49.         sendDelayed(msg, delay, "out");
  50.  
  51.         cMessage *msg2 = new cMessage("tictocMsg2", (short)(type = D));
  52.         delay = par("delayTime");
  53.         sendDelayed(msg2, delay, "out");
  54.  
  55. //        type = ED;
  56. //        cMessage *msg3 = new cMessage("tictocMsg3", (short)type);
  57. //        sendDelayed(msg3, 2, "out");
  58.     }
  59. }
  60.  
  61. void Txc1::handleMessage(cMessage *msg)
  62. {
  63.     // The handleMessage() method is called whenever a message arrives
  64.     // at the module. Here, we just send it to the other module, through
  65.     // gate `out'. Because both `tic' and `toc' does the same, the message
  66.     // will bounce between the two.
  67.     simtime_t delay = par("delayTime");
  68.  
  69. //    enum MessageType type;
  70. //    if (msg->getKind() == MessageType::C) {
  71. //        sendDelayed(msg, delay, "out");
  72. //    } else if (msg->getKind() == MessageType::D) {
  73. //        sendDelayed(msg, delay, "out");
  74. //    } else { // ED
  75. //        sendDelayed(msg, delay, "out");
  76. //    }
  77.     sendDelayed(msg, delay, "out");
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement