A Discrete-Event Network Simulator
API
simulator.h
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2005 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
21#ifndef SIMULATOR_H
22#define SIMULATOR_H
23
24#include "event-id.h"
25#include "event-impl.h"
26#include "make-event.h"
27#include "nstime.h"
28
29#include "object-factory.h"
30
31#include <stdint.h>
32#include <string>
33
40namespace ns3 {
41
42class SimulatorImpl;
43class Scheduler;
44
69{
70public:
71 // Delete default constructor and destructor to avoid misuse
72 Simulator () = delete;
73 ~Simulator () = delete;
74
87 static void SetImplementation (Ptr<SimulatorImpl> impl);
88
107
116 static void SetScheduler (ObjectFactory schedulerFactory);
117
127 static void Destroy (void);
128
138 static bool IsFinished (void);
139
150 static void Run (void);
151
160 static void Stop (void);
161
171 static void Stop (const Time &delay);
172
189 static uint32_t GetContext (void);
190
198 enum : uint32_t
199 {
203 NO_CONTEXT = 0xffffffff
204 };
205
210 static uint64_t GetEventCount (void);
211
212
233 template <typename FUNC,
234 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
236 typename... Ts>
237 static EventId Schedule (Time const &delay, FUNC f, Ts&&... args);
238
254 template <typename... Us, typename... Ts>
255 static EventId Schedule (Time const &delay, void (*f)(Us...), Ts&&... args); // Schedule events (in the same context) to run at a future time.
257
279 template <typename FUNC,
280 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
282 typename... Ts>
283 static void ScheduleWithContext (uint32_t context, Time const &delay, FUNC f, Ts&&... args);
284
297 template <typename... Us, typename... Ts>
298 static void ScheduleWithContext (uint32_t context, Time const &delay, void (*f)(Us...), Ts&&... args); // Schedule events (in a different context) to run now or at a future time.
300
319 template <typename FUNC,
320 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
322 typename... Ts>
323 static EventId ScheduleNow (FUNC f, Ts&&... args);
324
336 template <typename... Us, typename... Ts>
337 static EventId ScheduleNow (void (*f)(Us...), Ts&&... args);
338 // Schedule events (in the same context) to run now.
340
360 template <typename FUNC,
361 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
363 typename... Ts>
364 static EventId ScheduleDestroy (FUNC f, Ts&&... args);
365
378 template <typename... Us, typename... Ts>
379 static EventId ScheduleDestroy (void (*f)(Us...), Ts&&... args);
380 // Schedule events to run when Simulator:Destroy() is called.
382
395 static void Remove (const EventId &id);
396
410 static void Cancel (const EventId &id);
411
426 static bool IsExpired (const EventId &id);
427
433 static Time Now (void);
434
443 static Time GetDelayLeft (const EventId &id);
444
453 static Time GetMaximumSimulationTime (void);
454
462 static EventId Schedule (const Time &delay, const Ptr<EventImpl> &event);
463
472 static void ScheduleWithContext (uint32_t context, const Time &delay, EventImpl *event);
473
481 static EventId ScheduleDestroy (const Ptr<EventImpl> &event);
482
489 static EventId ScheduleNow (const Ptr<EventImpl> &event);
490
498 static uint32_t GetSystemId (void);
499
500private:
507 static EventId DoSchedule (Time const &delay, EventImpl *event);
513 static EventId DoScheduleNow (EventImpl *event);
519 static EventId DoScheduleDestroy (EventImpl *event);
520
521}; // class Simulator
522
536Time Now (void);
537
538} // namespace ns3
539
540
541/********************************************************************
542 * Implementation of the templates declared above.
543 ********************************************************************/
544
545namespace ns3 {
546
547// Doxygen has trouble with static template functions in a class:
548// it treats the in-class declaration as different from the
549// out of class definition, so makes two entries in the member list. Ugh
550
551template <typename FUNC,
552 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
554 typename... Ts>
555EventId Simulator::Schedule (Time const &delay, FUNC f, Ts&&... args)
556{
557 return DoSchedule (delay, MakeEvent (f, std::forward<Ts> (args)...));
558}
559
560template <typename... Us, typename... Ts>
561EventId Simulator::Schedule (Time const &delay, void (*f)(Us...), Ts&&... args)
562{
563 return DoSchedule (delay, MakeEvent (f, std::forward<Ts> (args)...));
564}
565
566template <typename FUNC,
567 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
569 typename... Ts>
570void Simulator::ScheduleWithContext (uint32_t context, Time const &delay, FUNC f, Ts&&... args)
571{
572 return ScheduleWithContext (context, delay, MakeEvent (f, std::forward<Ts> (args)...));
573}
574
575template <typename... Us, typename... Ts>
576void Simulator::ScheduleWithContext (uint32_t context, Time const &delay, void (*f)(Us...), Ts&&... args)
577{
578 return ScheduleWithContext (context, delay, MakeEvent (f, std::forward<Ts> (args)...));
579}
580
581template <typename FUNC,
582 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
584 typename... Ts>
587{
588 return DoScheduleNow (MakeEvent (f, std::forward<Ts> (args)...));
589}
590
591template <typename... Us, typename... Ts>
593Simulator::ScheduleNow (void (*f)(Us...), Ts&&... args)
594{
595 return DoScheduleNow (MakeEvent (f, std::forward<Ts> (args)...));
596}
597
598
599template <typename FUNC,
600 typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
602 typename... Ts>
605{
606 return DoScheduleDestroy (MakeEvent (f, std::forward<Ts> (args)...));
607}
608
609template <typename... Us, typename... Ts>
611Simulator::ScheduleDestroy (void (*f)(Us...), Ts&&... args)
612{
613 return DoScheduleDestroy (MakeEvent (f, std::forward<Ts> (args)...));
614}
615
616} // namespace ns3
617
618#endif /* SIMULATOR_H */
double f(double x, void *params)
Definition: 80211b.c:70
An identifier for simulation events.
Definition: event-id.h:54
A simulation event.
Definition: event-impl.h:45
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
Control the scheduling of simulation events.
Definition: simulator.h:69
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:555
static EventId DoSchedule(Time const &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:235
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:203
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:570
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
Simulator()=delete
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:604
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:586
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
~Simulator()=delete
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
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::EventId declarations.
ns3::EventImpl declarations.
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:34
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
ns3::MakeEvent function declarations and template implementation.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:794
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::ObjectFactory class declaration.