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 {
35 {
36  ObjectFactory factory;
38  return factory;
39 }
40 }
41 
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::VisualSimulatorImpl")
48  .SetGroupName ("Visualizer")
49  .AddConstructor<VisualSimulatorImpl> ()
50  .AddAttribute ("SimulatorImplFactory",
51  "Factory for the underlying simulator implementation used by the visualizer.",
55  ;
56  return tid;
57 }
58 
59 
61 {
62 }
63 
65 {
66 }
67 
68 void
70 {
71  if (m_simulator)
72  {
73  m_simulator->Dispose ();
74  m_simulator = NULL;
75  }
77 }
78 
79 void
81 {
83 }
84 
85 
86 void
88 {
89  m_simulator->Destroy ();
90 }
91 
92 void
94 {
95  m_simulator->SetScheduler (schedulerFactory);
96 }
97 
98 // System ID for non-distributed simulation is always zero
99 uint32_t
101 {
102  return m_simulator->GetSystemId ();
103 }
104 
105 bool
107 {
108  return m_simulator->IsFinished ();
109 }
110 
111 void
113 {
114  if (!Py_IsInitialized ())
115  {
116  const char *argv[] = { "python", NULL};
117  Py_Initialize ();
118  PySys_SetArgv (1, (char**) argv);
119  PyRun_SimpleString (
120  "import visualizer\n"
121  "visualizer.start();\n"
122  );
123  }
124  else
125  {
126  PyGILState_STATE __py_gil_state = PyGILState_Ensure ();
127 
128  PyRun_SimpleString (
129  "import visualizer\n"
130  "visualizer.start();\n"
131  );
132 
133  PyGILState_Release (__py_gil_state);
134  }
135 }
136 
137 void
139 {
140  m_simulator->Stop ();
141 }
142 
143 void
145 {
146  m_simulator->Stop (delay);
147 }
148 
149 //
150 // Schedule an event for a _relative_ time in the future.
151 //
152 EventId
154 {
155  return m_simulator->Schedule (delay, event);
156 }
157 
158 void
159 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &delay, EventImpl *event)
160 {
161  m_simulator->ScheduleWithContext (context, delay, event);
162 }
163 
164 EventId
166 {
167  return m_simulator->ScheduleNow (event);
168 }
169 
170 EventId
172 {
173  return m_simulator->ScheduleDestroy (event);
174 }
175 
176 Time
178 {
179  return m_simulator->Now ();
180 }
181 
182 Time
184 {
185  return m_simulator->GetDelayLeft (id);
186 }
187 
188 void
190 {
191  m_simulator->Remove (id);
192 }
193 
194 void
196 {
197  m_simulator->Cancel (id);
198 }
199 
200 bool
202 {
203  return m_simulator->IsExpired (id);
204 }
205 
206 Time
208 {
209  return m_simulator->GetMaximumSimulationTime ();
210 }
211 
212 uint32_t
214 {
215  return m_simulator->GetContext ();
216 }
217 
218 void
220 {
221  m_simulator->Run ();
222 }
223 
224 
225 } // namespace ns3
226 
227 
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:44
virtual void Run(void)
Run the simulation.
void DoDispose()
Destructor implementation.
#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
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:904
The SimulatorImpl base class.