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
21NS_OBJECT_ENSURE_REGISTERED(VisualSimulatorImpl);
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 return m_simulator->Stop(delay);
136}
137
138//
139// Schedule an event for a _relative_ time in the future.
140//
143{
144 return m_simulator->Schedule(delay, event);
145}
146
147void
149{
150 m_simulator->ScheduleWithContext(context, delay, event);
151}
152
155{
156 return m_simulator->ScheduleNow(event);
157}
158
161{
162 return m_simulator->ScheduleDestroy(event);
163}
164
165Time
167{
168 return m_simulator->Now();
169}
170
171Time
173{
174 return m_simulator->GetDelayLeft(id);
175}
176
177void
179{
180 m_simulator->Remove(id);
181}
182
183void
185{
186 m_simulator->Cancel(id);
187}
188
189bool
191{
192 return m_simulator->IsExpired(id);
193}
194
195Time
197{
198 return m_simulator->GetMaximumSimulationTime();
199}
200
203{
204 return m_simulator->GetContext();
205}
206
207uint64_t
209{
210 return m_simulator->GetEventCount();
211}
212
213void
218
219} // namespace ns3
static TypeId GetTypeId()
Register this type.
An identifier for simulation events.
Definition event-id.h:45
A simulation event.
Definition event-impl.h:35
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
static void Enable()
Enable the packet metadata.
The SimulatorImpl base class.
virtual void Destroy()=0
Execute the events scheduled with ScheduleDestroy().
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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).
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.
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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.
Ptr< const AttributeChecker > MakeObjectFactoryChecker()
Ptr< const AttributeAccessor > MakeObjectFactoryAccessor(T1 a1)