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  const wchar_t* argv[] = {L"python", nullptr};
123  Py_Initialize();
124  PySys_SetArgv(1, (wchar_t**)argv);
125  PyRun_SimpleString("import visualizer\n"
126  "visualizer.start();\n");
127  }
128  else
129  {
130  PyGILState_STATE __py_gil_state = PyGILState_Ensure();
131 
132  PyRun_SimpleString("import visualizer\n"
133  "visualizer.start();\n");
134 
135  PyGILState_Release(__py_gil_state);
136  }
137 }
138 
139 void
141 {
142  m_simulator->Stop();
143 }
144 
145 void
147 {
148  m_simulator->Stop(delay);
149 }
150 
151 //
152 // Schedule an event for a _relative_ time in the future.
153 //
154 EventId
156 {
157  return m_simulator->Schedule(delay, event);
158 }
159 
160 void
161 VisualSimulatorImpl::ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event)
162 {
163  m_simulator->ScheduleWithContext(context, delay, event);
164 }
165 
166 EventId
168 {
169  return m_simulator->ScheduleNow(event);
170 }
171 
172 EventId
174 {
175  return m_simulator->ScheduleDestroy(event);
176 }
177 
178 Time
180 {
181  return m_simulator->Now();
182 }
183 
184 Time
186 {
187  return m_simulator->GetDelayLeft(id);
188 }
189 
190 void
192 {
193  m_simulator->Remove(id);
194 }
195 
196 void
198 {
199  m_simulator->Cancel(id);
200 }
201 
202 bool
204 {
205  return m_simulator->IsExpired(id);
206 }
207 
208 Time
210 {
211  return m_simulator->GetMaximumSimulationTime();
212 }
213 
214 uint32_t
216 {
217  return m_simulator->GetContext();
218 }
219 
220 uint64_t
222 {
223  return m_simulator->GetEventCount();
224 }
225 
226 void
228 {
229  m_simulator->Run();
230 }
231 
232 } // 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.
AttributeValue implementation for ObjectFactory.
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
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:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
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.
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: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.