A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("VisualSimulatorImpl");
26 
27 namespace ns3 {
28 
29 
30 
31 NS_OBJECT_ENSURE_REGISTERED (VisualSimulatorImpl)
32  ;
33 
34 namespace
35 {
38 {
39  ObjectFactory factory;
41  return factory;
42 }
43 }
44 
45 
46 TypeId
48 {
49  static TypeId tid = TypeId ("ns3::VisualSimulatorImpl")
51  .AddConstructor<VisualSimulatorImpl> ()
52  .AddAttribute ("SimulatorImplFactory",
53  "Factory for the underlying simulator implementation used by the visualizer.",
55  MakeObjectFactoryAccessor (&VisualSimulatorImpl::m_simulatorImplFactory),
56  MakeObjectFactoryChecker ())
57  ;
58  return tid;
59 }
60 
61 
63 {
64 }
65 
67 {
68 }
69 
70 void
72 {
73  if (m_simulator)
74  {
75  m_simulator->Dispose ();
76  m_simulator = NULL;
77  }
79 }
80 
81 void
83 {
85 }
86 
87 
88 void
90 {
91  m_simulator->Destroy ();
92 }
93 
94 void
96 {
97  m_simulator->SetScheduler (schedulerFactory);
98 }
99 
100 // System ID for non-distributed simulation is always zero
101 uint32_t
103 {
104  return m_simulator->GetSystemId ();
105 }
106 
107 bool
109 {
110  return m_simulator->IsFinished ();
111 }
112 
113 void
115 {
116  if (!Py_IsInitialized ())
117  {
118  const char *argv[] = { "python", NULL};
119  Py_Initialize ();
120  PySys_SetArgv (1, (char**) argv);
121  PyRun_SimpleString (
122  "import visualizer\n"
123  "visualizer.start();\n"
124  );
125  }
126  else
127  {
128  PyGILState_STATE __py_gil_state = PyGILState_Ensure ();
129 
130  PyRun_SimpleString (
131  "import visualizer\n"
132  "visualizer.start();\n"
133  );
134 
135  PyGILState_Release (__py_gil_state);
136  }
137 }
138 
139 void
141 {
142  m_simulator->Stop ();
143 }
144 
145 void
147 {
148  m_simulator->Stop (time);
149 }
150 
151 //
152 // Schedule an event for a _relative_ time in the future.
153 //
154 EventId
156 {
157  return m_simulator->Schedule (time, event);
158 }
159 
160 void
161 VisualSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, EventImpl *event)
162 {
163  m_simulator->ScheduleWithContext (context, time, event);
164 }
165 
166 EventId
168 {
169  return m_simulator->ScheduleNow (event);
170 }
171 
172 EventId
174 {
175  return m_simulator->ScheduleDestroy (event);
176 }
177 
178 Time
180 {
181  return m_simulator->Now ();
182 }
183 
184 Time
186 {
187  return m_simulator->GetDelayLeft (id);
188 }
189 
190 void
192 {
193  m_simulator->Remove (id);
194 }
195 
196 void
198 {
199  m_simulator->Cancel (id);
200 }
201 
202 bool
204 {
205  return m_simulator->IsExpired (ev);
206 }
207 
208 Time
210 {
211  return m_simulator->GetMaximumSimulationTime ();
212 }
213 
214 uint32_t
216 {
217  return m_simulator->GetContext ();
218 }
219 
220 void
222 {
223  m_simulator->Run ();
224 }
225 
226 
227 } // namespace ns3
228 
229 
void NotifyConstructionCompleted(void)
This method is invoked once all member attributes have been initialized.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
virtual void Run(void)
Run the simulation until one of:
void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
void SetTypeId(TypeId tid)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
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)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
virtual bool IsFinished(void) const
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
Ptr< Object > Create(void) const
virtual void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::Run method bef...
virtual Time GetMaximumSimulationTime(void) const
virtual EventId Schedule(Time const &time, EventImpl *event)
virtual void Remove(const EventId &ev)
Remove an event from the event list.
virtual Time GetDelayLeft(const EventId &id) const
NS_LOG_COMPONENT_DEFINE("VisualSimulatorImpl")
virtual void Destroy()
This method is typically invoked at the end of a simulation to avoid false-positive reports by a leak...
virtual EventId ScheduleNow(EventImpl *event)
Ptr< SimulatorImpl > m_simulator
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
This method has O(1) complexity.
virtual uint32_t GetSystemId(void) const
virtual Time Now(void) const
Return the "current simulation time".
a unique identifier for an interface.
Definition: type-id.h:49
virtual uint32_t GetContext(void) const
virtual EventId ScheduleDestroy(EventImpl *event)
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
Doxygen introspection did not find any typical Config paths.