简体   繁体   中英

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".?

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 {
   // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM