A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
38namespace ns3
39{
40
41class SimulatorImpl;
42class Scheduler;
43
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
171 static EventId Stop(const Time& delay);
172
178 static EventId GetStopEvent();
179
196 static uint32_t GetContext();
197
205 enum : uint32_t
206 {
210 NO_CONTEXT = 0xffffffff
211 };
212
217 static uint64_t GetEventCount();
218
239 template <typename FUNC,
240 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
241 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 0,
242 typename... Ts>
243 static EventId Schedule(const Time& delay, FUNC f, Ts&&... args);
244
260 template <typename... Us, typename... Ts>
261 static EventId Schedule(const Time& delay, void (*f)(Us...), Ts&&... args); // Schedule events (in the same context) to run at a future time.
263
285 template <typename FUNC,
286 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
287 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 0,
288 typename... Ts>
289 static void ScheduleWithContext(uint32_t context, const Time& delay, FUNC f, Ts&&... args);
290
303 template <typename... Us, typename... Ts>
304 static void ScheduleWithContext(uint32_t context,
305 const Time& delay,
306 void (*f)(Us...),
307 Ts&&... args); // Schedule events (in a different context) to run now or at a future time.
309
328 template <typename FUNC,
329 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
330 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 0,
331 typename... Ts>
332 static EventId ScheduleNow(FUNC f, Ts&&... args);
333
345 template <typename... Us, typename... Ts>
346 static EventId ScheduleNow(void (*f)(Us...), Ts&&... args);
347 // Schedule events (in the same context) to run now.
349
370 template <typename FUNC,
371 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
372 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 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
535
536}; // class Simulator
537
551Time Now();
552
553} // namespace ns3
554
555/********************************************************************
556 * Implementation of the templates declared above.
557 ********************************************************************/
558
559namespace ns3
560{
561
562// Doxygen has trouble with static template functions in a class:
563// it treats the in-class declaration as different from the
564// out of class definition, so makes two entries in the member list. Ugh
565
566template <typename FUNC,
567 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
568 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
569 typename... Ts>
570EventId
571Simulator::Schedule(const Time& delay, FUNC f, Ts&&... args)
572{
573 return DoSchedule(delay, MakeEvent(f, std::forward<Ts>(args)...));
574}
575
576template <typename... Us, typename... Ts>
578Simulator::Schedule(const Time& delay, void (*f)(Us...), Ts&&... args)
579{
580 return DoSchedule(delay, MakeEvent(f, std::forward<Ts>(args)...));
581}
582
583template <typename FUNC,
584 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
585 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
586 typename... Ts>
587void
588Simulator::ScheduleWithContext(uint32_t context, const Time& delay, FUNC f, Ts&&... args)
589{
590 return ScheduleWithContext(context, delay, MakeEvent(f, std::forward<Ts>(args)...));
591}
592
593template <typename... Us, typename... Ts>
594void
595Simulator::ScheduleWithContext(uint32_t context, const Time& delay, void (*f)(Us...), Ts&&... args)
596{
597 return ScheduleWithContext(context, delay, MakeEvent(f, std::forward<Ts>(args)...));
598}
599
600template <typename FUNC,
601 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
602 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
603 typename... Ts>
605Simulator::ScheduleNow(FUNC f, Ts&&... args)
606{
607 return DoScheduleNow(MakeEvent(f, std::forward<Ts>(args)...));
608}
609
610template <typename... Us, typename... Ts>
612Simulator::ScheduleNow(void (*f)(Us...), Ts&&... args)
613{
614 return DoScheduleNow(MakeEvent(f, std::forward<Ts>(args)...));
615}
616
617template <typename FUNC,
618 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
619 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
620 typename... Ts>
622Simulator::ScheduleDestroy(FUNC f, Ts&&... args)
623{
624 return DoScheduleDestroy(MakeEvent(f, std::forward<Ts>(args)...));
625}
626
627template <typename... Us, typename... Ts>
629Simulator::ScheduleDestroy(void (*f)(Us...), Ts&&... args)
630{
631 return DoScheduleDestroy(MakeEvent(f, std::forward<Ts>(args)...));
632}
633
634} // namespace ns3
635
636#endif /* SIMULATOR_H */
double f(double x, void *params)
Definition: 80211b.c:70
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:77
Control the scheduling of simulation events.
Definition: simulator.h:68
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition: simulator.cc:269
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Ptr< SimulatorImpl > GetImplementation()
Get the SimulatorImpl singleton.
Definition: simulator.cc:373
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:285
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static EventId m_stopEvent
Stop event (if present)
Definition: simulator.h:534
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:171
static uint32_t GetSystemId()
Get the system id of this simulator.
Definition: simulator.cc:330
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:210
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
Definition: simulator.cc:295
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:622
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:164
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:260
static uint64_t GetEventCount()
Get the number of events executed.
Definition: simulator.cc:324
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
~Simulator()=delete
static EventId DoSchedule(const Time &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:251
static Time GetMaximumSimulationTime()
Get the maximum representable simulation time.
Definition: simulator.cc:311
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:275
static EventId GetStopEvent()
Returns the Stop Event, or an invalid event if the simulation does not have a scheduled stop time.
Definition: simulator.cc:202
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:345
static uint32_t GetContext()
Get the current simulation context.
Definition: simulator.cc:318
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:217
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:305
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.