A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
40 // Note: Logging in this file is largely avoided due to the
41 // number of calls that are made to these functions and the possibility
42 // of causing recursions leading to stack overflow
43 
44 NS_LOG_COMPONENT_DEFINE ("Simulator");
45 
46 namespace ns3 {
47 
48 GlobalValue g_simTypeImpl = GlobalValue ("SimulatorImplementationType",
49  "The object class to use as the simulator implementation",
50  StringValue ("ns3::DefaultSimulatorImpl"),
51  MakeStringChecker ());
52 
54  "The object class to use as the scheduler implementation",
56  MakeTypeIdChecker ());
57 
58 static void
59 TimePrinter (std::ostream &os)
60 {
61  os << Simulator::Now ().GetSeconds () << "s";
62 }
63 
64 static void
65 NodePrinter (std::ostream &os)
66 {
67  if (Simulator::GetContext () == 0xffffffff)
68  {
69  os << "-1";
70  }
71  else
72  {
73  os << Simulator::GetContext ();
74  }
75 }
76 
77 static SimulatorImpl **PeekImpl (void)
78 {
79  static SimulatorImpl *impl = 0;
80  return &impl;
81 }
82 
83 static SimulatorImpl * GetImpl (void)
84 {
85  SimulatorImpl **pimpl = PeekImpl ();
86  /* Please, don't include any calls to logging macros in this function
87  * or pay the price, that is, stack explosions.
88  */
89  if (*pimpl == 0)
90  {
91  {
92  ObjectFactory factory;
93  StringValue s;
94 
96  factory.SetTypeId (s.Get ());
97  *pimpl = GetPointer (factory.Create<SimulatorImpl> ());
98  }
99  {
100  ObjectFactory factory;
101  StringValue s;
103  factory.SetTypeId (s.Get ());
104  (*pimpl)->SetScheduler (factory);
105  }
106 
107 //
108 // Note: we call LogSetTimePrinter _after_ creating the implementation
109 // object because the act of creation can trigger calls to the logging
110 // framework which would call the TimePrinter function which would call
111 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
112 // in an infinite recursion until the stack explodes.
113 //
116  }
117  return *pimpl;
118 }
119 
120 void
122 {
124 
125  SimulatorImpl **pimpl = PeekImpl ();
126  if (*pimpl == 0)
127  {
128  return;
129  }
130  /* Note: we have to call LogSetTimePrinter (0) below because if we do not do
131  * this, and restart a simulation after this call to Destroy, (which is
132  * legal), Simulator::GetImpl will trigger again an infinite recursion until
133  * the stack explodes.
134  */
135  LogSetTimePrinter (0);
136  LogSetNodePrinter (0);
137  (*pimpl)->Destroy ();
138  (*pimpl)->Unref ();
139  *pimpl = 0;
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (schedulerFactory);
146  GetImpl ()->SetScheduler (schedulerFactory);
147 }
148 
149 bool
151 {
153  return GetImpl ()->IsFinished ();
154 }
155 
156 void
158 {
161  GetImpl ()->Run ();
162 }
163 
164 void
166 {
168  NS_LOG_LOGIC ("stop");
169  GetImpl ()->Stop ();
170 }
171 
172 void
173 Simulator::Stop (Time const &time)
174 {
175  NS_LOG_FUNCTION (time);
176  GetImpl ()->Stop (time);
177 }
178 
179 Time
181 {
182  /* Please, don't include any calls to logging macros in this function
183  * or pay the price, that is, stack explosions.
184  */
185  return GetImpl ()->Now ();
186 }
187 
188 Time
190 {
191  NS_LOG_FUNCTION (&id);
192  return GetImpl ()->GetDelayLeft (id);
193 }
194 
195 EventId
196 Simulator::Schedule (Time const &time, const Ptr<EventImpl> &ev)
197 {
198  return DoSchedule (time, GetPointer (ev));
199 }
200 
201 EventId
203 {
204  return DoScheduleNow (GetPointer (ev));
205 }
206 void
207 Simulator::ScheduleWithContext (uint32_t context, const Time &time, EventImpl *impl)
208 {
209  return GetImpl ()->ScheduleWithContext (context, time, impl);
210 }
211 EventId
213 {
214  return DoScheduleDestroy (GetPointer (ev));
215 }
216 EventId
218 {
219  return GetImpl ()->Schedule (time, impl);
220 }
221 EventId
223 {
224  return GetImpl ()->ScheduleNow (impl);
225 }
226 EventId
228 {
229  return GetImpl ()->ScheduleDestroy (impl);
230 }
231 
232 
233 EventId
234 Simulator::Schedule (Time const &time, void (*f)(void))
235 {
236  return DoSchedule (time, MakeEvent (f));
237 }
238 
239 void
240 Simulator::ScheduleWithContext (uint32_t context, Time const &time, void (*f)(void))
241 {
242  return ScheduleWithContext (context, time, MakeEvent (f));
243 }
244 
245 EventId
246 Simulator::ScheduleNow (void (*f)(void))
247 {
248  return DoScheduleNow (MakeEvent (f));
249 }
250 
251 EventId
252 Simulator::ScheduleDestroy (void (*f)(void))
253 {
254  return DoScheduleDestroy (MakeEvent (f));
255 }
256 
257 void
259 {
260  if (*PeekImpl () == 0)
261  {
262  return;
263  }
264  return GetImpl ()->Remove (ev);
265 }
266 
267 void
269 {
270  if (*PeekImpl () == 0)
271  {
272  return;
273  }
274  return GetImpl ()->Cancel (ev);
275 }
276 
277 bool
279 {
280  if (*PeekImpl () == 0)
281  {
282  return true;
283  }
284  return GetImpl ()->IsExpired (id);
285 }
286 
287 Time Now (void)
288 {
289  return Time (Simulator::Now ());
290 }
291 
292 Time
294 {
296  return GetImpl ()->GetMaximumSimulationTime ();
297 }
298 
299 uint32_t
301 {
302  return GetImpl ()->GetContext ();
303 }
304 
305 uint32_t
307 {
309 
310  if (*PeekImpl () != 0)
311  {
312  return GetImpl ()->GetSystemId ();
313  }
314  else
315  {
316  return 0;
317  }
318 }
319 
320 void
322 {
323  NS_LOG_FUNCTION (impl);
324  if (*PeekImpl () != 0)
325  {
326  NS_FATAL_ERROR ("It is not possible to set the implementation after calling any Simulator:: function. Call Simulator::SetImplementation earlier or after Simulator::Destroy.");
327  }
328  *PeekImpl () = GetPointer (impl);
329  // Set the default scheduler
330  ObjectFactory factory;
331  StringValue s;
333  factory.SetTypeId (s.Get ());
334  impl->SetScheduler (factory);
335 //
336 // Note: we call LogSetTimePrinter _after_ creating the implementation
337 // object because the act of creation can trigger calls to the logging
338 // framework which would call the TimePrinter function which would call
339 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
340 // in an infinite recursion until the stack explodes.
341 //
344 }
347 {
349  return GetImpl ();
350 }
351 
352 
353 
354 } // namespace ns3
355 
static Time GetDelayLeft(const EventId &id)
Definition: simulator.cc:189
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
virtual void ScheduleWithContext(uint32_t context, Time const &time, EventImpl *event)=0
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
void LogSetNodePrinter(LogNodePrinter printer)
Definition: log.cc:572
std::string Get(void) const
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:321
virtual bool IsExpired(const EventId &ev) const =0
This method has O(1) complexity.
hold variables of type string
Definition: string.h:19
static EventId DoScheduleDestroy(EventImpl *event)
Definition: simulator.cc:227
static Ptr< SimulatorImpl > GetImplementation(void)
Definition: simulator.cc:346
static uint32_t GetSystemId(void)
Definition: simulator.cc:306
virtual Time Now(void) const =0
Return the "current simulation time".
static uint32_t GetContext(void)
Definition: simulator.cc:300
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
static EventId DoSchedule(Time const &time, EventImpl *event)
Definition: simulator.cc:217
virtual EventId ScheduleNow(EventImpl *event)=0
void SetTypeId(TypeId tid)
T * GetPointer(const Ptr< T > &p)
Definition: ptr.h:285
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:268
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
GlobalValue g_simTypeImpl
Definition: simulator.cc:48
void LogSetTimePrinter(LogTimePrinter printer)
Definition: log.cc:558
virtual void SetScheduler(ObjectFactory schedulerFactory)=0
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
hold a so-called 'global value'.
Definition: global-value.h:47
virtual void Run(void)=0
Run the simulation until one of:
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
virtual Time GetMaximumSimulationTime(void) const =0
virtual bool IsFinished(void) const =0
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
double GetSeconds(void) const
Definition: nstime.h:274
static void TimePrinter(std::ostream &os)
Definition: simulator.cc:59
GlobalValue g_schedTypeImpl
Definition: simulator.cc:53
Ptr< Object > Create(void) const
virtual Time GetDelayLeft(const EventId &id) const =0
Ptr< SampleEmitter > s
hold objects of type ns3::TypeId
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:904
virtual EventId ScheduleDestroy(EventImpl *event)=0
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
virtual void Cancel(const EventId &ev)=0
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static bool IsExpired(const EventId &id)
This method has O(1) complexity.
Definition: simulator.cc:278
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
NS_LOG_COMPONENT_DEFINE("Simulator")
static void SetScheduler(ObjectFactory schedulerFactory)
Definition: simulator.cc:143
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
static SimulatorImpl ** PeekImpl(void)
Definition: simulator.cc:77
static SimulatorImpl * GetImpl(void)
Definition: simulator.cc:83
virtual EventId Schedule(Time const &time, EventImpl *event)=0
instantiate subclasses of ns3::Object.
a simulation event
Definition: event-impl.h:39
static EventId DoScheduleNow(EventImpl *event)
Definition: simulator.cc:222
virtual void Remove(const EventId &ev)=0
Remove an event from the event list.
an identifier for simulation events.
Definition: event-id.h:46
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
virtual void Stop(void)=0
If an event invokes this method, it will be the last event scheduled by the Simulator::Run method bef...
static bool IsFinished(void)
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
Definition: simulator.cc:150
EventImpl * MakeEvent(void(*f)(void))
Definition: make-event.cc:8
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Schedule an event to expire at Destroy time.
Definition: simulator.h:1076
virtual uint32_t GetSystemId() const =0
static void ClearMarkedTimes()
Remove all MarkedTimes.
Definition: time.cc:251
virtual uint32_t GetContext(void) const =0
static void NodePrinter(std::ostream &os)
Definition: simulator.cc:65
static Time GetMaximumSimulationTime(void)
Definition: simulator.cc:293
Doxygen introspection did not find any typical Config paths.
void GetValue(AttributeValue &value) const
static TypeId GetTypeId(void)