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 #undef HAVE_PTHREAD_H
22 #undef HAVE_SYS_STAT_H
23 #include "visual-simulator-impl.h"
24 #include "ns3/default-simulator-impl.h"
25 #include "ns3/log.h"
26 #include "ns3/packet-metadata.h"
27 
28 namespace ns3 {
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 }
48 
49 
50 TypeId
52 {
53  static TypeId tid = TypeId ("ns3::VisualSimulatorImpl")
55  .SetGroupName ("Visualizer")
56  .AddConstructor<VisualSimulatorImpl> ()
57  .AddAttribute ("SimulatorImplFactory",
58  "Factory for the underlying simulator implementation used by the visualizer.",
62  ;
63  return tid;
64 }
65 
66 
68 {
70 }
71 
73 {
74 }
75 
76 void
78 {
79  if (m_simulator)
80  {
81  m_simulator->Dispose ();
82  m_simulator = NULL;
83  }
85 }
86 
87 void
89 {
91 }
92 
93 
94 void
96 {
97  m_simulator->Destroy ();
98 }
99 
100 void
102 {
103  m_simulator->SetScheduler (schedulerFactory);
104 }
105 
106 // System ID for non-distributed simulation is always zero
107 uint32_t
109 {
110  return m_simulator->GetSystemId ();
111 }
112 
113 bool
115 {
116  return m_simulator->IsFinished ();
117 }
118 
119 void
121 {
122  if (!Py_IsInitialized ())
123  {
124  #if PY_MAJOR_VERSION >= 3
125  const wchar_t *argv[] = { L"python", NULL};
126  Py_Initialize ();
127  PySys_SetArgv (1, (wchar_t**) argv);
128  #else
129  const char *argv[] = { "python", NULL};
130  Py_Initialize ();
131  PySys_SetArgv (1, (char**) argv);
132  #endif
133  PyRun_SimpleString (
134  "import visualizer\n"
135  "visualizer.start();\n"
136  );
137  }
138  else
139  {
140  PyGILState_STATE __py_gil_state = PyGILState_Ensure ();
141 
142  PyRun_SimpleString (
143  "import visualizer\n"
144  "visualizer.start();\n"
145  );
146 
147  PyGILState_Release (__py_gil_state);
148  }
149 }
150 
151 void
153 {
154  m_simulator->Stop ();
155 }
156 
157 void
159 {
160  m_simulator->Stop (delay);
161 }
162 
163 //
164 // Schedule an event for a _relative_ time in the future.
165 //
166 EventId
168 {
169  return m_simulator->Schedule (delay, event);
170 }
171 
172 void
173 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &delay, EventImpl *event)
174 {
175  m_simulator->ScheduleWithContext (context, delay, event);
176 }
177 
178 EventId
180 {
181  return m_simulator->ScheduleNow (event);
182 }
183 
184 EventId
186 {
187  return m_simulator->ScheduleDestroy (event);
188 }
189 
190 Time
192 {
193  return m_simulator->Now ();
194 }
195 
196 Time
198 {
199  return m_simulator->GetDelayLeft (id);
200 }
201 
202 void
204 {
205  m_simulator->Remove (id);
206 }
207 
208 void
210 {
211  m_simulator->Cancel (id);
212 }
213 
214 bool
216 {
217  return m_simulator->IsExpired (id);
218 }
219 
220 Time
222 {
223  return m_simulator->GetMaximumSimulationTime ();
224 }
225 
226 uint32_t
228 {
229  return m_simulator->GetContext ();
230 }
231 
232 uint64_t
234 {
235  return m_simulator->GetEventCount ();
236 }
237 
238 void
240 {
241  m_simulator->Run ();
242 }
243 
244 
245 } // namespace ns3
246 
247 
static TypeId GetTypeId(void)
Register this type.
An identifier for simulation events.
Definition: event-id.h:54
A simulation event.
Definition: event-impl.h:45
Instantiate subclasses of ns3::Object.
Ptr< Object > Create(void) 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(void)
Destructor implementation.
Definition: object.cc:346
static void Enable(void)
Enable the packet metadata.
The SimulatorImpl base class.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
A replacement simulator that starts the visualizer.
virtual void SetScheduler(ObjectFactory schedulerFactory)
Set the Scheduler to be used to manage the event list.
virtual uint32_t GetContext(void) const
Get the current simulation context.
virtual Time Now(void) const
Return the current simulation virtual time.
virtual void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static TypeId GetTypeId(void)
Get the type ID.
void DoDispose()
Destructor implementation.
virtual void Destroy()
Execute the events scheduled with ScheduleDestroy().
virtual void Remove(const EventId &id)
Remove an event from the event list.
virtual void Stop(void)
Tell the Simulator the calling event should be the last one executed.
virtual void ScheduleWithContext(uint32_t context, Time const &delay, EventImpl *event)
Schedule a future event execution (in a different context).
void RunRealSimulator(void)
calls Run() in the wrapped simulator
ObjectFactory m_simulatorImplFactory
simulator implementation factory
virtual Time GetMaximumSimulationTime(void) const
Get the maximum representable simulation time.
Ptr< SimulatorImpl > m_simulator
the simulator implementation
void NotifyConstructionCompleted(void)
Notifier called once the ObjectBase is fully constructed.
virtual uint32_t GetSystemId(void) const
Get the system id of this simulator.
virtual void Run(void)
Run the simulation.
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...
virtual EventId Schedule(Time const &delay, EventImpl *event)
Schedule a future event execution (in the same context).
virtual Time GetDelayLeft(const EventId &id) const
Get the remaining time until this event will execute.
virtual bool IsFinished(void) const
Check if the simulation should finish.
virtual uint64_t GetEventCount(void) const
Get the number of events executed.
virtual bool IsExpired(const EventId &id) const
Check if an event has already run or been cancelled.
virtual EventId ScheduleNow(EventImpl *event)
Schedule an event to run at the current virtual time.
Ptr< const AttributeAccessor > MakeObjectFactoryAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeChecker > MakeObjectFactoryChecker(void)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
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.