A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
visual-simulator-impl.cc
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 NS_LOG_COMPONENT_DEFINE ("VisualSimulatorImpl");
26 
27 namespace ns3 {
28 
29 
30 
31 NS_OBJECT_ENSURE_REGISTERED (VisualSimulatorImpl);
32 
33 namespace
34 {
36 GetDefaultSimulatorImplFactory ()
37 {
38  ObjectFactory factory;
39  factory.SetTypeId (DefaultSimulatorImpl::GetTypeId ());
40  return factory;
41 }
42 }
43 
44 
45 TypeId
46 VisualSimulatorImpl::GetTypeId (void)
47 {
48  static TypeId tid = TypeId ("ns3::VisualSimulatorImpl")
50  .AddConstructor<VisualSimulatorImpl> ()
51  .AddAttribute ("SimulatorImplFactory",
52  "Factory for the underlying simulator implementation used by the visualizer.",
53  ObjectFactoryValue (GetDefaultSimulatorImplFactory ()),
54  MakeObjectFactoryAccessor (&VisualSimulatorImpl::m_simulatorImplFactory),
55  MakeObjectFactoryChecker ())
56  ;
57  return tid;
58 }
59 
60 
61 VisualSimulatorImpl::VisualSimulatorImpl ()
62 {
63 }
64 
65 VisualSimulatorImpl::~VisualSimulatorImpl ()
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 {
83  m_simulator = m_simulatorImplFactory.Create<SimulatorImpl> ();
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 (time);
148 }
149 
150 //
151 // Schedule an event for a _relative_ time in the future.
152 //
153 EventId
155 {
156  return m_simulator->Schedule (time, event);
157 }
158 
159 void
160 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, EventImpl *event)
161 {
162  m_simulator->ScheduleWithContext (context, time, 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 (ev);
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 
keep track of time unit.
Definition: nstime.h:149
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void SetTypeId(TypeId tid)
virtual void DoDispose(void)
Definition: object.cc:335
virtual void ScheduleWithContext(uint32_t context, Time const &time, EventImpl *event)
virtual void SetScheduler(ObjectFactory schedulerFactory)
hold objects of type ns3::ObjectFactory
virtual void Cancel(const EventId &ev)
virtual bool IsFinished(void) const
Ptr< Object > Create(void) const
virtual Time GetMaximumSimulationTime(void) const
virtual EventId Schedule(Time const &time, EventImpl *event)
virtual void Remove(const EventId &ev)
virtual Time GetDelayLeft(const EventId &id) const
virtual EventId ScheduleNow(EventImpl *event)
void RunRealSimulator(void)
calls Run() in the wrapped simulator
instantiate subclasses of ns3::Object.
a simulation event
Definition: event-impl.h:39
an identifier for simulation events.
Definition: event-id.h:46
virtual bool IsExpired(const EventId &ev) const
virtual uint32_t GetSystemId(void) const
virtual Time Now(void) const
a unique identifier for an interface.
Definition: type-id.h:44
virtual uint32_t GetContext(void) const
virtual EventId ScheduleDestroy(EventImpl *event)
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471