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 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("VisualSimulatorImpl");
28 
29 NS_OBJECT_ENSURE_REGISTERED (VisualSimulatorImpl);
30 
31 namespace
32 {
36 {
37  ObjectFactory factory;
39  return factory;
40 }
41 }
42 
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::VisualSimulatorImpl")
49  .SetGroupName ("Visualizer")
50  .AddConstructor<VisualSimulatorImpl> ()
51  .AddAttribute ("SimulatorImplFactory",
52  "Factory for the underlying simulator implementation used by the visualizer.",
56  ;
57  return tid;
58 }
59 
60 
62 {
63 }
64 
66 {
67 }
68 
69 void
71 {
72  if (m_simulator)
73  {
74  m_simulator->Dispose ();
75  m_simulator = NULL;
76  }
78 }
79 
80 void
82 {
84 }
85 
86 
87 void
89 {
90  m_simulator->Destroy ();
91 }
92 
93 void
95 {
96  m_simulator->SetScheduler (schedulerFactory);
97 }
98 
99 // System ID for non-distributed simulation is always zero
100 uint32_t
102 {
103  return m_simulator->GetSystemId ();
104 }
105 
106 bool
108 {
109  return m_simulator->IsFinished ();
110 }
111 
112 void
114 {
115  if (!Py_IsInitialized ())
116  {
117  #if PY_MAJOR_VERSION >= 3
118  const wchar_t *argv[] = { L"python", NULL};
119  Py_Initialize ();
120  PySys_SetArgv (1, (wchar_t**) argv);
121  #else
122  const char *argv[] = { "python", NULL};
123  Py_Initialize ();
124  PySys_SetArgv (1, (char**) argv);
125  #endif
126  PyRun_SimpleString (
127  "import visualizer\n"
128  "visualizer.start();\n"
129  );
130  }
131  else
132  {
133  PyGILState_STATE __py_gil_state = PyGILState_Ensure ();
134 
135  PyRun_SimpleString (
136  "import visualizer\n"
137  "visualizer.start();\n"
138  );
139 
140  PyGILState_Release (__py_gil_state);
141  }
142 }
143 
144 void
146 {
147  m_simulator->Stop ();
148 }
149 
150 void
152 {
153  m_simulator->Stop (delay);
154 }
155 
156 //
157 // Schedule an event for a _relative_ time in the future.
158 //
159 EventId
161 {
162  return m_simulator->Schedule (delay, event);
163 }
164 
165 void
166 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &delay, EventImpl *event)
167 {
168  m_simulator->ScheduleWithContext (context, delay, event);
169 }
170 
171 EventId
173 {
174  return m_simulator->ScheduleNow (event);
175 }
176 
177 EventId
179 {
180  return m_simulator->ScheduleDestroy (event);
181 }
182 
183 Time
185 {
186  return m_simulator->Now ();
187 }
188 
189 Time
191 {
192  return m_simulator->GetDelayLeft (id);
193 }
194 
195 void
197 {
198  m_simulator->Remove (id);
199 }
200 
201 void
203 {
204  m_simulator->Cancel (id);
205 }
206 
207 bool
209 {
210  return m_simulator->IsExpired (id);
211 }
212 
213 Time
215 {
216  return m_simulator->GetMaximumSimulationTime ();
217 }
218 
219 uint32_t
221 {
222  return m_simulator->GetContext ();
223 }
224 
225 uint64_t
227 {
228  return m_simulator->GetEventCount ();
229 }
230 
231 void
233 {
234  m_simulator->Run ();
235 }
236 
237 
238 } // namespace ns3
239 
240 
virtual EventId Schedule(Time const &delay, EventImpl *event)
Schedule a future event execution (in the same context).
void NotifyConstructionCompleted(void)
Notifier called once the ObjectBase is fully constructed.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#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:204
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
Ptr< const AttributeChecker > MakeObjectFactoryChecker(void)
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.
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:915
static TypeId GetTypeId(void)
Get the type ID.
The SimulatorImpl base class.