A Discrete-Event Network Simulator
API
simulator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/core-config.h"
21 #include "simulator.h"
22 #include "simulator-impl.h"
23 #include "scheduler.h"
24 #include "map-scheduler.h"
25 #include "event-impl.h"
26 #include "des-metrics.h"
27 
28 #include "ptr.h"
29 #include "string.h"
30 #include "object-factory.h"
31 #include "global-value.h"
32 #include "assert.h"
33 #include "log.h"
34 
35 #include <cmath>
36 #include <fstream>
37 #include <list>
38 #include <vector>
39 #include <iostream>
40 #include <iomanip>
41 
49 namespace ns3 {
50 
51 // Note: Logging in this file is largely avoided due to the
52 // number of calls that are made to these functions and the possibility
53 // of causing recursions leading to stack overflow
54 NS_LOG_COMPONENT_DEFINE ("Simulator");
55 
63  ("SimulatorImplementationType",
64  "The object class to use as the simulator implementation",
65  StringValue ("ns3::DefaultSimulatorImpl"),
67 
74 static GlobalValue g_schedTypeImpl = GlobalValue ("SchedulerType",
75  "The object class to use as the scheduler implementation",
78 
84 static SimulatorImpl **PeekImpl (void)
85 {
86  static SimulatorImpl *impl = 0;
87  return &impl;
88 }
89 
96 static SimulatorImpl * GetImpl (void)
97 {
98  SimulatorImpl **pimpl = PeekImpl ();
99  /* Please, don't include any calls to logging macros in this function
100  * or pay the price, that is, stack explosions.
101  */
102  if (*pimpl == 0)
103  {
104  {
105  ObjectFactory factory;
106  StringValue s;
107 
109  factory.SetTypeId (s.Get ());
110  *pimpl = GetPointer (factory.Create<SimulatorImpl> ());
111  }
112  {
113  ObjectFactory factory;
114  StringValue s;
116  factory.SetTypeId (s.Get ());
117  (*pimpl)->SetScheduler (factory);
118  }
119 
120 //
121 // Note: we call LogSetTimePrinter _after_ creating the implementation
122 // object because the act of creation can trigger calls to the logging
123 // framework which would call the TimePrinter function which would call
124 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
125 // in an infinite recursion until the stack explodes.
126 //
129  }
130  return *pimpl;
131 }
132 
133 void
135 {
137 
138  SimulatorImpl **pimpl = PeekImpl ();
139  if (*pimpl == 0)
140  {
141  return;
142  }
143  /* Note: we have to call LogSetTimePrinter (0) below because if we do not do
144  * this, and restart a simulation after this call to Destroy, (which is
145  * legal), Simulator::GetImpl will trigger again an infinite recursion until
146  * the stack explodes.
147  */
148  LogSetTimePrinter (0);
149  LogSetNodePrinter (0);
150  (*pimpl)->Destroy ();
151  (*pimpl)->Unref ();
152  *pimpl = 0;
153 }
154 
155 void
157 {
158  NS_LOG_FUNCTION (schedulerFactory);
159  GetImpl ()->SetScheduler (schedulerFactory);
160 }
161 
162 bool
164 {
166  return GetImpl ()->IsFinished ();
167 }
168 
169 void
171 {
174  GetImpl ()->Run ();
175 }
176 
177 void
179 {
181  NS_LOG_LOGIC ("stop");
182  GetImpl ()->Stop ();
183 }
184 
185 void
186 Simulator::Stop (Time const &delay)
187 {
188  NS_LOG_FUNCTION (delay);
189  GetImpl ()->Stop (delay);
190 }
191 
192 Time
194 {
195  /* Please, don't include any calls to logging macros in this function
196  * or pay the price, that is, stack explosions.
197  */
198  return GetImpl ()->Now ();
199 }
200 
201 Time
203 {
204  NS_LOG_FUNCTION (&id);
205  return GetImpl ()->GetDelayLeft (id);
206 }
207 
208 EventId
209 Simulator::Schedule (Time const &delay, const Ptr<EventImpl> &event)
210 {
211  return DoSchedule (delay, GetPointer (event));
212 }
213 
214 EventId
216 {
217  return DoScheduleNow (GetPointer (ev));
218 }
219 void
220 Simulator::ScheduleWithContext (uint32_t context, const Time &delay, EventImpl *impl)
221 {
222 #ifdef ENABLE_DES_METRICS
223  DesMetrics::Get ()->TraceWithContext (context, Now (), delay);
224 #endif
225  return GetImpl ()->ScheduleWithContext (context, delay, impl);
226 }
227 EventId
229 {
230  return DoScheduleDestroy (GetPointer (ev));
231 }
232 EventId
234 {
235 #ifdef ENABLE_DES_METRICS
236  DesMetrics::Get ()->Trace (Now (), time);
237 #endif
238  return GetImpl ()->Schedule (time, impl);
239 }
240 EventId
242 {
243 #ifdef ENABLE_DES_METRICS
244  DesMetrics::Get ()->Trace (Now (), Time (0));
245 #endif
246  return GetImpl ()->ScheduleNow (impl);
247 }
248 EventId
250 {
251  return GetImpl ()->ScheduleDestroy (impl);
252 }
253 
254 
255 EventId
256 Simulator::Schedule (Time const &delay, void (*f)(void))
257 {
258  return DoSchedule (delay, MakeEvent (f));
259 }
260 
261 void
262 Simulator::ScheduleWithContext (uint32_t context, Time const &delay, void (*f)(void))
263 {
264  return ScheduleWithContext (context, delay, MakeEvent (f));
265 }
266 
267 EventId
268 Simulator::ScheduleNow (void (*f)(void))
269 {
270  return DoScheduleNow (MakeEvent (f));
271 }
272 
273 EventId
275 {
276  return DoScheduleDestroy (MakeEvent (f));
277 }
278 
279 void
281 {
282  if (*PeekImpl () == 0)
283  {
284  return;
285  }
286  return GetImpl ()->Remove (id);
287 }
288 
289 void
291 {
292  if (*PeekImpl () == 0)
293  {
294  return;
295  }
296  return GetImpl ()->Cancel (id);
297 }
298 
299 bool
301 {
302  if (*PeekImpl () == 0)
303  {
304  return true;
305  }
306  return GetImpl ()->IsExpired (id);
307 }
308 
309 Time Now (void)
310 {
311  return Time (Simulator::Now ());
312 }
313 
314 Time
316 {
318  return GetImpl ()->GetMaximumSimulationTime ();
319 }
320 
321 uint32_t
323 {
324  return GetImpl ()->GetContext ();
325 }
326 
327 uint64_t
329 {
330  return GetImpl ()-> GetEventCount ();
331 }
332 
333 uint32_t
335 {
337 
338  if (*PeekImpl () != 0)
339  {
340  return GetImpl ()->GetSystemId ();
341  }
342  else
343  {
344  return 0;
345  }
346 }
347 
348 void
350 {
351  NS_LOG_FUNCTION (impl);
352  if (*PeekImpl () != 0)
353  {
354  NS_FATAL_ERROR ("It is not possible to set the implementation after calling any Simulator:: function. Call Simulator::SetImplementation earlier or after Simulator::Destroy.");
355  }
356  *PeekImpl () = GetPointer (impl);
357  // Set the default scheduler
358  ObjectFactory factory;
359  StringValue s;
361  factory.SetTypeId (s.Get ());
362  impl->SetScheduler (factory);
363 //
364 // Note: we call LogSetTimePrinter _after_ creating the implementation
365 // object because the act of creation can trigger calls to the logging
366 // framework which would call the TimePrinter function which would call
367 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
368 // in an infinite recursion until the stack explodes.
369 //
372 }
373 
376 {
378  return GetImpl ();
379 }
380 
381 
382 
383 } // namespace ns3
384 
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:202
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
void DefaultTimePrinter(std::ostream &os)
Default Time printer.
Definition: time-printer.cc:39
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void GetValue(AttributeValue &value) const
Get the value.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
ns3::EventImpl declarations.
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:349
virtual bool IsExpired(const EventId &id) const =0
Check if an event has already run or been cancelled.
Hold variables of type string.
Definition: string.h:41
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition: simulator.cc:249
static Ptr< SimulatorImpl > GetImplementation(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:375
ns3::StringValue attribute value declarations.
ns3::Ptr smart pointer declaration and implementation.
static uint32_t GetSystemId(void)
Get the system id of this simulator.
Definition: simulator.cc:334
virtual Time Now(void) const =0
Return the current simulation virtual time.
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:629
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:322
ns3::ObjectFactory class declaration.
static void Run(void)
Run the simulation.
Definition: simulator.cc:170
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
virtual EventId ScheduleNow(EventImpl *event)=0
Schedule an event to run at the current virtual time.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:290
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:743
ns3::Simulator declaration.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event)=0
Schedule a future event execution (in a different context).
virtual void SetScheduler(ObjectFactory schedulerFactory)=0
Set the Scheduler to be used to manage the event list.
Hold a so-called &#39;global value&#39;.
Definition: global-value.h:73
virtual void Cancel(const EventId &id)=0
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
static SimulatorImpl * GetImpl(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:96
static DesMetrics * Get(void)
Get a pointer to the singleton instance.
Definition: singleton.h:89
ns3::DesMetrics declaration.
virtual void Run(void)=0
Run the simulation.
virtual Time GetMaximumSimulationTime(void) const =0
Get the maximum representable simulation time.
virtual bool IsFinished(void) const =0
Check if the simulation should finish.
virtual void Remove(const EventId &id)=0
Remove an event from the event list.
ns3::SimulatorImpl declaration.
ns3::Scheduler abstract base class, ns3::Scheduler::Event and ns3::Scheduler::EventKey declarations...
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:570
std::string Get(void) const
Definition: string.cc:31
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1389
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:643
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
virtual Time GetDelayLeft(const EventId &id) const =0
Get the remaining time until this event will execute.
AttributeValue implementation for TypeId.
Definition: type-id.h:608
static SimulatorImpl ** PeekImpl(void)
Get the static SimulatorImpl instance.
Definition: simulator.cc:84
virtual EventId ScheduleDestroy(EventImpl *event)=0
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
static EventId DoSchedule(Time const &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:233
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
double f(double x, void *params)
Definition: 80211b.c:70
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:280
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
Definition: simulator.cc:300
void TraceWithContext(uint32_t context, const Time &now, const Time &delay)
Trace an event (with context) at the time it is scheduled.
Definition: des-metrics.cc:105
static uint64_t GetEventCount(void)
Get the number of events executed.
Definition: simulator.cc:328
ns3::GlobalValue declaration.
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1578
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:156
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
static GlobalValue g_schedTypeImpl
The specific event scheduler implementation to use.
Definition: simulator.cc:74
Instantiate subclasses of ns3::Object.
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1483
A simulation event.
Definition: event-impl.h:44
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:241
virtual EventId Schedule(const Time &delay, EventImpl *event)=0
Schedule a future event execution (in the same context).
An identifier for simulation events.
Definition: event-id.h:53
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:178
virtual void Stop(void)=0
Tell the Simulator the calling event should be the last one executed.
void DefaultNodePrinter(std::ostream &os)
Default node id printer implementation.
Definition: node-printer.cc:44
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:163
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Schedule an event to expire when Simulator::Destroy is called.
Definition: simulator.h:1685
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:309
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
Definition: des-metrics.cc:99
virtual uint32_t GetSystemId() const =0
Get the system id of this simulator.
ns3::MapScheduler declaration.
static void ClearMarkedTimes()
Remove all MarkedTimes.
Definition: time.cc:256
static GlobalValue g_simTypeImpl
The specific simulator implementation to use.
Definition: simulator.cc:62
Debug message logging.
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1225
virtual uint32_t GetContext(void) const =0
Get the current simulation context.
static Time GetMaximumSimulationTime(void)
Get the maximum representable simulation time.
Definition: simulator.cc:315
The SimulatorImpl base class.
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:34
static TypeId GetTypeId(void)
Register this type.