A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
visual-simulator-impl.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Gustavo Carneiro
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gustavo Carneiro <gjcarneiro@gmail.com> <gjc@inescporto.pt>
7 */
8#include <Python.h>
9#undef HAVE_SYS_STAT_H
11
12#include "ns3/default-simulator-impl.h"
13#include "ns3/log.h"
14#include "ns3/packet-metadata.h"
15
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("VisualSimulatorImpl");
20
22
23namespace
24{
25/**
26 * Get an object factory configured to the default simulator implementation
27 * @return an object factory.
28 */
31{
32 ObjectFactory factory;
34 return factory;
35}
36} // namespace
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::VisualSimulatorImpl")
44 .SetGroupName("Visualizer")
45 .AddConstructor<VisualSimulatorImpl>()
46 .AddAttribute(
47 "SimulatorImplFactory",
48 "Factory for the underlying simulator implementation used by the visualizer.",
49 ObjectFactoryValue(GetDefaultSimulatorImplFactory()),
52 return tid;
53}
54
59
63
64void
66{
67 if (m_simulator)
68 {
69 m_simulator->Dispose();
70 m_simulator = nullptr;
71 }
73}
74
75void
80
81void
86
87void
89{
90 m_simulator->SetScheduler(schedulerFactory);
91}
92
93// System ID for non-distributed simulation is always zero
96{
97 return m_simulator->GetSystemId();
98}
99
100bool
102{
103 return m_simulator->IsFinished();
104}
105
106void
108{
109 if (!Py_IsInitialized())
110 {
111 Py_Initialize();
112 PyRun_SimpleString("import visualizer\n"
113 "visualizer.start();\n");
114 }
115 else
116 {
117 PyGILState_STATE __py_gil_state = PyGILState_Ensure();
118
119 PyRun_SimpleString("import visualizer\n"
120 "visualizer.start();\n");
121
122 PyGILState_Release(__py_gil_state);
123 }
124}
125
126void
128{
129 m_simulator->Stop();
130}
131
134{
135 m_stopTime = delay;
136 return m_simulator->Stop(delay);
137}
138
139Time
144
145//
146// Schedule an event for a _relative_ time in the future.
147//
150{
151 return m_simulator->Schedule(delay, event);
152}
153
154void
156{
157 m_simulator->ScheduleWithContext(context, delay, event);
158}
159
162{
163 return m_simulator->ScheduleNow(event);
164}
165
168{
169 return m_simulator->ScheduleDestroy(event);
170}
171
172Time
174{
175 return m_simulator->Now();
176}
177
178Time
180{
181 return m_simulator->GetDelayLeft(id);
182}
183
184void
186{
187 m_simulator->Remove(id);
188}
189
190void
192{
193 m_simulator->Cancel(id);
194}
195
196bool
198{
199 return m_simulator->IsExpired(id);
200}
201
202Time
204{
205 return m_simulator->GetMaximumSimulationTime();
206}
207
210{
211 return m_simulator->GetContext();
212}
213
214uint64_t
216{
217 return m_simulator->GetEventCount();
218}
219
220void
225
226} // namespace ns3
static TypeId GetTypeId()
Register this type.
An identifier for simulation events.
Definition event-id.h:44
A simulation event.
Definition event-impl.h:35
Instantiate subclasses of ns3::Object.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
AttributeValue implementation for ObjectFactory.
virtual void DoDispose()
Destructor implementation.
Definition object.cc:430
static void Enable()
Enable the packet metadata.
The SimulatorImpl base class.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
A replacement simulator that starts the visualizer.
static TypeId GetTypeId()
Get the type ID.
EventId Schedule(const Time &delay, EventImpl *event) override
Schedule a future event execution (in the same context).
Time GetStopTime()
Get the simulator implementation stop time.
void Cancel(const EventId &id) override
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
void Remove(const EventId &id) override
Remove an event from the event list.
Time Now() const override
Return the current simulation virtual time.
Time GetDelayLeft(const EventId &id) const override
Get the remaining time until this event will execute.
Time m_stopTime
The stop time of the simulation.
uint32_t GetSystemId() const override
Get the system id of this simulator.
ObjectFactory m_simulatorImplFactory
Simulator implementation factory.
uint32_t GetContext() const override
Get the current simulation context.
Ptr< SimulatorImpl > m_simulator
The simulator implementation.
Time GetMaximumSimulationTime() const override
Get the maximum representable simulation time.
EventId ScheduleDestroy(EventImpl *event) override
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
void DoDispose() override
Destructor implementation.
bool IsFinished() const override
Check if the simulation should finish.
uint64_t GetEventCount() const override
Get the number of events executed.
void Destroy() override
Execute the events scheduled with ScheduleDestroy().
bool IsExpired(const EventId &id) const override
Check if an event has already run or been cancelled.
void SetScheduler(ObjectFactory schedulerFactory) override
Set the Scheduler to be used to manage the event list.
void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event) override
Schedule a future event execution (in a different context).
void NotifyConstructionCompleted() override
Notifier called once the ObjectBase is fully constructed.
void Run() override
Run the simulation.
void RunRealSimulator()
calls Run() in the wrapped simulator
EventId ScheduleNow(EventImpl *event) override
Schedule an event to run at the current virtual time.
void Stop() override
Tell the Simulator the calling event should be the last one executed.
Ptr< const AttributeChecker > MakeObjectFactoryChecker()
Ptr< const AttributeAccessor > MakeObjectFactoryAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
ObjectFactory GetDefaultSimulatorImplFactory()
Get an object factory configured to the default simulator implementation.
Every class exported by the ns3 library is enclosed in the ns3 namespace.