0

I want to implement a timer-based message scheme in VEINs/OMNeT++. Here is a scenario: one node sends message to many nodes (let's say 5 nodes). Each node after receiving message sets its timer to broadcast message to other nodes in a network basing on its distance from sender node, such that the furthest node, set shortest timer. And when a node receives message from other nodes before its timer expired, it cancels the timer. But if the timer expires and it has not received any message from other nodes it broadcast the message.

I tried followed explanation in this link How to implement timers in Omnet++?

I have declared a timer message in the initialize() function

MyApp::Initialize(int stage)
{
    RstValueEvt = new cMessage("reset value evt");
}

Then onWSM function for receiving message checks if a message is received again, I check the timer event, if it is scheduled I cancel the timer using:

MyApp::onWSM(BaseFrame1609* frame)

infoMsg* wsm = check _and_cast<infoMsg>(frame)

if(wsm.getrecipient==myId)
{
    if(RstValueEvt->isScheduled())
           { cancelEvent(RstValueEvt); }
    else{scheduleAt(simTime()+timer, RstValueEvt);
//creating copy of the message that I need to rebroadcast
cMessage* copyMessage = (cMessage *)infoMsg.dup;

}
}

My issue, is how to make this node broadcast the copy of message(infoMsg) to all nodes in the network when timer expires, that is how to handle this message in handleselfmsg fcn and onWSM fcn?

bettyn
  • 25
  • 4

1 Answers1

0

I suggest the following way to achieve your goal:

  • declare in your class a message for storing received broadcast message, e.g.

    cMessage * toBroadcastMsg;
    
  • in constructor set

    toBroadcastMsg = nullptr;
    
  • create an instance of selfmessage (timer):

    MyApp::initialize() {
      // ...
      RstValueEvt = new cMessage("reset value evt");
      // ... }
    
  • check whether your selfmessage (timer) expires:

    MyApp::handleSelfMsg(cMessage* msg) {
      // ...
      if (RstValueEvt == msg && toBroadcastMsg != nullptr) {
            // (2) send a message from toBroadcastMsg 
            // ...
            toBroadcastMsg = nullptr;
       }
    
  • schedule the timer after receiving a broadcast message as well as store a duplicate of received broadcast message:

    MyApp::onWSM(BaseFrame1609_4* wsm) {
      if(wsm.getrecipient == myId) {
        if(RstValueEvt->isScheduled()) { 
           cancelEvent(RstValueEvt); 
        } else {
           scheduleAt(simTime() + timer, RstValueEvt);
           // (1) remember copy of the message for future use
           toBroadcastMsg = wsm->dup;
        }
      }
    
Jerzy D.
  • 6,707
  • 2
  • 16
  • 22
  • Can you please help with the code for sending a rebroadcast when the timer expires @Jerzy D. – bettyn Nov 14 '22 at 19:46
  • Your question does not contain any information about the type of message using to broadcast nor when and how it is sent. Could you provide the code you are using to send and receive the first broadcast message? – Jerzy D. Nov 15 '22 at 22:15
  • I have edited my question to include code of the message required to be broadcasted after being received in onWSM function – bettyn Nov 17 '22 at 09:38
  • I have added a variable for storing a copy of received message in my answer. In (2) you should add a command for sending broadcast message - you did not show that command in your code, so I cannot include it in my answer. – Jerzy D. Nov 18 '22 at 10:19
  • Thank you very much @Jerzy D., I have managed to broadcast after following up on your answer but my issue now is all the nodes are broadcasting after their timer ends, seems like the cancel event part does not work or maybe the broadcasted messages does not reach the scheduled nodes before the timer expires – bettyn Nov 24 '22 at 19:39