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 
27 #include "ptr.h"
28 #include "string.h"
29 #include "object-factory.h"
30 #include "global-value.h"
31 #include "assert.h"
32 #include "log.h"
33 
34 #include <cmath>
35 #include <fstream>
36 #include <list>
37 #include <vector>
38 #include <iostream>
39 
48 namespace ns3 {
49 
50 // Note: Logging in this file is largely avoided due to the
51 // number of calls that are made to these functions and the possibility
52 // of causing recursions leading to stack overflow
53 NS_LOG_COMPONENT_DEFINE ("Simulator");
54 
62  ("SimulatorImplementationType",
63  "The object class to use as the simulator implementation",
64  StringValue ("ns3::DefaultSimulatorImpl"),
66 
73 static GlobalValue g_schedTypeImpl = GlobalValue ("SchedulerType",
74  "The object class to use as the scheduler implementation",
77 
84 static void
85 TimePrinter (std::ostream &os)
86 {
87  os << Simulator::Now ().GetSeconds () << "s";
88 }
89 
96 static void
97 NodePrinter (std::ostream &os)
98 {
99  if (Simulator::GetContext () == 0xffffffff)
100  {
101  os << "-1";
102  }
103  else
104  {
105  os << Simulator::GetContext ();
106  }
107 }
108 
114 static SimulatorImpl **PeekImpl (void)
115 {
116  static SimulatorImpl *impl = 0;
117  return &impl;
118 }
119 
126 static SimulatorImpl * GetImpl (void)
127 {
128  SimulatorImpl **pimpl = PeekImpl ();
129  /* Please, don't include any calls to logging macros in this function
130  * or pay the price, that is, stack explosions.
131  */
132  if (*pimpl == 0)
133  {
134  {
135  ObjectFactory factory;
136  StringValue s;
137 
138  g_simTypeImpl.GetValue (s);
139  factory.SetTypeId (s.Get ());
140  *pimpl = GetPointer (factory.Create<SimulatorImpl> ());
141  }
142  {
143  ObjectFactory factory;
144  StringValue s;
145  g_schedTypeImpl.GetValue (s);
146  factory.SetTypeId (s.Get ());
147  (*pimpl)->SetScheduler (factory);
148  }
149 
150 //
151 // Note: we call LogSetTimePrinter _after_ creating the implementation
152 // object because the act of creation can trigger calls to the logging
153 // framework which would call the TimePrinter function which would call
154 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
155 // in an infinite recursion until the stack explodes.
156 //
159  }
160  return *pimpl;
161 }
162 
163 void
165 {
167 
168  SimulatorImpl **pimpl = PeekImpl ();
169  if (*pimpl == 0)
170  {
171  return;
172  }
173  /* Note: we have to call LogSetTimePrinter (0) below because if we do not do
174  * this, and restart a simulation after this call to Destroy, (which is
175  * legal), Simulator::GetImpl will trigger again an infinite recursion until
176  * the stack explodes.
177  */
178  LogSetTimePrinter (0);
179  LogSetNodePrinter (0);
180  (*pimpl)->Destroy ();
181  (*pimpl)->Unref ();
182  *pimpl = 0;
183 }
184 
185 void
187 {
188  NS_LOG_FUNCTION (schedulerFactory);
189  GetImpl ()->SetScheduler (schedulerFactory);
190 }
191 
192 bool
194 {
196  return GetImpl ()->IsFinished ();
197 }
198 
199 void
201 {
204  GetImpl ()->Run ();
205 }
206 
207 void
209 {
211  NS_LOG_LOGIC ("stop");
212  GetImpl ()->Stop ();
213 }
214 
215 void
216 Simulator::Stop (Time const &delay)
217 {
218  NS_LOG_FUNCTION (delay);
219  GetImpl ()->Stop (delay);
220 }
221 
222 Time
224 {
225  /* Please, don't include any calls to logging macros in this function
226  * or pay the price, that is, stack explosions.
227  */
228  return GetImpl ()->Now ();
229 }
230 
231 Time
233 {
234  NS_LOG_FUNCTION (&id);
235  return GetImpl ()->GetDelayLeft (id);
236 }
237 
238 EventId
239 Simulator::Schedule (Time const &delay, const Ptr<EventImpl> &event)
240 {
241  return DoSchedule (delay, GetPointer (event));
242 }
243 
244 EventId
246 {
247  return DoScheduleNow (GetPointer (ev));
248 }
249 void
250 Simulator::ScheduleWithContext (uint32_t context, const Time &delay, EventImpl *impl)
251 {
252  return GetImpl ()->ScheduleWithContext (context, delay, impl);
253 }
254 EventId
256 {
257  return DoScheduleDestroy (GetPointer (ev));
258 }
259 EventId
261 {
262  return GetImpl ()->Schedule (time, impl);
263 }
264 EventId
266 {
267  return GetImpl ()->ScheduleNow (impl);
268 }
269 EventId
271 {
272  return GetImpl ()->ScheduleDestroy (impl);
273 }
274 
275 
276 EventId
277 Simulator::Schedule (Time const &delay, void (*f)(void))
278 {
279  return DoSchedule (delay, MakeEvent (f));
280 }
281 
282 void
283 Simulator::ScheduleWithContext (uint32_t context, Time const &delay, void (*f)(void))
284 {
285  return ScheduleWithContext (context, delay, MakeEvent (f));
286 }
287 
288 EventId
289 Simulator::ScheduleNow (void (*f)(void))
290 {
291  return DoScheduleNow (MakeEvent (f));
292 }
293 
294 EventId
295 Simulator::ScheduleDestroy (void (*f)(void))
296 {
297  return DoScheduleDestroy (MakeEvent (f));
298 }
299 
300 void
302 {
303  if (*PeekImpl () == 0)
304  {
305  return;
306  }
307  return GetImpl ()->Remove (id);
308 }
309 
310 void
312 {
313  if (*PeekImpl () == 0)
314  {
315  return;
316  }
317  return GetImpl ()->Cancel (id);
318 }
319 
320 bool
322 {
323  if (*PeekImpl () == 0)
324  {
325  return true;
326  }
327  return GetImpl ()->IsExpired (id);
328 }
329 
330 Time Now (void)
331 {
332  return Time (Simulator::Now ());
333 }
334 
335 Time
337 {
339  return GetImpl ()->GetMaximumSimulationTime ();
340 }
341 
342 uint32_t
344 {
345  return GetImpl ()->GetContext ();
346 }
347 
348 uint32_t
350 {
352 
353  if (*PeekImpl () != 0)
354  {
355  return GetImpl ()->GetSystemId ();
356  }
357  else
358  {
359  return 0;
360  }
361 }
362 
363 void
365 {
366  NS_LOG_FUNCTION (impl);
367  if (*PeekImpl () != 0)
368  {
369  NS_FATAL_ERROR ("It is not possible to set the implementation after calling any Simulator:: function. Call Simulator::SetImplementation earlier or after Simulator::Destroy.");
370  }
371  *PeekImpl () = GetPointer (impl);
372  // Set the default scheduler
373  ObjectFactory factory;
374  StringValue s;
375  g_schedTypeImpl.GetValue (s);
376  factory.SetTypeId (s.Get ());
377  impl->SetScheduler (factory);
378 //
379 // Note: we call LogSetTimePrinter _after_ creating the implementation
380 // object because the act of creation can trigger calls to the logging
381 // framework which would call the TimePrinter function which would call
382 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
383 // in an infinite recursion until the stack explodes.
384 //
387 }
388 
391 {
393  return GetImpl ();
394 }
395 
396 
397 
398 } // namespace ns3
399 
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:232
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:364
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:270
static Ptr< SimulatorImpl > GetImplementation(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:390
String attribute value declarations.
Smart pointer implementation.
static uint32_t GetSystemId(void)
Get the system id of this simulator.
Definition: simulator.cc:349
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:343
ns3::ObjectFactory class declaration.
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#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:145
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:311
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 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:54
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:126
virtual EventId Schedule(Time const &delay, EventImpl *event)=0
Schedule a future event execution (in the same context).
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:1216
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:624
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:548
static SimulatorImpl ** PeekImpl(void)
Get the static SimulatorImpl instance.
Definition: simulator.cc:114
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:260
static void NodePrinter(std::ostream &os)
Default node id printer implementation.
Definition: simulator.cc:97
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:301
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:321
ns3::GlobalValue declaration.
static void TimePrinter(std::ostream &os)
Default TimePrinter implementation.
Definition: simulator.cc:85
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1379
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:186
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
void LogSetTimePrinter(LogTimePrinter printer)
Set the LogTimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:610
static GlobalValue g_schedTypeImpl
The specific event scheduler implementation to use.
Definition: simulator.cc:73
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:1297
A simulation event.
Definition: event-impl.h:44
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:265
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:208
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:193
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Schedule an event to expire when Simulator::Destroy is called.
Definition: simulator.h:1470
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:330
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:61
Debug message logging.
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1081
virtual uint32_t GetContext(void) const =0
Get the current simulation context.
virtual void ScheduleWithContext(uint32_t context, Time const &delay, EventImpl *event)=0
Schedule a future event execution (in a different context).
static Time GetMaximumSimulationTime(void)
Get the maximum representable simulation time.
Definition: simulator.cc:336
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.