A Discrete-Event Network Simulator
API
visual-simulator-impl.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Gustavo Carneiro
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Gustavo Carneiro <gjcarneiro@gmail.com> <gjc@inescporto.pt>
19  */
20 #include <Python.h>
21 #include "visual-simulator-impl.h"
22 #include "ns3/default-simulator-impl.h"
23 #include "ns3/log.h"
24 #include "ns3/packet-metadata.h"
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("VisualSimulatorImpl");
29 
30 NS_OBJECT_ENSURE_REGISTERED (VisualSimulatorImpl);
31 
32 namespace
33 {
37 {
38  ObjectFactory factory;
40  return factory;
41 }
42 }
43 
44 
45 TypeId
47 {
48  static TypeId tid = TypeId ("ns3::VisualSimulatorImpl")
50  .SetGroupName ("Visualizer")
51  .AddConstructor<VisualSimulatorImpl> ()
52  .AddAttribute ("SimulatorImplFactory",
53  "Factory for the underlying simulator implementation used by the visualizer.",
57  ;
58  return tid;
59 }
60 
61 
63 {
65 }
66 
68 {
69 }
70 
71 void
73 {
74  if (m_simulator)
75  {
76  m_simulator->Dispose ();
77  m_simulator = NULL;
78  }
80 }
81 
82 void
84 {
86 }
87 
88 
89 void
91 {
92  m_simulator->Destroy ();
93 }
94 
95 void
97 {
98  m_simulator->SetScheduler (schedulerFactory);
99 }
100 
101 // System ID for non-distributed simulation is always zero
102 uint32_t
104 {
105  return m_simulator->GetSystemId ();
106 }
107 
108 bool
110 {
111  return m_simulator->IsFinished ();
112 }
113 
114 void
116 {
117  if (!Py_IsInitialized ())
118  {
119  #if PY_MAJOR_VERSION >= 3
120  const wchar_t *argv[] = { L"python", NULL};
121  Py_Initialize ();
122  PySys_SetArgv (1, (wchar_t**) argv);
123  #else
124  const char *argv[] = { "python", NULL};
125  Py_Initialize ();
126  PySys_SetArgv (1, (char**) argv);
127  #endif
128  PyRun_SimpleString (
129  "import visualizer\n"
130  "visualizer.start();\n"
131  );
132  }
133  else
134  {
135  PyGILState_STATE __py_gil_state = PyGILState_Ensure ();
136 
137  PyRun_SimpleString (
138  "import visualizer\n"
139  "visualizer.start();\n"
140  );
141 
142  PyGILState_Release (__py_gil_state);
143  }
144 }
145 
146 void
148 {
149  m_simulator->Stop ();
150 }
151 
152 void
154 {
155  m_simulator->Stop (delay);
156 }
157 
158 //
159 // Schedule an event for a _relative_ time in the future.
160 //
161 EventId
163 {
164  return m_simulator->Schedule (delay, event);
165 }
166 
167 void
168 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &delay, EventImpl *event)
169 {
170  m_simulator->ScheduleWithContext (context, delay, event);
171 }
172 
173 EventId
175 {
176  return m_simulator->ScheduleNow (event);
177 }
178 
179 EventId
181 {
182  return m_simulator->ScheduleDestroy (event);
183 }
184 
185 Time
187 {
188  return m_simulator->Now ();
189 }
190 
191 Time
193 {
194  return m_simulator->GetDelayLeft (id);
195 }
196 
197 void
199 {
200  m_simulator->Remove (id);
201 }
202 
203 void
205 {
206  m_simulator->Cancel (id);
207 }
208 
209 bool
211 {
212  return m_simulator->IsExpired (id);
213 }
214 
215 Time
217 {
218  return m_simulator->GetMaximumSimulationTime ();
219 }
220 
221 uint32_t
223 {
224  return m_simulator->GetContext ();
225 }
226 
227 uint64_t
229 {
230  return m_simulator->GetEventCount ();
231 }
232 
233 void
235 {
236  m_simulator->Run ();
237 }
238 
239 
240 } // namespace ns3
241 
242 
virtual EventId Schedule(Time const &delay, EventImpl *event)
Schedule a future event execution (in the same context).
Ptr< const AttributeChecker > MakeObjectFactoryChecker(void)
void NotifyConstructionCompleted(void)
Notifier called once the ObjectBase is fully constructed.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
virtual void Run(void)
Run the simulation.
virtual uint32_t GetContext(void) const
Get the current simulation context.
ObjectFactory GetDefaultSimulatorImplFactory()
Get an object factory configured to the default simulator implementation.
void DoDispose()
Destructor implementation.
ObjectFactory m_simulatorImplFactory
simulator implementation factory
virtual Time Now(void) const
Return the current simulation virtual time.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual void Remove(const EventId &id)
Remove an event from the event list.
virtual void SetScheduler(ObjectFactory schedulerFactory)
Set the Scheduler to be used to manage the event list.
A replacement simulator that starts the visualizer.
AttributeValue implementation for ObjectFactory.
virtual void ScheduleWithContext(uint32_t context, Time const &delay, EventImpl *event)
Schedule a future event execution (in a different context).
static TypeId GetTypeId(void)
Register this type.
virtual uint64_t GetEventCount(void) const
Get the number of events executed.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
virtual void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Ptr< const AttributeAccessor > MakeObjectFactoryAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
virtual void Destroy()
Execute the events scheduled with ScheduleDestroy().
virtual Time GetDelayLeft(const EventId &id) const
Get the remaining time until this event will execute.
virtual EventId ScheduleNow(EventImpl *event)
Schedule an event to run at the current virtual time.
virtual uint32_t GetSystemId(void) const
Get the system id of this simulator.
static void Enable(void)
Enable the packet metadata.
Ptr< SimulatorImpl > m_simulator
the simulator implementation
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void RunRealSimulator(void)
calls Run() in the wrapped simulator
Instantiate subclasses of ns3::Object.
A simulation event.
Definition: event-impl.h:44
An identifier for simulation events.
Definition: event-id.h:53
virtual void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
virtual bool IsFinished(void) const
Check if the simulation should finish.
virtual bool IsExpired(const EventId &id) const
Check if an event has already run or been cancelled.
virtual Time GetMaximumSimulationTime(void) const
Get the maximum representable simulation time.
a unique identifier for an interface.
Definition: type-id.h:58
virtual EventId ScheduleDestroy(EventImpl *event)
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
static TypeId GetTypeId(void)
Get the type ID.
The SimulatorImpl base class.