Hello,
I want to run a specific function every X second during simulation time.
I have coded the below example, it works, but I would like to be advised if this is the best approach to do it?
What are your opinions guys?
BRsmalta
***********************************************************
NS_LOG_COMPONENT_DEFINE ("Schedule");
static voidsomeEvent (Time interval){NS_LOG_INFO ("event a time :" << Simulator::Now ().GetSeconds () );
Simulator::Schedule (interval ,&someEvent, interval);}
int main(int argc, char *argv[]){
bool verbose = false;//Simulation Time In Secondsdouble SimTime = 10;
you can also declare this as:
Time SimTime = Seconds (10);
There is no problem with using ns3::Time objects at the command line; in fact, there are some possible benefits because it naturally handles units, such as:
--SimTime=60s
or
--SimTime=1min
In general, we recommend to store time values in ns3::Time units
where possible (and do arithmetic on them without converting back
to basic types, if possible).
uint32_t interval = 1;
declare this as:
Time interval = Seconds (1);
There is no need for this variable if you declare 'interval' as a ns3::Time.Time eventInterval = Seconds(interval);
CommandLine cmd (__FILE__);cmd.AddValue("SimTime", "Simulation Time in Seconds", SimTime);cmd.AddValue("Interval", "Tell Function to run every X second", interval);cmd.AddValue("verbose", "Tell aplication to log if true", verbose);cmd.Parse (argc,argv);
Time::SetResolution (Time::NS);
if (verbose){LogComponentEnableAll (LOG_PREFIX_TIME);LogComponentEnable ("Schedule", LOG_INFO);}
eventInterval = Seconds(interval);Simulator::Schedule(Seconds(interval), &someEvent, eventInterval);
The above two statements can then be compressed into one:
Simulator::Schedule (interval, &someEvent, interval);
Simulator::Stop (Seconds(SimTime));
This would become:
Simulator::Stop (SimTime);
std::cout << "Simulation Time :" << SimTime << std::endl;Simulator::Run ();Simulator::Destroy ();return 0;}
--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/d8b02862-96cb-492b-a94c-3b47ce08e242n%40googlegroups.com.