1 # -*- coding: utf-8 -*-
3 from util import _get_ipv4_protocol_guid, _get_node_guid, _get_dev_number
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
15 def load_ns3_module():
17 if 'ns3' in sys.modules:
24 bindings = os.environ["NEPI_NS3BINDINGS"] \
25 if "NEPI_NS3BINDINGS" in os.environ else None
26 libdir = os.environ["NEPI_NS3LIBRARY"] \
27 if "NEPI_NS3LIBRARY" in os.environ else None
30 files = os.listdir(libdir)
31 regex = re.compile("(.*\.so)$")
32 libs = [m.group(1) for filename in files for m in [regex.search(filename)] if m]
37 libfile = os.path.join(libdir, lib)
39 ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
43 # if did not load any libraries in the last iteration
44 if len(libscp) == len(libs):
45 raise RuntimeError("Imposible to load shared libraries %s" % str(libs))
49 sys.path.append(bindings)
51 import ns3_bindings_import as mod
52 sys.modules["ns3"] = mod
54 class TestbedController(testbed_impl.TestbedController):
55 from nepi.util.tunchannel_impl import TunChannel
58 'ns3::Nepi::TunChannel' : TunChannel,
61 DUMMY_FACTORIES = ['ns3::Nepi::MobilityPair']
63 LOCAL_TYPES = tuple(LOCAL_FACTORIES.values())
66 super(TestbedController, self).__init__(TESTBED_ID, TESTBED_VERSION)
68 self._home_directory = None
70 self._simulator_thread = None
71 self._condition = None
74 def home_directory(self):
75 return self._home_directory
82 self._home_directory = self._attributes.\
83 get_attribute_value("homeDirectory")
84 self._ns3 = self._configure_ns3_module()
87 home = os.path.normpath(self.home_directory)
88 if not os.path.exists(home):
89 os.makedirs(home, 0755)
91 super(TestbedController, self).do_setup()
94 super(TestbedController, self).start()
95 self._condition = threading.Condition()
96 self._simulator_thread = threading.Thread(target = self._simulator_run,
97 args = [self._condition])
98 self._simulator_thread.setDaemon(True)
99 self._simulator_thread.start()
101 def stop(self, time = TIME_NOW):
102 super(TestbedController, self).stop(time)
103 self._stop_simulation(time)
105 def set(self, guid, name, value, time = TIME_NOW):
106 super(TestbedController, self).set(guid, name, value, time)
108 # TODO: take on account schedule time for the task
109 factory_id = self._create[guid]
110 if factory_id in self.DUMMY_FACTORIES:
113 factory = self._factories[factory_id]
114 element = self._elements[guid]
115 if factory_id in self.LOCAL_FACTORIES:
116 setattr(element, name, value)
117 elif not factory.box_attributes.is_attribute_metadata(name):
119 ipv4_guid = _get_ipv4_protocol_guid(self, guid)
120 if not ipv4_guid in self._elements:
122 ipv4 = self._elements[ipv4_guid]
124 nint = ipv4.GetNInterfaces()
125 for i in xrange(0, nint):
128 nint = ipv4.GetNInterfaces()
129 for i in xrange(0, nint):
132 ns3_value = self._to_ns3_value(guid, name, value)
133 self._set_attribute(name, ns3_value, element)
135 def get(self, guid, name, time = TIME_NOW):
136 value = super(TestbedController, self).get(guid, name, time)
138 # TODO: take on account schedule time for the task
139 factory_id = self._create[guid]
140 if factory_id in self.DUMMY_FACTORIES:
143 factory = self._factories[factory_id]
144 element = self._elements[guid]
145 if factory_id in self.LOCAL_FACTORIES:
146 if hasattr(element, name):
147 return getattr(element, name)
152 ipv4_guid = _get_ipv4_protocol_guid(self, guid)
153 if not ipv4_guid in self._elements:
155 ipv4 = self._elements[ipv4_guid]
156 nint = ipv4.GetNInterfaces()
158 for i in xrange(0, nint):
163 if factory.box_attributes.is_attribute_metadata(name):
166 TypeId = self.ns3.TypeId()
167 typeid = TypeId.LookupByName(factory_id)
168 info = TypeId.AttributeInformation()
169 if not typeid or not typeid.LookupAttributeByName(name, info):
170 raise AttributeError("Invalid attribute %s for element type %d" % \
172 checker = info.checker
173 ns3_value = checker.Create()
174 self._get_attribute(name, ns3_value, element)
175 value = ns3_value.SerializeToString(checker)
176 attr_type = factory.box_attributes.get_attribute_type(name)
178 if attr_type == Attribute.INTEGER:
180 if attr_type == Attribute.DOUBLE:
182 if attr_type == Attribute.BOOL:
183 return value == "true"
186 def action(self, time, guid, action):
187 raise NotImplementedError
189 def trace_filepath(self, guid, trace_id):
190 filename = self._traces[guid][trace_id]
191 return os.path.join(self.home_directory, filename)
193 def trace_filename(self, guid, trace_id):
194 return self._traces[guid][trace_id]
196 def follow_trace(self, guid, trace_id, filename):
197 if not guid in self._traces:
198 self._traces[guid] = dict()
199 self._traces[guid][trace_id] = filename
202 for element in self._elements.itervalues():
203 if isinstance(element, self.LOCAL_TYPES):
204 # graceful shutdown of locally-implemented objects
207 if not self.ns3.Simulator.IsFinished():
210 # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
211 if self._simulator_thread:
212 self._simulator_thread.join()
214 self.ns3.Simulator.Destroy()
216 self._elements.clear()
222 def _simulator_run(self, condition):
224 self.ns3.Simulator.Run()
225 # Signal condition on simulation end to notify waiting threads
227 condition.notifyAll()
230 def _schedule_event(self, condition, func, *args):
231 """Schedules event on running experiment"""
232 def execute_event(contextId, condition, has_event_occurred, func, *args):
238 has_event_occurred[0] = True
239 # notify condition indicating attribute was set
241 condition.notifyAll()
244 # contextId is defined as general context
245 contextId = long(0xffffffff)
246 # delay 0 means that the event is expected to execute inmediately
247 delay = self.ns3.Seconds(0)
248 # flag to indicate that the event occured
249 # because bool is an inmutable object in python, in order to create a
250 # bool flag, a list is used as wrapper
251 has_event_occurred = [False]
254 if not self.ns3.Simulator.IsFinished():
255 self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event,
256 condition, has_event_occurred, func, *args)
257 while not has_event_occurred[0] and not self.ns3.Simulator.IsFinished():
262 def _set_attribute(self, name, ns3_value, element):
263 if self.status() == TS.STATUS_STARTED:
264 # schedule the event in the Simulator
265 self._schedule_event(self._condition, self._set_ns3_attribute,
266 name, ns3_value, element)
268 self._set_ns3_attribute(name, ns3_value, element)
270 def _get_attribute(self, name, ns3_value, element):
271 if self.status() == TS.STATUS_STARTED:
272 # schedule the event in the Simulator
273 self._schedule_event(self._condition, self._get_ns3_attribute,
274 name, ns3_value, element)
276 self._get_ns3_attribute(name, ns3_value, element)
278 def _set_ns3_attribute(self, name, ns3_value, element):
279 element.SetAttribute(name, ns3_value)
281 def _get_ns3_attribute(self, name, ns3_value, element):
282 element.GetAttribute(name, ns3_value)
284 def _stop_simulation(self, time):
285 if self.status() == TS.STATUS_STARTED:
286 # schedule the event in the Simulator
287 self._schedule_event(self._condition, self._stop_ns3_simulation,
290 self._stop_ns3_simulation(time)
292 def _stop_ns3_simulation(self, time = TIME_NOW):
296 self.ns3.Simulator.Stop()
298 self.ns3.Simulator.Stop(self.ns3.Time(time))
300 def _to_ns3_value(self, guid, name, value):
301 factory_id = self._create[guid]
302 TypeId = self.ns3.TypeId()
303 typeid = TypeId.LookupByName(factory_id)
304 info = TypeId.AttributeInformation()
305 if not typeid.LookupAttributeByName(name, info):
306 raise RuntimeError("Attribute %s doesn't belong to element %s" \
307 % (name, factory_id))
308 str_value = str(value)
309 if isinstance(value, bool):
310 str_value = str_value.lower()
311 checker = info.checker
312 ns3_value = checker.Create()
313 ns3_value.DeserializeFromString(str_value, checker)
316 def _configure_ns3_module(self):
317 simu_impl_type = self._attributes.get_attribute_value(
318 "SimulatorImplementationType")
319 sched_impl_type = self._attributes.get_attribute_value(
321 checksum = self._attributes.get_attribute_value("ChecksumEnabled")
322 stop_time = self._attributes.get_attribute_value("StopTime")
329 value = mod.StringValue(simu_impl_type)
330 mod.GlobalValue.Bind ("SimulatorImplementationType", value)
332 value = mod.StringValue(sched_impl_type)
333 mod.GlobalValue.Bind ("SchedulerType", value)
335 value = mod.BooleanValue(checksum)
336 mod.GlobalValue.Bind ("ChecksumEnabled", value)
338 value = mod.Time(stop_time)
339 mod.Simulator.Stop (value)
342 def _get_construct_parameters(self, guid):
343 params = self._get_parameters(guid)
344 construct_params = dict()
345 factory_id = self._create[guid]
346 TypeId = self.ns3.TypeId()
347 typeid = TypeId.LookupByName(factory_id)
348 for name, value in params.iteritems():
349 info = self.ns3.TypeId.AttributeInformation()
350 found = typeid.LookupAttributeByName(name, info)
352 (info.flags & TypeId.ATTR_CONSTRUCT == TypeId.ATTR_CONSTRUCT):
353 construct_params[name] = value
354 return construct_params