Merging with head
[nepi.git] / src / nepi / testbeds / ns3 / execute.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
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
8 import os
9 import sys
10 import threading
11 import random
12 import socket
13 import weakref
14
15 def init():
16     if 'ns3' in sys.modules:
17         return
18
19     import ctypes
20     import imp
21
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
26
27     if libfile:
28         ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
29         """
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]
33         i = 0
34         while len(libs) > 0:
35             i += 1
36             lib = libs[ i % len(libs)]
37             libfile = os.path.join(build_path, lib)
38             try:
39                 ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
40                 libs.remove(lib)
41             except:
42                 pass
43         """
44
45         path = [ os.path.dirname(__file__) ] + sys.path
46         if bindings:
47             path = [ bindings ] + path
48
49         try:
50             module = imp.find_module ('ns3', path)
51             mod = imp.load_module ('ns3', *module)
52         except ImportError:
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
58             
59             # When using _ns3, we have to make sure we destroy
60             # the simulator when the process finishes
61             import atexit
62             atexit.register(mod.Simulator.Destroy)
63
64  
65
66 class TestbedController(testbed_impl.TestbedController):
67     from nepi.util.tunchannel_impl import TunChannel
68     
69     LOCAL_FACTORIES = {
70         'ns3::Nepi::TunChannel' : TunChannel,
71     }
72     
73     LOCAL_TYPES = tuple(LOCAL_FACTORIES.values())
74
75     def __init__(self):
76         super(TestbedController, self).__init__(TESTBED_ID, TESTBED_VERSION)
77         self._ns3 = None
78         self._home_directory = None
79         self._traces = dict()
80         self._simulator_thread = None
81         self._condition = None
82
83     @property
84     def home_directory(self):
85         return self._home_directory
86
87     @property
88     def ns3(self):
89         return self._ns3
90
91     def do_setup(self):
92         self._home_directory = self._attributes.\
93             get_attribute_value("homeDirectory")
94         self._ns3 = self._load_ns3_module()
95         
96         # create home...
97         home = os.path.normpath(self.home_directory)
98         if not os.path.exists(home):
99             os.makedirs(home, 0755)
100         
101         super(TestbedController, self).do_setup()
102
103     def start(self):
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()
110
111     def stop(self, time = TIME_NOW):
112         super(TestbedController, self).stop(time)
113         #self.ns3.Simulator.Stop()
114         self._stop_simulation(time)
115
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)
127
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)
137             else:
138                 return value
139         if factory.box_attributes.is_attribute_metadata(name):
140             return value
141
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" % \
147                 (name, guid))
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:
154             return int(value)
155         if attr_type == Attribute.DOUBLE:
156             return float(value)
157         if attr_type == Attribute.BOOL:
158             return value == "true"
159         return value
160
161     def action(self, time, guid, action):
162         raise NotImplementedError
163
164     def trace_filepath(self, guid, trace_id):
165         filename = self._traces[guid][trace_id]
166         return os.path.join(self.home_directory, filename)
167
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
172
173     def shutdown(self):
174         for element in self._elements.itervalues():
175             if isinstance(element, self.LOCAL_TYPES):
176                 # graceful shutdown of locally-implemented objects
177                 element.Cleanup()
178         if self.ns3:
179             self.ns3.Simulator.Stop()
180             
181             # Wait for it to stop, with a 30s timeout
182             for i in xrange(300):
183                 if self.ns3.Simulator.IsFinished():
184                     break
185                 time.sleep(0.1)
186             #self._stop_simulation("0s")
187         
188         self._elements.clear()
189         
190         if self.ns3:
191             # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
192             #   if self._simulator_thread:
193             #       self._simulator_thread.join()
194             self.ns3.Simulator.Destroy()
195         
196         self._ns3 = None
197         sys.stdout.flush()
198         sys.stderr.flush()
199
200     def _simulator_run(self, condition):
201         # Run simulation
202         self.ns3.Simulator.Run()
203         # Signal condition on simulation end to notify waiting threads
204         condition.acquire()
205         condition.notifyAll()
206         condition.release()
207
208     def _schedule_event(self, condition, func, *args):
209         """Schedules event on running experiment"""
210         def execute_event(condition, has_event_occurred, func, *args):
211             # exec func
212             try:
213                 func(*args)
214             finally:
215                 # flag event occured
216                 has_event_occurred[0] = True
217                 # notify condition indicating attribute was set
218                 condition.acquire()
219                 condition.notifyAll()
220                 condition.release()
221
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]
230         condition.acquire()
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():
235                 condition.wait()
236                 condition.release()
237
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)
243         else:
244             self._set_ns3_attribute(name, ns3_value, element)
245
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)
251         else:
252             self._get_ns3_attribute(name, ns3_value, element)
253
254     def _set_ns3_attribute(self, name, ns3_value, element):
255         element.SetAttribute(name, ns3_value)
256
257     def _get_ns3_attribute(self, name, ns3_value, element):
258         element.GetAttribute(name, ns3_value)
259
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, 
264                     time)
265         else:
266             self._stop_ns3_simulation(time)
267
268     def _stop_ns3_simulation(self, time = TIME_NOW):
269         if not self.ns3:
270             return
271         if time == TIME_NOW:
272             self.ns3.Simulator.Stop()
273         else:
274             self.ns3.Simulator.Stop(self.ns3.Time(time))
275
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)
290         return ns3_value
291
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")
297
298         init()
299
300         import ns3 as mod
301  
302         if simu_impl_type:
303             value = mod.StringValue(simu_impl_type)
304             mod.GlobalValue.Bind ("SimulatorImplementationType", value)
305         if checksum:
306             value = mod.BooleanValue(checksum)
307             mod.GlobalValue.Bind ("ChecksumEnabled", value)
308         if stop_time:
309             value = mod.Time(stop_time)
310             mod.Simulator.Stop (value)
311         return mod
312
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)
322             if found and \
323                 (info.flags & TypeId.ATTR_CONSTRUCT == TypeId.ATTR_CONSTRUCT):
324                 construct_params[name] = value
325         return construct_params
326
327
328