Search code examples
omnet++inet

OMNET++: How to create a timer event that fires on each second?


I am using OMNET++ with INET Framework 4.0. How can I create a Timer in my custom Mobility Module to fire at each second so that I can move my AdhocHost every time my timer event fires?


Solution

  • In OMNeT++ self-messages are timers.
    To achieve your goal you should create a cMessage object, schedule it using scheduleAt(), then in handleMessage() reschedule it every time when it expires.
    For example:

    // somewhere in simple module class declaration (*.h)
    simtime_t timerInterval;
    cMessage * timer;
    
    // in initialize()
    timerInterval = 1.0; // one second
    timer = new cMessage("one second timer");
    scheduleAt(simTime() + timerInterval, timer);
    
    // in handleMessage()
    if (msg == timer) {
       // do something
    
       scheduleAt(simTime() + timerInterval, timer);  // rescheduling
    }
    
    // in finish()
    cancelAndDelete(timer);
    


    By the way: there are a lot of examples of using self-messages in OMNeT++ samples, especially in Tictoc.