explorer
explorer

Reputation: 113

How to use simulation time to triger action in modules?

I want to create simulation with server and 2 nodes. Nodes have defined vector that contain times for turn on/off.(example timersOnOff = 5,10,13,25 … nod will turn on in 5th second of beginning simulation, and then will be shutdown in 10th seconds etc). How to trigger action at certain time to send msg to node to "turn on" or "turn off".?

Upvotes: 1

Views: 101

Answers (1)

Jerzy D.
Jerzy D.

Reputation: 7002

Let's assume that these times are written in timersOnOff declared as:

std::vector<simtime_t> timersOnOff;

In initialize() add the following code:

for (int i = 0; i < timersOnOff.size(); i = i + 2) {
   simtime_t timeOn = timersOnOff[i];
   simtime_t timeOff = timersOnOff[i+1];
   cMessage * msgOn = new cMessage("nodeOn");     // (1)
   cMessage * msgOff = new cMessage("nodeOff");   // (2)
   scheduleAt (timeOn, msgOn);
   scheduleAt (timeOff, msgOff);
}

The above code schedules all ON and OFF events.

Then, in handleMessage() add:

if (msg->isSelfMessage()) {
   if (msg->isName("nodeOn")) {  // the same name as in (1)
      delete msg;
      // turning on the node

   } else if (msg->isName("nodeOff")) { // the same name as in (2)
      delete msg;
      // turning off the node

   }
} else {
   // ...
}

Upvotes: 1

Related Questions