A Discrete-Event Network Simulator
API
simulator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #ifndef SIMULATOR_H
21 #define SIMULATOR_H
22 
23 #include "event-id.h"
24 #include "event-impl.h"
25 #include "make-event.h"
26 #include "nstime.h"
27 #include "object-factory.h"
28 
29 #include <stdint.h>
30 #include <string>
31 
38 namespace ns3
39 {
40 
41 class SimulatorImpl;
42 class Scheduler;
43 
67 class Simulator
68 {
69  public:
70  // Delete default constructor and destructor to avoid misuse
71  Simulator() = delete;
72  ~Simulator() = delete;
73 
86  static void SetImplementation(Ptr<SimulatorImpl> impl);
87 
106 
115  static void SetScheduler(ObjectFactory schedulerFactory);
116 
126  static void Destroy();
127 
137  static bool IsFinished();
138 
149  static void Run();
150 
159  static void Stop();
160 
170  static void Stop(const Time& delay);
171 
188  static uint32_t GetContext();
189 
197  enum : uint32_t
198  {
202  NO_CONTEXT = 0xffffffff
203  };
204 
209  static uint64_t GetEventCount();
210 
231  template <
232  typename FUNC,
233  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type = 0,
235  int>::type = 0,
236  typename... Ts>
237  static EventId Schedule(const Time& delay, FUNC f, Ts&&... args);
238 
254  template <typename... Us, typename... Ts>
255  static EventId Schedule(const Time& delay, void (*f)(Us...), Ts&&... args); // Schedule events (in the same context) to run at a future time.
257 
279  template <
280  typename FUNC,
281  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type = 0,
283  int>::type = 0,
284  typename... Ts>
285  static void ScheduleWithContext(uint32_t context, const Time& delay, FUNC f, Ts&&... args);
286 
299  template <typename... Us, typename... Ts>
300  static void ScheduleWithContext(uint32_t context,
301  const Time& delay,
302  void (*f)(Us...),
303  Ts&&... args); // Schedule events (in a different context) to run now or at a future time.
305 
324  template <
325  typename FUNC,
326  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type = 0,
328  int>::type = 0,
329  typename... Ts>
330  static EventId ScheduleNow(FUNC f, Ts&&... args);
331 
343  template <typename... Us, typename... Ts>
344  static EventId ScheduleNow(void (*f)(Us...), Ts&&... args);
345  // Schedule events (in the same context) to run now.
347 
368  template <
369  typename FUNC,
370  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type = 0,
372  int>::type = 0,
373  typename... Ts>
374  static EventId ScheduleDestroy(FUNC f, Ts&&... args);
375 
388  template <typename... Us, typename... Ts>
389  static EventId ScheduleDestroy(void (*f)(Us...), Ts&&... args);
390  // Schedule events to run when Simulator:Destroy() is called.
392 
405  static void Remove(const EventId& id);
406 
420  static void Cancel(const EventId& id);
421 
436  static bool IsExpired(const EventId& id);
437 
443  static Time Now();
444 
453  static Time GetDelayLeft(const EventId& id);
454 
464 
472  static EventId Schedule(const Time& delay, const Ptr<EventImpl>& event);
473 
482  static void ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event);
483 
491  static EventId ScheduleDestroy(const Ptr<EventImpl>& event);
492 
499  static EventId ScheduleNow(const Ptr<EventImpl>& event);
500 
508  static uint32_t GetSystemId();
509 
510  private:
517  static EventId DoSchedule(const Time& delay, EventImpl* event);
523  static EventId DoScheduleNow(EventImpl* event);
529  static EventId DoScheduleDestroy(EventImpl* event);
530 
531 }; // class Simulator
532 
546 Time Now();
547 
548 } // namespace ns3
549 
550 /********************************************************************
551  * Implementation of the templates declared above.
552  ********************************************************************/
553 
554 namespace ns3
555 {
556 
557 // Doxygen has trouble with static template functions in a class:
558 // it treats the in-class declaration as different from the
559 // out of class definition, so makes two entries in the member list. Ugh
560 
561 template <
562  typename FUNC,
563  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type,
565  int>::type,
566  typename... Ts>
567 EventId
568 Simulator::Schedule(const Time& delay, FUNC f, Ts&&... args)
569 {
570  return DoSchedule(delay, MakeEvent(f, std::forward<Ts>(args)...));
571 }
572 
573 template <typename... Us, typename... Ts>
574 EventId
575 Simulator::Schedule(const Time& delay, void (*f)(Us...), Ts&&... args)
576 {
577  return DoSchedule(delay, MakeEvent(f, std::forward<Ts>(args)...));
578 }
579 
580 template <
581  typename FUNC,
582  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type,
584  int>::type,
585  typename... Ts>
586 void
587 Simulator::ScheduleWithContext(uint32_t context, const Time& delay, FUNC f, Ts&&... args)
588 {
589  return ScheduleWithContext(context, delay, MakeEvent(f, std::forward<Ts>(args)...));
590 }
591 
592 template <typename... Us, typename... Ts>
593 void
594 Simulator::ScheduleWithContext(uint32_t context, const Time& delay, void (*f)(Us...), Ts&&... args)
595 {
596  return ScheduleWithContext(context, delay, MakeEvent(f, std::forward<Ts>(args)...));
597 }
598 
599 template <
600  typename FUNC,
601  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type,
603  int>::type,
604  typename... Ts>
605 EventId
607 {
608  return DoScheduleNow(MakeEvent(f, std::forward<Ts>(args)...));
609 }
610 
611 template <typename... Us, typename... Ts>
612 EventId
613 Simulator::ScheduleNow(void (*f)(Us...), Ts&&... args)
614 {
615  return DoScheduleNow(MakeEvent(f, std::forward<Ts>(args)...));
616 }
617 
618 template <
619  typename FUNC,
620  typename std::enable_if<!std::is_convertible<FUNC, Ptr<EventImpl>>::value, int>::type,
622  int>::type,
623  typename... Ts>
624 EventId
626 {
627  return DoScheduleDestroy(MakeEvent(f, std::forward<Ts>(args)...));
628 }
629 
630 template <typename... Us, typename... Ts>
631 EventId
632 Simulator::ScheduleDestroy(void (*f)(Us...), Ts&&... args)
633 {
634  return DoScheduleDestroy(MakeEvent(f, std::forward<Ts>(args)...));
635 }
636 
637 } // namespace ns3
638 
639 #endif /* SIMULATOR_H */
double f(double x, void *params)
Definition: 80211b.c:71
An identifier for simulation events.
Definition: event-id.h:55
A simulation event.
Definition: event-impl.h:46
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Control the scheduling of simulation events.
Definition: simulator.h:68
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition: simulator.cc:260
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static Ptr< SimulatorImpl > GetImplementation()
Get the SimulatorImpl singleton.
Definition: simulator.cc:364
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:276
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:587
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:169
static uint32_t GetSystemId()
Get the system id of this simulator.
Definition: simulator.cc:321
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:202
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
static void Run()
Run the simulation.
Definition: simulator.cc:176
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
Definition: simulator.cc:286
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:625
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:162
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:251
static uint64_t GetEventCount()
Get the number of events executed.
Definition: simulator.cc:315
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
~Simulator()=delete
static EventId DoSchedule(const Time &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:242
static Time GetMaximumSimulationTime()
Get the maximum representable simulation time.
Definition: simulator.cc:302
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:266
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:336
static uint32_t GetContext()
Get the current simulation context.
Definition: simulator.cc:309
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
ns3::EventId declarations.
ns3::EventImpl declarations.
EventImpl * MakeEvent(void(*f)())
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:36
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
ns3::MakeEvent function declarations and template implementation.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:848
Every class exported by the ns3 library is enclosed in the ns3 namespace.
value
Definition: second.py:41
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::ObjectFactory class declaration.