A Discrete-Event Network Simulator
API
visual-simulator-impl.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Gustavo Carneiro
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: Gustavo Carneiro <gjcarneiro@gmail.com> <gjc@inescporto.pt>
18  */
19 #include <Python.h>
20 #undef HAVE_SYS_STAT_H
21 #include "visual-simulator-impl.h"
22 
23 #include "ns3/default-simulator-impl.h"
24 #include "ns3/log.h"
25 #include "ns3/packet-metadata.h"
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("VisualSimulatorImpl");
31 
32 NS_OBJECT_ENSURE_REGISTERED(VisualSimulatorImpl);
33 
34 namespace
35 {
42 {
43  ObjectFactory factory;
45  return factory;
46 }
47 } // namespace
48 
49 TypeId
51 {
52  static TypeId tid =
53  TypeId("ns3::VisualSimulatorImpl")
55  .SetGroupName("Visualizer")
56  .AddConstructor<VisualSimulatorImpl>()
57  .AddAttribute(
58  "SimulatorImplFactory",
59  "Factory for the underlying simulator implementation used by the visualizer.",
63  return tid;
64 }
65 
67 {
69 }
70 
72 {
73 }
74 
75 void
77 {
78  if (m_simulator)
79  {
80  m_simulator->Dispose();
81  m_simulator = nullptr;
82  }
84 }
85 
86 void
88 {
90 }
91 
92 void
94 {
95  m_simulator->Destroy();
96 }
97 
98 void
100 {
101  m_simulator->SetScheduler(schedulerFactory);
102 }
103 
104 // System ID for non-distributed simulation is always zero
105 uint32_t
107 {
108  return m_simulator->GetSystemId();
109 }
110 
111 bool
113 {
114  return m_simulator->IsFinished();
115 }
116 
117 void
119 {
120  if (!Py_IsInitialized())
121  {
122  Py_Initialize();
123  PyRun_SimpleString("import visualizer\n"
124  "visualizer.start();\n");
125  }
126  else
127  {
128  PyGILState_STATE __py_gil_state = PyGILState_Ensure();
129 
130  PyRun_SimpleString("import visualizer\n"
131  "visualizer.start();\n");
132 
133  PyGILState_Release(__py_gil_state);
134  }
135 }
136 
137 void
139 {
140  m_simulator->Stop();
141 }
142 
143 EventId
145 {
146  return m_simulator->Stop(delay);
147 }
148 
149 //
150 // Schedule an event for a _relative_ time in the future.
151 //
152 EventId
154 {
155  return m_simulator->Schedule(delay, event);
156 }
157 
158 void
159 VisualSimulatorImpl::ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event)
160 {
161  m_simulator->ScheduleWithContext(context, delay, event);
162 }
163 
164 EventId
166 {
167  return m_simulator->ScheduleNow(event);
168 }
169 
170 EventId
172 {
173  return m_simulator->ScheduleDestroy(event);
174 }
175 
176 Time
178 {
179  return m_simulator->Now();
180 }
181 
182 Time
184 {
185  return m_simulator->GetDelayLeft(id);
186 }
187 
188 void
190 {
191  m_simulator->Remove(id);
192 }
193 
194 void
196 {
197  m_simulator->Cancel(id);
198 }
199 
200 bool
202 {
203  return m_simulator->IsExpired(id);
204 }
205 
206 Time
208 {
209  return m_simulator->GetMaximumSimulationTime();
210 }
211 
212 uint32_t
214 {
215  return m_simulator->GetContext();
216 }
217 
218 uint64_t
220 {
221  return m_simulator->GetEventCount();
222 }
223 
224 void
226 {
227  m_simulator->Run();
228 }
229 
230 } // namespace ns3
static TypeId GetTypeId()
Register this type.
An identifier for simulation events.
Definition: event-id.h:55
A simulation event.
Definition: event-impl.h:46
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:352
static void Enable()
Enable the packet metadata.
The SimulatorImpl base class.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
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:202
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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 AttributeAccessor > MakeObjectFactoryAccessor(T1 a1)
Ptr< const AttributeChecker > MakeObjectFactoryChecker()