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
49namespace 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
54NS_LOG_COMPONENT_DEFINE ("Simulator");
55
64 ("SimulatorImplementationType",
65 "The object class to use as the simulator implementation",
66 StringValue ("ns3::DefaultSimulatorImpl"),
68
76static GlobalValue g_schedTypeImpl = GlobalValue ("SchedulerType",
77 "The object class to use as the scheduler implementation",
80
86static SimulatorImpl ** PeekImpl (void)
87{
88 static SimulatorImpl *impl = 0;
89 return &impl;
90}
91
98static SimulatorImpl * GetImpl (void)
99{
100 SimulatorImpl **pimpl = PeekImpl ();
101 /* Please, don't include any calls to logging macros in this function
102 * or pay the price, that is, stack explosions.
103 */
104 if (*pimpl == 0)
105 {
106 {
107 ObjectFactory factory;
108 StringValue s;
109
111 factory.SetTypeId (s.Get ());
112 *pimpl = GetPointer (factory.Create<SimulatorImpl> ());
113 }
114 {
115 ObjectFactory factory;
116 StringValue s;
118 factory.SetTypeId (s.Get ());
119 (*pimpl)->SetScheduler (factory);
120 }
121
122//
123// Note: we call LogSetTimePrinter _after_ creating the implementation
124// object because the act of creation can trigger calls to the logging
125// framework which would call the TimePrinter function which would call
126// Simulator::Now which would call Simulator::GetImpl, and, thus, get us
127// in an infinite recursion until the stack explodes.
128//
131 }
132 return *pimpl;
133}
134
135void
137{
139
140 SimulatorImpl **pimpl = PeekImpl ();
141 if (*pimpl == 0)
142 {
143 return;
144 }
145 /* Note: we have to call LogSetTimePrinter (0) below because if we do not do
146 * this, and restart a simulation after this call to Destroy, (which is
147 * legal), Simulator::GetImpl will trigger again an infinite recursion until
148 * the stack explodes.
149 */
152 (*pimpl)->Destroy ();
153 (*pimpl)->Unref ();
154 *pimpl = 0;
155}
156
157void
159{
160 NS_LOG_FUNCTION (schedulerFactory);
161 GetImpl ()->SetScheduler (schedulerFactory);
162}
163
164bool
166{
168 return GetImpl ()->IsFinished ();
169}
170
171void
173{
176 GetImpl ()->Run ();
177}
178
179void
181{
183 NS_LOG_LOGIC ("stop");
184 GetImpl ()->Stop ();
185}
186
187void
188Simulator::Stop (Time const &delay)
189{
190 NS_LOG_FUNCTION (delay);
191 GetImpl ()->Stop (delay);
192}
193
194Time
196{
197 /* Please, don't include any calls to logging macros in this function
198 * or pay the price, that is, stack explosions.
199 */
200 return GetImpl ()->Now ();
201}
202
203Time
205{
206 NS_LOG_FUNCTION (&id);
207 return GetImpl ()->GetDelayLeft (id);
208}
209
211Simulator::Schedule (Time const &delay, const Ptr<EventImpl> &event)
212{
213 return DoSchedule (delay, GetPointer (event));
214}
215
218{
219 return DoScheduleNow (GetPointer (ev));
220}
221void
223{
224#ifdef ENABLE_DES_METRICS
225 DesMetrics::Get ()->TraceWithContext (context, Now (), delay);
226#endif
227 return GetImpl ()->ScheduleWithContext (context, delay, impl);
228}
231{
232 return DoScheduleDestroy (GetPointer (ev));
233}
236{
237#ifdef ENABLE_DES_METRICS
238 DesMetrics::Get ()->Trace (Now (), time);
239#endif
240 return GetImpl ()->Schedule (time, impl);
241}
244{
245#ifdef ENABLE_DES_METRICS
246 DesMetrics::Get ()->Trace (Now (), Time (0));
247#endif
248 return GetImpl ()->ScheduleNow (impl);
249}
252{
253 return GetImpl ()->ScheduleDestroy (impl);
254}
255
256
257void
259{
260 if (*PeekImpl () == 0)
261 {
262 return;
263 }
264 return GetImpl ()->Remove (id);
265}
266
267void
269{
270 if (*PeekImpl () == 0)
271 {
272 return;
273 }
274 return GetImpl ()->Cancel (id);
275}
276
277bool
279{
280 if (*PeekImpl () == 0)
281 {
282 return true;
283 }
284 return GetImpl ()->IsExpired (id);
285}
286
287Time Now (void)
288{
289 return Time (Simulator::Now ());
290}
291
292Time
294{
296 return GetImpl ()->GetMaximumSimulationTime ();
297}
298
301{
302 return GetImpl ()->GetContext ();
303}
304
305uint64_t
307{
308 return GetImpl ()->GetEventCount ();
309}
310
313{
315
316 if (*PeekImpl () != 0)
317 {
318 return GetImpl ()->GetSystemId ();
319 }
320 else
321 {
322 return 0;
323 }
324}
325
326void
328{
329 NS_LOG_FUNCTION (impl);
330 if (*PeekImpl () != 0)
331 {
332 NS_FATAL_ERROR ("It is not possible to set the implementation after calling any Simulator:: function. Call Simulator::SetImplementation earlier or after Simulator::Destroy.");
333 }
334 *PeekImpl () = GetPointer (impl);
335 // Set the default scheduler
336 ObjectFactory factory;
337 StringValue s;
339 factory.SetTypeId (s.Get ());
340 impl->SetScheduler (factory);
341//
342// Note: we call LogSetTimePrinter _after_ creating the implementation
343// object because the act of creation can trigger calls to the logging
344// framework which would call the TimePrinter function which would call
345// Simulator::Now which would call Simulator::GetImpl, and, thus, get us
346// in an infinite recursion until the stack explodes.
347//
350}
351
354{
356 return GetImpl ();
357}
358
359
360
361} // namespace ns3
362
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
Definition: des-metrics.cc:102
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:108
An identifier for simulation events.
Definition: event-id.h:54
A simulation event.
Definition: event-impl.h:45
Hold a so-called 'global value'.
Definition: global-value.h:74
void GetValue(AttributeValue &value) const
Get the value.
static TypeId GetTypeId(void)
Register this type.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition: simulator.cc:251
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
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static EventId DoSchedule(Time const &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:235
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
Definition: simulator.cc:278
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:300
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition: simulator.h:605
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:158
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:243
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:165
static Time GetMaximumSimulationTime(void)
Get the maximum representable simulation time.
Definition: simulator.cc:293
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
static uint32_t GetSystemId(void)
Get the system id of this simulator.
Definition: simulator.cc:312
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:327
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:204
static uint64_t GetEventCount(void)
Get the number of events executed.
Definition: simulator.cc:306
static Ptr< SimulatorImpl > GetImplementation(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:353
The SimulatorImpl base class.
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...
virtual Time GetMaximumSimulationTime(void) const =0
Get the maximum representable simulation time.
virtual Time GetDelayLeft(const EventId &id) const =0
Get the remaining time until this event will execute.
virtual void SetScheduler(ObjectFactory schedulerFactory)=0
Set the Scheduler to be used to manage the event list.
virtual uint32_t GetSystemId() const =0
Get the system id of this simulator.
virtual EventId Schedule(const Time &delay, EventImpl *event)=0
Schedule a future event execution (in the same context).
virtual bool IsExpired(const EventId &id) const =0
Check if an event has already run or been cancelled.
virtual void Run(void)=0
Run the simulation.
virtual void Remove(const EventId &id)=0
Remove an event from the event list.
virtual void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event)=0
Schedule a future event execution (in a different context).
virtual uint32_t GetContext(void) const =0
Get the current simulation context.
virtual EventId ScheduleNow(EventImpl *event)=0
Schedule an event to run at the current virtual time.
virtual Time Now(void) const =0
Return the current simulation virtual time.
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...
virtual void Stop(void)=0
Tell the Simulator the calling event should be the last one executed.
virtual bool IsFinished(void) const =0
Check if the simulation should finish.
virtual uint64_t GetEventCount(void) const =0
Get the number of events executed.
static DesMetrics * Get(void)
Get a pointer to the singleton instance.
Definition: singleton.h:100
Hold variables of type string.
Definition: string.h:41
std::string Get(void) const
Definition: string.cc:31
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
static void ClearMarkedTimes()
Remove all MarkedTimes.
Definition: time.cc:300
AttributeValue implementation for TypeId.
Definition: type-id.h:595
ns3::DesMetrics declaration.
ns3::EventImpl declarations.
ns3::GlobalValue declaration.
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1226
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
void DefaultNodePrinter(std::ostream &os)
Default node id printer implementation.
Definition: node-printer.cc:38
static GlobalValue g_schedTypeImpl
The specific event scheduler implementation to use.
Definition: simulator.cc:76
static GlobalValue g_simTypeImpl
The specific simulator implementation to use.
Definition: simulator.cc:63
static SimulatorImpl ** PeekImpl(void)
Get the static SimulatorImpl instance.
Definition: simulator.cc:86
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
static SimulatorImpl * GetImpl(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:98
Debug message logging.
ns3::MapScheduler declaration.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:793
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition: log.cc:620
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:421
void DefaultTimePrinter(std::ostream &os)
Default Time printer.
Definition: time-printer.cc:39
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:634
ns3::ObjectFactory class declaration.
ns3::Ptr smart pointer declaration and implementation.
ns3::Scheduler abstract base class, ns3::Scheduler::Event and ns3::Scheduler::EventKey declarations.
ns3::SimulatorImpl declaration.
ns3::Simulator declaration.
ns3::StringValue attribute value declarations.