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 
40 namespace ns3 {
41 
42 class SimulatorImpl;
43 class Scheduler;
44 
68 class Simulator
69 {
70 public:
83  static void SetImplementation (Ptr<SimulatorImpl> impl);
84 
103 
112  static void SetScheduler (ObjectFactory schedulerFactory);
113 
123  static void Destroy (void);
124 
134  static bool IsFinished (void);
135 
146  static void Run (void);
147 
156  static void Stop (void);
157 
167  static void Stop (const Time &delay);
168 
185  static uint32_t GetContext (void);
186 
194  enum : uint32_t
195  {
199  NO_CONTEXT = 0xffffffff
200  };
201 
206  static uint64_t GetEventCount (void);
207 
208 
229  template <typename FUNC,
230  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
231  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type = 0,
232  typename... Ts>
233  static EventId Schedule (Time const &delay, FUNC f, Ts&&... args);
234 
250  template <typename... Us, typename... Ts>
251  static EventId Schedule (Time const &delay, void (*f)(Us...), Ts&&... args); // Schedule events (in the same context) to run at a future time.
253 
275  template <typename FUNC,
276  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
277  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type = 0,
278  typename... Ts>
279  static void ScheduleWithContext (uint32_t context, Time const &delay, FUNC f, Ts&&... args);
280 
293  template <typename... Us, typename... Ts>
294  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.
296 
315  template <typename FUNC,
316  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
317  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type = 0,
318  typename... Ts>
319  static EventId ScheduleNow (FUNC f, Ts&&... args);
320 
332  template <typename... Us, typename... Ts>
333  static EventId ScheduleNow (void (*f)(Us...), Ts&&... args);
334  // Schedule events (in the same context) to run now.
336 
356  template <typename FUNC,
357  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type = 0,
358  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type = 0,
359  typename... Ts>
360  static EventId ScheduleDestroy (FUNC f, Ts&&... args);
361 
374  template <typename... Us, typename... Ts>
375  static EventId ScheduleDestroy (void (*f)(Us...), Ts&&... args);
376  // Schedule events to run when Simulator:Destroy() is called.
378 
391  static void Remove (const EventId &id);
392 
406  static void Cancel (const EventId &id);
407 
422  static bool IsExpired (const EventId &id);
423 
429  static Time Now (void);
430 
439  static Time GetDelayLeft (const EventId &id);
440 
449  static Time GetMaximumSimulationTime (void);
450 
458  static EventId Schedule (const Time &delay, const Ptr<EventImpl> &event);
459 
468  static void ScheduleWithContext (uint32_t context, const Time &delay, EventImpl *event);
469 
477  static EventId ScheduleDestroy (const Ptr<EventImpl> &event);
478 
485  static EventId ScheduleNow (const Ptr<EventImpl> &event);
486 
494  static uint32_t GetSystemId (void);
495 
496 private:
501 
508  static EventId DoSchedule (Time const &delay, EventImpl *event);
514  static EventId DoScheduleNow (EventImpl *event);
520  static EventId DoScheduleDestroy (EventImpl *event);
521 
522 }; // class Simulator
523 
537 Time Now (void);
538 
539 } // namespace ns3
540 
541 
542 /********************************************************************
543  * Implementation of the templates declared above.
544  ********************************************************************/
545 
546 namespace ns3 {
547 
548 // Doxygen has trouble with static template functions in a class:
549 // it treats the in-class declaration as different from the
550 // out of class definition, so makes two entries in the member list. Ugh
551 
552 template <typename FUNC,
553  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
554  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
555  typename... Ts>
556 EventId Simulator::Schedule (Time const &delay, FUNC f, Ts&&... args)
557 {
558  return DoSchedule (delay, MakeEvent (f, std::forward<Ts> (args)...));
559 }
560 
561 template <typename... Us, typename... Ts>
562 EventId Simulator::Schedule (Time const &delay, void (*f)(Us...), Ts&&... args)
563 {
564  return DoSchedule (delay, MakeEvent (f, std::forward<Ts> (args)...));
565 }
566 
567 template <typename FUNC,
568  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
569  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
570  typename... Ts>
571 void Simulator::ScheduleWithContext (uint32_t context, Time const &delay, FUNC f, Ts&&... args)
572 {
573  return ScheduleWithContext (context, delay, MakeEvent (f, std::forward<Ts> (args)...));
574 }
575 
576 template <typename... Us, typename... Ts>
577 void Simulator::ScheduleWithContext (uint32_t context, Time const &delay, void (*f)(Us...), Ts&&... args)
578 {
579  return ScheduleWithContext (context, delay, MakeEvent (f, std::forward<Ts> (args)...));
580 }
581 
582 template <typename FUNC,
583  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
584  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
585  typename... Ts>
586 EventId
587 Simulator::ScheduleNow (FUNC f, Ts&&... args)
588 {
589  return DoScheduleNow (MakeEvent (f, std::forward<Ts> (args)...));
590 }
591 
592 template <typename... Us, typename... Ts>
593 EventId
594 Simulator::ScheduleNow (void (*f)(Us...), Ts&&... args)
595 {
596  return DoScheduleNow (MakeEvent (f, std::forward<Ts> (args)...));
597 }
598 
599 
600 template <typename FUNC,
601  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
602  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
603  typename... Ts>
604 EventId
605 Simulator::ScheduleDestroy (FUNC f, Ts&&... args)
606 {
607  return DoScheduleDestroy (MakeEvent (f, std::forward<Ts> (args)...));
608 }
609 
610 template <typename... Us, typename... Ts>
611 EventId
612 Simulator::ScheduleDestroy (void (*f)(Us...), Ts&&... args)
613 {
614  return DoScheduleDestroy (MakeEvent (f, std::forward<Ts> (args)...));
615 }
616 
617 } // namespace ns3
618 
619 #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
Simulator()
Default constructor.
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
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:199
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:327
~Simulator()
Destructor.
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:103
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.
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.