How to conditionally add events 'during' the simulation in NS3

249 views
Skip to first unread message

Shashank

unread,
Apr 24, 2016, 8:10:33 AM4/24/16
to ns-3-users
Question 1 : I want to run a simulation in NS3 where i don't know the time values initially. Is it possible to run such type of simulation without changing the source code of simulator ?. So far in NS3 i had to put the accurate time values for running simulation but i want to conditionally add an event to the event queue of the simulator. Here is the sample code which i want to run.


 
   #include <ns3/log.h>
   
#include <ns3/core-module.h>
   
#include <ns3/simulator.h>
   
#include <iostream>
   
   
using namespace ns3;
   
   
int TEST = 0; // sample global variable
   
void simCheck(){
    TEST
= (int)rand()%10;
      std
::cout<<"Test "<<TEST<<"\n";
   
}
   
   
/* I want the if(TEST>5) condition to pass.
       Is there any way i can do this
        I've tried a few variations but the Simulator::Now().GetSeconds()
        goes out of bounds and simulation stops.
    */

   
int main(int argc, char const *argv[]) {
   
int mtime = 0;
   
while(mtime < 10 ){
       
Simulator::Schedule(Seconds(Simulator::Now().GetSeconds() + mtime),&simCheck);
       
if(TEST > 5){
       
Simulator::Schedule(Seconds (Simulator::Now().GetSeconds() + mtime ),&simCheck);
       std
::cout<<"If condition passed"<<"\n";
       
}
        mtime
+= 1;
     
}
     
Simulator::Run();
   
Simulator::Destroy ();
     
     
return 0;
   
}




Question 2: Is there any reason why NS3 is implemented in this fashion, Of just adding the simulation statements into the queue ignoring any dynamically added events like the one in the above code example? 
Thanks in advance

Tommaso Pecorella

unread,
Apr 24, 2016, 12:09:12 PM4/24/16
to ns-3-users
Hi,

please read the tutorial and clarify your ideas about what's an event-based simulator.

The code example you posted shows that you are thinking procedurally, while a event based simulator is an object oriented system where instance calls are organized by a clock.
As a consequence, your main mistake ins't in the code, it's in how you're thinking to the code.

T.

Shashank

unread,
Apr 24, 2016, 1:40:41 PM4/24/16
to ns-3-users
Thanks for the quick reply. I will go through the tutorials again. 
Regards
Reply all
Reply to author
Forward
0 new messages