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 
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 
85 static void
86 TimePrinter (std::ostream &os)
87 {
88  os << Simulator::Now ().GetSeconds () << "s";
89 }
90 
97 static void
98 NodePrinter (std::ostream &os)
99 {
101  {
102  os << "-1";
103  }
104  else
105  {
106  os << Simulator::GetContext ();
107  }
108 }
109 
115 static SimulatorImpl **PeekImpl (void)
116 {
117  static SimulatorImpl *impl = 0;
118  return &impl;
119 }
120 
127 static SimulatorImpl * GetImpl (void)
128 {
129  SimulatorImpl **pimpl = PeekImpl ();
130  /* Please, don't include any calls to logging macros in this function
131  * or pay the price, that is, stack explosions.
132  */
133  if (*pimpl == 0)
134  {
135  {
136  ObjectFactory factory;
137  StringValue s;
138 
139  g_simTypeImpl.GetValue (s);
140  factory.SetTypeId (s.Get ());
141  *pimpl = GetPointer (factory.Create<SimulatorImpl> ());
142  }
143  {
144  ObjectFactory factory;
145  StringValue s;
146  g_schedTypeImpl.GetValue (s);
147  factory.SetTypeId (s.Get ());
148  (*pimpl)->SetScheduler (factory);
149  }
150 
151 //
152 // Note: we call LogSetTimePrinter _after_ creating the implementation
153 // object because the act of creation can trigger calls to the logging
154 // framework which would call the TimePrinter function which would call
155 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
156 // in an infinite recursion until the stack explodes.
157 //
160  }
161  return *pimpl;
162 }
163 
164 void
166 {
168 
169  SimulatorImpl **pimpl = PeekImpl ();
170  if (*pimpl == 0)
171  {
172  return;
173  }
174  /* Note: we have to call LogSetTimePrinter (0) below because if we do not do
175  * this, and restart a simulation after this call to Destroy, (which is
176  * legal), Simulator::GetImpl will trigger again an infinite recursion until
177  * the stack explodes.
178  */
179  LogSetTimePrinter (0);
180  LogSetNodePrinter (0);
181  (*pimpl)->Destroy ();
182  (*pimpl)->Unref ();
183  *pimpl = 0;
184 }
185 
186 void
188 {
189  NS_LOG_FUNCTION (schedulerFactory);
190  GetImpl ()->SetScheduler (schedulerFactory);
191 }
192 
193 bool
195 {
197  return GetImpl ()->IsFinished ();
198 }
199 
200 void
202 {
205  GetImpl ()->Run ();
206 }
207 
208 void
210 {
212  NS_LOG_LOGIC ("stop");
213  GetImpl ()->Stop ();
214 }
215 
216 void
217 Simulator::Stop (Time const &delay)
218 {
219  NS_LOG_FUNCTION (delay);
220  GetImpl ()->Stop (delay);
221 }
222 
223 Time
225 {
226  /* Please, don't include any calls to logging macros in this function
227  * or pay the price, that is, stack explosions.
228  */
229  return GetImpl ()->Now ();
230 }
231 
232 Time
234 {
235  NS_LOG_FUNCTION (&id);
236  return GetImpl ()->GetDelayLeft (id);
237 }
238 
239 EventId
240 Simulator::Schedule (Time const &delay, const Ptr<EventImpl> &event)
241 {
242  return DoSchedule (delay, GetPointer (event));
243 }
244 
245 EventId
247 {
248  return DoScheduleNow (GetPointer (ev));
249 }
250 void
251 Simulator::ScheduleWithContext (uint32_t context, const Time &delay, EventImpl *impl)
252 {
253 #ifdef ENABLE_DES_METRICS
254  DesMetrics::Get ()->TraceWithContext (context, Now (), delay);
255 #endif
256  return GetImpl ()->ScheduleWithContext (context, delay, impl);
257 }
258 EventId
260 {
261  return DoScheduleDestroy (GetPointer (ev));
262 }
263 EventId
265 {
266 #ifdef ENABLE_DES_METRICS
267  DesMetrics::Get ()->Trace (Now (), time);
268 #endif
269  return GetImpl ()->Schedule (time, impl);
270 }
271 EventId
273 {
274 #ifdef ENABLE_DES_METRICS
275  DesMetrics::Get ()->Trace (Now (), Time (0));
276 #endif
277  return GetImpl ()->ScheduleNow (impl);
278 }
279 EventId
281 {
282  return GetImpl ()->ScheduleDestroy (impl);
283 }
284 
285 
286 EventId
287 Simulator::Schedule (Time const &delay, void (*f)(void))
288 {
289  return DoSchedule (delay, MakeEvent (f));
290 }
291 
292 void
293 Simulator::ScheduleWithContext (uint32_t context, Time const &delay, void (*f)(void))
294 {
295  return ScheduleWithContext (context, delay, MakeEvent (f));
296 }
297 
298 EventId
299 Simulator::ScheduleNow (void (*f)(void))
300 {
301  return DoScheduleNow (MakeEvent (f));
302 }
303 
304 EventId
306 {
307  return DoScheduleDestroy (MakeEvent (f));
308 }
309 
310 void
312 {
313  if (*PeekImpl () == 0)
314  {
315  return;
316  }
317  return GetImpl ()->Remove (id);
318 }
319 
320 void
322 {
323  if (*PeekImpl () == 0)
324  {
325  return;
326  }
327  return GetImpl ()->Cancel (id);
328 }
329 
330 bool
332 {
333  if (*PeekImpl () == 0)
334  {
335  return true;
336  }
337  return GetImpl ()->IsExpired (id);
338 }
339 
340 Time Now (void)
341 {
342  return Time (Simulator::Now ());
343 }
344 
345 Time
347 {
349  return GetImpl ()->GetMaximumSimulationTime ();
350 }
351 
352 uint32_t
354 {
355  return GetImpl ()->GetContext ();
356 }
357 
358 uint32_t
360 {
362 
363  if (*PeekImpl () != 0)
364  {
365  return GetImpl ()->GetSystemId ();
366  }
367  else
368  {
369  return 0;
370  }
371 }
372 
373 void
375 {
376  NS_LOG_FUNCTION (impl);
377  if (*PeekImpl () != 0)
378  {
379  NS_FATAL_ERROR ("It is not possible to set the implementation after calling any Simulator:: function. Call Simulator::SetImplementation earlier or after Simulator::Destroy.");
380  }
381  *PeekImpl () = GetPointer (impl);
382  // Set the default scheduler
383  ObjectFactory factory;
384  StringValue s;
385  g_schedTypeImpl.GetValue (s);
386  factory.SetTypeId (s.Get ());
387  impl->SetScheduler (factory);
388 //
389 // Note: we call LogSetTimePrinter _after_ creating the implementation
390 // object because the act of creation can trigger calls to the logging
391 // framework which would call the TimePrinter function which would call
392 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
393 // in an infinite recursion until the stack explodes.
394 //
397 }
398 
401 {
403  return GetImpl ();
404 }
405 
406 
407 
408 } // namespace ns3
409 
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:233
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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 "...
std::string Get(void) const
Definition: string.cc:31
ns3::EventImpl declarations.
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:374
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:280
static Ptr< SimulatorImpl > GetImplementation(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:400
String attribute value declarations.
Smart pointer implementation.
static uint32_t GetSystemId(void)
Get the system id of this simulator.
Definition: simulator.cc:359
virtual Time Now(void) const =0
Return the current simulation virtual time.
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:353
ns3::ObjectFactory class declaration.
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
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's associated function will not be invoked when it expires...
Definition: simulator.cc:321
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:719
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 'global value'.
Definition: global-value.h:70
virtual void Cancel(const EventId &id)=0
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static SimulatorImpl * GetImpl(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:127
static DesMetrics * Get(void)
Get a pointer to the singleton instance.
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.
Declaration of class ns3::SimulatorImpl.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
ns3::Scheduler abstract base class, ns3::Scheduler::Event and ns3::Scheduler::EventKey declarations...
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:568
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void LogSetNodePrinter(LogNodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:625
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
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:115
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...
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
static EventId DoSchedule(Time const &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:264
static void NodePrinter(std::ostream &os)
Default node id printer implementation.
Definition: simulator.cc:98
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
double f(double x, void *params)
Definition: 80211b.c:60
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:311
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:331
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
ns3::GlobalValue declaration.
static void TimePrinter(std::ostream &os)
Default TimePrinter implementation.
Definition: simulator.cc:86
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1401
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:187
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
Flag for events not associated with any particular context.
Definition: simulator.h:192
void LogSetTimePrinter(LogTimePrinter printer)
Set the LogTimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:611
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:1319
A simulation event.
Definition: event-impl.h:44
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:272
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:209
virtual void Stop(void)=0
Tell the Simulator the calling event should be the last one executed.
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:194
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Schedule an event to expire when Simulator::Destroy is called.
Definition: simulator.h:1492
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:340
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.
Declaration of ns3::MapScheduler class.
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:1202
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:346
The SimulatorImpl base class.
void GetValue(AttributeValue &value) const
Get the value.
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.