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  const char *argv[] = { "python", NULL};
118  Py_Initialize ();
119  PySys_SetArgv (1, (char**) argv);
120  PyRun_SimpleString (
121  "import visualizer\n"
122  "visualizer.start();\n"
123  );
124  }
125  else
126  {
127  PyGILState_STATE __py_gil_state = PyGILState_Ensure ();
128 
129  PyRun_SimpleString (
130  "import visualizer\n"
131  "visualizer.start();\n"
132  );
133 
134  PyGILState_Release (__py_gil_state);
135  }
136 }
137 
138 void
140 {
141  m_simulator->Stop ();
142 }
143 
144 void
146 {
147  m_simulator->Stop (delay);
148 }
149 
150 //
151 // Schedule an event for a _relative_ time in the future.
152 //
153 EventId
155 {
156  return m_simulator->Schedule (delay, event);
157 }
158 
159 void
160 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &delay, EventImpl *event)
161 {
162  m_simulator->ScheduleWithContext (context, delay, event);
163 }
164 
165 EventId
167 {
168  return m_simulator->ScheduleNow (event);
169 }
170 
171 EventId
173 {
174  return m_simulator->ScheduleDestroy (event);
175 }
176 
177 Time
179 {
180  return m_simulator->Now ();
181 }
182 
183 Time
185 {
186  return m_simulator->GetDelayLeft (id);
187 }
188 
189 void
191 {
192  m_simulator->Remove (id);
193 }
194 
195 void
197 {
198  m_simulator->Cancel (id);
199 }
200 
201 bool
203 {
204  return m_simulator->IsExpired (id);
205 }
206 
207 Time
209 {
210  return m_simulator->GetMaximumSimulationTime ();
211 }
212 
213 uint32_t
215 {
216  return m_simulator->GetContext ();
217 }
218 
219 void
221 {
222  m_simulator->Run ();
223 }
224 
225 
226 } // namespace ns3
227 
228 
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.
ObjectFactory GetDefaultSimulatorImplFactory()
Get an object factory configured to the default simulator implementation.
void DoDispose()
Destructor implementation.
ObjectFactory m_simulatorImplFactory
simulator implementation factory
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
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 bool IsFinished(void) const
Check if the simulation should finish.
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.
virtual Time GetMaximumSimulationTime(void) const
Get the maximum representable simulation time.
Ptr< const AttributeAccessor > MakeObjectFactoryAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
virtual Time GetDelayLeft(const EventId &id) const
Get the remaining time until this event will execute.
virtual bool IsExpired(const EventId &id) const
Check if an event has already run or been cancelled.
virtual void Destroy()
Execute the events scheduled with ScheduleDestroy().
virtual EventId ScheduleNow(EventImpl *event)
Schedule an event to run at the current virtual time.
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 uint32_t GetSystemId(void) const
Get the system id of this simulator.
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...
virtual Time Now(void) const
Return the current simulation virtual time.
a unique identifier for an interface.
Definition: type-id.h:58
virtual uint32_t GetContext(void) const
Get the current simulation context.
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:914
static TypeId GetTypeId(void)
Get the type ID.
The SimulatorImpl base class.