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 
469  static void ScheduleWithContext (uint32_t context, const Time &delay, EventImpl *event);
470 
478  static EventId ScheduleDestroy (const Ptr<EventImpl> &event);
479 
486  static EventId ScheduleNow (const Ptr<EventImpl> &event);
487 
495  static uint32_t GetSystemId (void);
496 
497 private:
499  Simulator ();
501  ~Simulator ();
502 
509  static EventId DoSchedule (Time const &delay, EventImpl *event);
515  static EventId DoScheduleNow (EventImpl *event);
521  static EventId DoScheduleDestroy (EventImpl *event);
522 
523 }; // class Simulator
524 
538 Time Now (void);
539 
540 } // namespace ns3
541 
542 
543 /********************************************************************
544  * Implementation of the templates declared above.
545  ********************************************************************/
546 
547 namespace ns3 {
548 
549 // Doxygen has trouble with static template functions in a class:
550 // it treats the in-class declaration as different from the
551 // out of class definition, so makes two entries in the member list. Ugh
552 
553 template <typename FUNC,
554  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
555  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
556  typename... Ts>
557 EventId Simulator::Schedule (Time const &delay, FUNC f, Ts&&... args)
558 {
559  return DoSchedule (delay, MakeEvent (f, std::forward<Ts> (args)...));
560 }
561 
562 template <typename... Us, typename... Ts>
563 EventId Simulator::Schedule (Time const &delay, void (*f)(Us...), Ts&&... args)
564 {
565  return DoSchedule (delay, MakeEvent (f, std::forward<Ts> (args)...));
566 }
567 
568 template <typename FUNC,
569  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
570  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
571  typename... Ts>
572 void Simulator::ScheduleWithContext (uint32_t context, Time const &delay, FUNC f, Ts&&... args)
573 {
574  return ScheduleWithContext (context, delay, MakeEvent (f, std::forward<Ts> (args)...));
575 }
576 
577 template <typename... Us, typename... Ts>
578 void Simulator::ScheduleWithContext (uint32_t context, Time const &delay, void (*f)(Us...), Ts&&... args)
579 {
580  return ScheduleWithContext (context, delay, MakeEvent (f, std::forward<Ts> (args)...));
581 }
582 
583 template <typename FUNC,
584  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
585  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
586  typename... Ts>
587 EventId
588 Simulator::ScheduleNow (FUNC f, Ts&&... args)
589 {
590  return DoScheduleNow (MakeEvent (f, std::forward<Ts> (args)...));
591 }
592 
593 template <typename... Us, typename... Ts>
594 EventId
595 Simulator::ScheduleNow (void (*f)(Us...), Ts&&... args)
596 {
597  return DoScheduleNow (MakeEvent (f, std::forward<Ts> (args)...));
598 }
599 
600 
601 template <typename FUNC,
602  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value,int>::type,
603  typename std::enable_if<!std::is_function<typename std::remove_pointer<FUNC>::type>::value,int>::type,
604  typename... Ts>
605 EventId
606 Simulator::ScheduleDestroy (FUNC f, Ts&&... args)
607 {
608  return DoScheduleDestroy (MakeEvent (f, std::forward<Ts> (args)...));
609 }
610 
611 template <typename... Us, typename... Ts>
612 EventId
613 Simulator::ScheduleDestroy (void (*f)(Us...), Ts&&... args)
614 {
615  return DoScheduleDestroy (MakeEvent (f, std::forward<Ts> (args)...));
616 }
617 
618 } // namespace ns3
619 
620 #endif /* SIMULATOR_H */
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:204
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Control the scheduling of simulation events.
Definition: simulator.h:68
ns3::EventImpl declarations.
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:327
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition: simulator.cc:251
static Ptr< SimulatorImpl > GetImplementation(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:353
static uint32_t GetSystemId(void)
Get the system id of this simulator.
Definition: simulator.cc:312
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:300
ns3::ObjectFactory class declaration.
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:268
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:813
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:606
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:588
ns3::MakeEvent function declarations and template implementation.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes...
~Simulator()
Destructor.
static EventId DoSchedule(Time const &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:235
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:572
double f(double x, void *params)
Definition: 80211b.c:70
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
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:278
Simulator()
Default constructor.
static uint64_t GetEventCount(void)
Get the number of events executed.
Definition: simulator.cc:306
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:158
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Instantiate subclasses of ns3::Object.
A simulation event.
Definition: event-impl.h:44
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:243
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:180
Flag for events not associated with any particular context.
Definition: simulator.h:199
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:165
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
ns3::EventId declarations.
static Time GetMaximumSimulationTime(void)
Get the maximum representable simulation time.
Definition: simulator.cc:293
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:34