3

I'm using TimerManager class provided in veins to manage timers in my simulation. I created a recurring timer that fires every 5 seconds and I want to cancel it when a specific msg is received but I don't know how to use the cancel method:

void cancel(TimerHandle handle)

I don't understand what is TimerHandle and what parameter should I pass to the method in order to cancel the timer

PS:i want to cancel a specific timer not the TimerManager instance

Nina
  • 129
  • 6

2 Answers2

2

When creating a timer, TimerManager::create returns a TimerHandle. You can use this to cancel the timer by passing it to TimerManager::cancel.

Horstinator
  • 518
  • 1
  • 4
  • 18
  • so i have to declare a `TimerHandle` : `TimerHandle=timerManager.create(TimerSpec, "timerName");` and then pass this `TimerHandel` to cancel but idon't know how to declare a `TimerHandle` i'm missing something – Nina Oct 07 '19 at 16:11
  • 1
    Yes, that is what you would have to do. To store the handle in your class you could use a [data member](https://en.cppreference.com/w/cpp/language/data_members). – Horstinator Oct 08 '19 at 10:40
  • i tried this : in file.h `Veins::TimerManager timerManager{this}; Veins::TimerManager::TimerHandle handle;` in file.c `handle=timerManager.create(ttrTimerSpec, "recurring timer");` and to cancel :`timerManager.cancel(handle);` but i didn't work the timer don't get cancel – Nina Oct 10 '19 at 10:20
  • The way you describe your approach it seems correct to me. You might have encountered a bug here. Would you mind sharing your code or, even better, opening an issue on GitHub with a minimum viable example? – Horstinator Oct 10 '19 at 10:59
  • 1
    There is a subtle mistake: initialize is called multiple times, since the class inherits `BaseModule` which defines `numInitStages` to return `2`. The usual approach is to guard the logic in initialize with `if (stage == X) {...}` where X defines the stage you want to use as some logic depends on fields initialized in base classes in earlier stages. In our case, using stage zero should be fine. – Horstinator Oct 10 '19 at 12:30
  • now my code is working as it should work thank you very much – Nina Oct 10 '19 at 13:42
0

@Horstinator this is my code : the RSU's application

    #ifndef RSUAPP_H_
    #define RSUAPP_H_
    #include <omnetpp.h>
    #include "veins/modules/application/ieee80211p/BaseWaveApplLayer.h"
    #include "ttr_m.h"
    #include "veins/modules/utility/TimerManager.h"

    using namespace omnetpp;
    using Veins::TimerManager;

    class rsuApp : public BaseWaveApplLayer {
    public:
    rsuApp();
    ~rsuApp();
    virtual void initialize(int stage);
    virtual void finish();
    protected:
    virtual void onBSM(BasicSafetyMessage* bsm);
    virtual void onWSM(WaveShortMessage* wsm);
    virtual void onWSA(WaveServiceAdvertisment* wsa);
    virtual void handleSelfMsg(cMessage* msg);
    virtual void handlePositionUpdate(cObject* obj);
//declaration of the TimerManager instance and the handle
    TimerManager timerManager{this};
    TimerManager::TimerHandle handle;
    };
   #endif

.

   #include "rsuApp.h"
   #include "ttr_m.h"
   #include "veins/modules/utility/TimerManager.h"
   #include <map>

   Define_Module(rsuApp);

   rsuApp::rsuApp(){}
   void rsuApp::initialize(int stage) {
   BaseWaveApplLayer::initialize(stage);
     auto ttrTimer = [this](){
                Ttr* ttr = new Ttr();
                populateWSM(ttr);
                sendDown(ttr);
          };
       auto ttrTimerSpec = Veins::TimerSpecification(ttrTimer).interval(1);
       handle=timerManager.create(ttrTimerSpec, "recurring timer");
   }

   void rsuApp::finish() {
     BaseWaveApplLayer::finish();
   }

   void rsuApp::onWSM(WaveShortMessage* wsm) {
     if(strcmp(wsm->getName(),"tip")==0){
       findHost()->getDisplayString().updateWith("r=60,red");
   //canceling the timer when a wsm message  is received 
       timerManager.cancel(handle);
     }
   }

   void rsuApp::handleSelfMsg(cMessage* msg) {
    BaseWaveApplLayer::handleSelfMsg(msg);
     timerManager.handleMessage(msg);  
   }

  void rsuApp::onBSM(BasicSafetyMessage* bsm) {}
  void rsuApp::onWSA(WaveServiceAdvertisment* wsa) {}
  void rsuApp::handlePositionUpdate(cObject* obj) {
   BaseWaveApplLayer::handlePositionUpdate(obj);
  }
  rsuApp::~rsuApp(){}
Nina
  • 129
  • 6