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  .AddConstructor<VisualSimulatorImpl> ()
49  .AddAttribute ("SimulatorImplFactory",
50  "Factory for the underlying simulator implementation used by the visualizer.",
54  ;
55  return tid;
56 }
57 
58 
60 {
61 }
62 
64 {
65 }
66 
67 void
69 {
70  if (m_simulator)
71  {
72  m_simulator->Dispose ();
73  m_simulator = NULL;
74  }
76 }
77 
78 void
80 {
82 }
83 
84 
85 void
87 {
88  m_simulator->Destroy ();
89 }
90 
91 void
93 {
94  m_simulator->SetScheduler (schedulerFactory);
95 }
96 
97 // System ID for non-distributed simulation is always zero
98 uint32_t
100 {
101  return m_simulator->GetSystemId ();
102 }
103 
104 bool
106 {
107  return m_simulator->IsFinished ();
108 }
109 
110 void
112 {
113  if (!Py_IsInitialized ())
114  {
115  const char *argv[] = { "python", NULL};
116  Py_Initialize ();
117  PySys_SetArgv (1, (char**) argv);
118  PyRun_SimpleString (
119  "import visualizer\n"
120  "visualizer.start();\n"
121  );
122  }
123  else
124  {
125  PyGILState_STATE __py_gil_state = PyGILState_Ensure ();
126 
127  PyRun_SimpleString (
128  "import visualizer\n"
129  "visualizer.start();\n"
130  );
131 
132  PyGILState_Release (__py_gil_state);
133  }
134 }
135 
136 void
138 {
139  m_simulator->Stop ();
140 }
141 
142 void
144 {
145  m_simulator->Stop (time);
146 }
147 
148 //
149 // Schedule an event for a _relative_ time in the future.
150 //
151 EventId
153 {
154  return m_simulator->Schedule (time, event);
155 }
156 
157 void
158 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, EventImpl *event)
159 {
160  m_simulator->ScheduleWithContext (context, time, event);
161 }
162 
163 EventId
165 {
166  return m_simulator->ScheduleNow (event);
167 }
168 
169 EventId
171 {
172  return m_simulator->ScheduleDestroy (event);
173 }
174 
175 Time
177 {
178  return m_simulator->Now ();
179 }
180 
181 Time
183 {
184  return m_simulator->GetDelayLeft (id);
185 }
186 
187 void
189 {
190  m_simulator->Remove (id);
191 }
192 
193 void
195 {
196  m_simulator->Cancel (id);
197 }
198 
199 bool
201 {
202  return m_simulator->IsExpired (id);
203 }
204 
205 Time
207 {
208  return m_simulator->GetMaximumSimulationTime ();
209 }
210 
211 uint32_t
213 {
214  return m_simulator->GetContext ();
215 }
216 
217 void
219 {
220  m_simulator->Run ();
221 }
222 
223 
224 } // namespace ns3
225 
226 
void NotifyConstructionCompleted(void)
Notifier called once the ObjectBase is fully constructed.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
#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:338
Ptr< const AttributeChecker > MakeObjectFactoryChecker(void)
virtual void ScheduleWithContext(uint32_t context, Time const &time, EventImpl *event)
Schedule a future event execution (in a different context).
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.
AttributeValue implementation for ObjectFactory.
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.
virtual EventId Schedule(Time const &time, EventImpl *event)
Schedule a future event execution (in the same context).
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:51
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)
Definition: type-id.cc:631
The SimulatorImpl base class.