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#include "ns3/system-path.h"
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("VisualSimulatorImpl");
21
23
24namespace
25{
26/**
27 * Get an object factory configured to the default simulator implementation
28 * @return an object factory.
29 */
32{
33 ObjectFactory factory;
35 return factory;
36}
37} // namespace
38
39TypeId
41{
42 static TypeId tid =
43 TypeId("ns3::VisualSimulatorImpl")
45 .SetGroupName("Visualizer")
46 .AddConstructor<VisualSimulatorImpl>()
47 .AddAttribute(
48 "SimulatorImplFactory",
49 "Factory for the underlying simulator implementation used by the visualizer.",
50 ObjectFactoryValue(GetDefaultSimulatorImplFactory()),
53 return tid;
54}
55
60
64
65void
67{
68 if (m_simulator)
69 {
70 m_simulator->Dispose();
71 m_simulator = nullptr;
72 }
74}
75
76void
81
82void
87
88void
90{
91 m_simulator->SetScheduler(schedulerFactory);
92}
93
94// System ID for non-distributed simulation is always zero
97{
98 return m_simulator->GetSystemId();
99}
100
101bool
103{
104 return m_simulator->IsFinished();
105}
106
107void
109{
110 std::stringstream ss;
111 // If not a python script, where we can easily get argv, then inject program path as argv0
112 if (SystemPath::FindSelf().find("python") == std::string::npos)
113 {
114 ss << "import sys\n"
115 << "sys.argv = ['" << SystemPath::FindSelf() << "']\n";
116 }
117 ss << "import visualizer\n"
118 << "visualizer.start()\n";
119 if (!Py_IsInitialized())
120 {
121 Py_Initialize();
122 PyRun_SimpleString(ss.str().data());
123 }
124 else
125 {
126 PyGILState_STATE __py_gil_state = PyGILState_Ensure();
127
128 PyRun_SimpleString(ss.str().data());
129
130 PyGILState_Release(__py_gil_state);
131 }
132}
133
134void
136{
137 m_simulator->Stop();
138}
139
142{
143 m_stopTime = delay;
144 return m_simulator->Stop(delay);
145}
146
147Time
152
153//
154// Schedule an event for a _relative_ time in the future.
155//
158{
159 return m_simulator->Schedule(delay, event);
160}
161
162void
164{
165 m_simulator->ScheduleWithContext(context, delay, event);
166}
167
170{
171 return m_simulator->ScheduleNow(event);
172}
173
176{
177 return m_simulator->ScheduleDestroy(event);
178}
179
180Time
182{
183 return m_simulator->Now();
184}
185
186Time
188{
189 return m_simulator->GetDelayLeft(id);
190}
191
192void
194{
195 m_simulator->Remove(id);
196}
197
198void
200{
201 m_simulator->Cancel(id);
202}
203
204bool
206{
207 return m_simulator->IsExpired(id);
208}
209
210Time
212{
213 return m_simulator->GetMaximumSimulationTime();
214}
215
218{
219 return m_simulator->GetContext();
220}
221
222uint64_t
224{
225 return m_simulator->GetEventCount();
226}
227
228void
233
234} // 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
std::string FindSelf()
Get the file system path to the current executable.
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.