2 # -*- coding: utf-8 -*-
4 from nepi.core import testbed_impl
5 from nepi.core.attributes import Attribute
6 from constants import TESTBED_ID, TESTBED_VERSION
7 from nepi.util.constants import TIME_NOW, TestbedStatus as TS
16 if 'ns3' in sys.modules:
22 bindings = os.environ["NEPI_NS3BINDINGS"] \
23 if "NEPI_NS3BINDINGS" in os.environ else None
24 libfile = os.environ["NEPI_NS3LIBRARY"] \
25 if "NEPI_NS3LIBRARY" in os.environ else None
28 ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
30 files = os.listdir(build_path)
31 regex = re.compile("(.*\.so)$")
32 libs = [m.group(1) for filename in files for m in [regex.search(filename)] if m]
36 lib = libs[ i % len(libs)]
37 libfile = os.path.join(build_path, lib)
39 ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
45 path = [ os.path.dirname(__file__) ] + sys.path
47 path = [ bindings ] + path
50 module = imp.find_module ('ns3', path)
51 mod = imp.load_module ('ns3', *module)
53 # In some environments, ns3 per-se does not exist,
54 # only the low-level _ns3
55 module = imp.find_module ('_ns3', path)
56 mod = imp.load_module ('_ns3', *module)
57 sys.modules["ns3"] = mod # install it as ns3 too
59 # When using _ns3, we have to make sure we destroy
60 # the simulator when the process finishes
62 atexit.register(mod.Simulator.Destroy)
66 class TestbedController(testbed_impl.TestbedController):
67 from nepi.util.tunchannel_impl import TunChannel
70 'ns3::Nepi::TunChannel' : TunChannel,
73 LOCAL_TYPES = tuple(LOCAL_FACTORIES.values())
76 super(TestbedController, self).__init__(TESTBED_ID, TESTBED_VERSION)
78 self._home_directory = None
80 self._simulator_thread = None
81 self._condition = None
84 def home_directory(self):
85 return self._home_directory
92 self._home_directory = self._attributes.\
93 get_attribute_value("homeDirectory")
94 self._ns3 = self._load_ns3_module()
97 home = os.path.normpath(self.home_directory)
98 if not os.path.exists(home):
99 os.makedirs(home, 0755)
101 super(TestbedController, self).do_setup()
104 super(TestbedController, self).start()
105 self._condition = threading.Condition()
106 self._simulator_thread = threading.Thread(target = self._simulator_run,
107 args = [self._condition])
108 self._simulator_thread.setDaemon(True)
109 self._simulator_thread.start()
111 def stop(self, time = TIME_NOW):
112 super(TestbedController, self).stop(time)
113 #self.ns3.Simulator.Stop()
114 self._stop_simulation(time)
116 def set(self, guid, name, value, time = TIME_NOW):
117 super(TestbedController, self).set(guid, name, value, time)
118 # TODO: take on account schedule time for the task
119 factory_id = self._create[guid]
120 factory = self._factories[factory_id]
121 element = self._elements[guid]
122 if factory_id in self.LOCAL_FACTORIES:
123 setattr(element, name, value)
124 elif not factory.box_attributes.is_attribute_metadata(name):
125 ns3_value = self._to_ns3_value(guid, name, value)
126 self._set_attribute(name, ns3_value, element)
128 def get(self, guid, name, time = TIME_NOW):
129 value = super(TestbedController, self).get(guid, name, time)
130 # TODO: take on account schedule time for the task
131 factory_id = self._create[guid]
132 factory = self._factories[factory_id]
133 element = self._elements[guid]
134 if factory_id in self.LOCAL_FACTORIES:
135 if hasattr(element, name):
136 return getattr(element, name)
139 if factory.box_attributes.is_attribute_metadata(name):
142 TypeId = self.ns3.TypeId()
143 typeid = TypeId.LookupByName(factory_id)
144 info = TypeId.AttributeInfo()
145 if not typeid or not typeid.LookupAttributeByName(name, info):
146 raise AttributeError("Invalid attribute %s for element type %d" % \
148 checker = info.checker
149 ns3_value = checker.Create()
150 self._get_attribute(name, ns3_value, element)
151 value = ns3_value.SerializeToString(checker)
152 attr_type = factory.box_attributes.get_attribute_type(name)
153 if attr_type == Attribute.INTEGER:
155 if attr_type == Attribute.DOUBLE:
157 if attr_type == Attribute.BOOL:
158 return value == "true"
161 def action(self, time, guid, action):
162 raise NotImplementedError
164 def trace_filepath(self, guid, trace_id):
165 filename = self._traces[guid][trace_id]
166 return os.path.join(self.home_directory, filename)
168 def follow_trace(self, guid, trace_id, filename):
169 if not guid in self._traces:
170 self._traces[guid] = dict()
171 self._traces[guid][trace_id] = filename
174 for element in self._elements.itervalues():
175 if isinstance(element, self.LOCAL_TYPES):
176 # graceful shutdown of locally-implemented objects
179 self.ns3.Simulator.Stop()
181 # Wait for it to stop, with a 30s timeout
182 for i in xrange(300):
183 if self.ns3.Simulator.IsFinished():
186 #self._stop_simulation("0s")
188 self._elements.clear()
191 # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
192 # if self._simulator_thread:
193 # self._simulator_thread.join()
194 self.ns3.Simulator.Destroy()
200 def _simulator_run(self, condition):
202 self.ns3.Simulator.Run()
203 # Signal condition on simulation end to notify waiting threads
205 condition.notifyAll()
208 def _schedule_event(self, condition, func, *args):
209 """Schedules event on running experiment"""
210 def execute_event(condition, has_event_occurred, func, *args):
216 has_event_occurred[0] = True
217 # notify condition indicating attribute was set
219 condition.notifyAll()
222 # contextId is defined as general context
223 contextId = long(0xffffffff)
224 # delay 0 means that the event is expected to execute inmediately
225 delay = self.ns3.Seconds(0)
226 # flag to indicate that the event occured
227 # because bool is an inmutable object in python, in order to create a
228 # bool flag, a list is used as wrapper
229 has_event_occurred = [False]
231 if not self.ns3.Simulator.IsFinished():
232 self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event,
233 condition, has_event_occurred, func, *args)
234 while not has_event_occurred[0] and not self.ns3.Simulator.IsFinished():
238 def _set_attribute(self, name, ns3_value, element):
239 if self.status() == TS.STATUS_STARTED:
240 # schedule the event in the Simulator
241 self._schedule_event(self._condition, self._set_ns3_attribute,
242 name, ns3_value, element)
244 self._set_ns3_attribute(name, ns3_value, element)
246 def _get_attribute(self, name, ns3_value, element):
247 if self.status() == TS.STATUS_STARTED:
248 # schedule the event in the Simulator
249 self._schedule_event(self._condition, self._get_ns3_attribute,
250 name, ns3_value, element)
252 self._get_ns3_attribute(name, ns3_value, element)
254 def _set_ns3_attribute(self, name, ns3_value, element):
255 element.SetAttribute(name, ns3_value)
257 def _get_ns3_attribute(self, name, ns3_value, element):
258 element.GetAttribute(name, ns3_value)
260 def _stop_simulation(self, time):
261 if self.status() == TS.STATUS_STARTED:
262 # schedule the event in the Simulator
263 self._schedule_event(self._condition, self._stop_ns3_simulation,
266 self._stop_ns3_simulation(time)
268 def _stop_ns3_simulation(self, time = TIME_NOW):
272 self.ns3.Simulator.Stop()
274 self.ns3.Simulator.Stop(self.ns3.Time(time))
276 def _to_ns3_value(self, guid, name, value):
277 factory_id = self._create[guid]
278 TypeId = self.ns3.TypeId()
279 typeid = TypeId.LookupByName(factory_id)
280 info = TypeId.AttributeInfo()
281 if not typeid.LookupAttributeByName(name, info):
282 raise RuntimeError("Attribute %s doesn't belong to element %s" \
283 % (name, factory_id))
284 str_value = str(value)
285 if isinstance(value, bool):
286 str_value = str_value.lower()
287 checker = info.checker
288 ns3_value = checker.Create()
289 ns3_value.DeserializeFromString(str_value, checker)
292 def _load_ns3_module(self):
293 simu_impl_type = self._attributes.get_attribute_value(
294 "SimulatorImplementationType")
295 checksum = self._attributes.get_attribute_value("ChecksumEnabled")
296 stop_time = self._attributes.get_attribute_value("StopTime")
303 value = mod.StringValue(simu_impl_type)
304 mod.GlobalValue.Bind ("SimulatorImplementationType", value)
306 value = mod.BooleanValue(checksum)
307 mod.GlobalValue.Bind ("ChecksumEnabled", value)
309 value = mod.Time(stop_time)
310 mod.Simulator.Stop (value)
313 def _get_construct_parameters(self, guid):
314 params = self._get_parameters(guid)
315 construct_params = dict()
316 factory_id = self._create[guid]
317 TypeId = self.ns3.TypeId()
318 typeid = TypeId.LookupByName(factory_id)
319 for name, value in params.iteritems():
320 info = self.ns3.TypeId.AttributeInfo()
321 found = typeid.LookupAttributeByName(name, info)
323 (info.flags & TypeId.ATTR_CONSTRUCT == TypeId.ATTR_CONSTRUCT):
324 construct_params[name] = value
325 return construct_params