Shutdown fix: wait for the simulator to stop running before dereferencing elements.
[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
7 from nepi.util.constants import TIME_NOW, \
8     TESTBED_STATUS_STARTED
9 import os
10 import sys
11 import threading
12 import random
13 import socket
14 import weakref
15
16 class TestbedController(testbed_impl.TestbedController):
17     from nepi.util.tunchannel_impl import TunChannel
18     
19     LOCAL_FACTORIES = {
20         'ns3::Nepi::TunChannel' : TunChannel,
21     }
22     
23     LOCAL_TYPES = tuple(LOCAL_FACTORIES.values())
24
25     def __init__(self, testbed_version):
26         super(TestbedController, self).__init__(TESTBED_ID, testbed_version)
27         self._ns3 = None
28         self._home_directory = None
29         self._traces = dict()
30         self._simulator_thread = None
31         self._condition = None
32
33     @property
34     def home_directory(self):
35         return self._home_directory
36
37     @property
38     def ns3(self):
39         return self._ns3
40
41     def do_setup(self):
42         self._home_directory = self._attributes.\
43             get_attribute_value("homeDirectory")
44         self._ns3 = self._load_ns3_module()
45         
46         # create home...
47         home = os.path.normpath(self.home_directory)
48         if not os.path.exists(home):
49             os.makedirs(home, 0755)
50         
51         super(TestbedController, self).do_setup()
52
53     def start(self):
54         super(TestbedController, self).start()
55         self._condition = threading.Condition()
56         self._simulator_thread = threading.Thread(target = self._simulator_run,
57                 args = [self._condition])
58         self._simulator_thread.setDaemon(True)
59         self._simulator_thread.start()
60
61     def stop(self, time = TIME_NOW):
62         super(TestbedController, self).stop(time)
63         #self.ns3.Simulator.Stop()
64         self._stop_simulation(time)
65
66     def set(self, guid, name, value, time = TIME_NOW):
67         super(TestbedController, self).set(guid, name, value, time)
68         # TODO: take on account schedule time for the task
69         factory_id = self._create[guid]
70         factory = self._factories[factory_id]
71         if factory.box_attributes.is_attribute_design_only(name):
72             return
73         element = self._elements[guid]
74         if factory_id in self.LOCAL_FACTORIES:
75             setattr(element, name, value)
76         elif factory.box_attributes.is_attribute_invisible(name):
77             return
78         else:
79             ns3_value = self._to_ns3_value(guid, name, value)
80             self._set_attribute(name, ns3_value, element)
81
82     def get(self, guid, name, time = TIME_NOW):
83         value = super(TestbedController, self).get(guid, name, time)
84         # TODO: take on account schedule time for the task
85         factory_id = self._create[guid]
86         factory = self._factories[factory_id]
87         element = self._elements[guid]
88         if factory_id in self.LOCAL_FACTORIES:
89             if hasattr(element, name):
90                 return getattr(element, name)
91             else:
92                 return value
93         if factory.box_attributes.is_attribute_design_only(name) or \
94                 factory.box_attributes.is_attribute_invisible(name):
95             return value
96         TypeId = self.ns3.TypeId()
97         typeid = TypeId.LookupByName(factory_id)
98         info = TypeId.AttributeInfo()
99         if not typeid or not typeid.LookupAttributeByName(name, info):
100             raise AttributeError("Invalid attribute %s for element type %d" % \
101                 (name, guid))
102         checker = info.checker
103         ns3_value = checker.Create() 
104         self._get_attribute(name, ns3_value, element)
105         value = ns3_value.SerializeToString(checker)
106         attr_type = factory.box_attributes.get_attribute_type(name)
107         if attr_type == Attribute.INTEGER:
108             return int(value)
109         if attr_type == Attribute.DOUBLE:
110             return float(value)
111         if attr_type == Attribute.BOOL:
112             return value == "true"
113         return value
114
115     def action(self, time, guid, action):
116         raise NotImplementedError
117
118     def trace_filepath(self, guid, trace_id):
119         filename = self._traces[guid][trace_id]
120         return os.path.join(self.home_directory, filename)
121
122     def follow_trace(self, guid, trace_id, filename):
123         if not guid in self._traces:
124             self._traces[guid] = dict()
125         self._traces[guid][trace_id] = filename
126
127     def shutdown(self):
128         for element in self._elements.itervalues():
129             if isinstance(element, self.LOCAL_TYPES):
130                 # graceful shutdown of locally-implemented objects
131                 element.Cleanup()
132         if self.ns3:
133             self.ns3.Simulator.Stop()
134             
135             # Wait for it to stop, with a 30s timeout
136             for i in xrange(300):
137                 if self.ns3.Simulator.isFinished():
138                     break
139                 time.sleep(0.1)
140             #self._stop_simulation("0s")
141         
142         self._elements.clear()
143         
144         if self.ns3:
145             # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
146             #   if self._simulator_thread:
147             #       self._simulator_thread.join()
148             self.ns3.Simulator.Destroy()
149         
150         self._ns3 = None
151         sys.stdout.flush()
152         sys.stderr.flush()
153
154     def _simulator_run(self, condition):
155         # Run simulation
156         self.ns3.Simulator.Run()
157         # Signal condition on simulation end to notify waiting threads
158         condition.acquire()
159         condition.notifyAll()
160         condition.release()
161
162     def _schedule_event(self, condition, func, *args):
163         """Schedules event on running experiment"""
164         def execute_event(condition, has_event_occurred, func, *args):
165             # exec func
166             try:
167                 func(*args)
168             finally:
169                 # flag event occured
170                 has_event_occurred[0] = True
171                 # notify condition indicating attribute was set
172                 condition.acquire()
173                 condition.notifyAll()
174                 condition.release()
175
176         # contextId is defined as general context
177         contextId = long(0xffffffff)
178         # delay 0 means that the event is expected to execute inmediately
179         delay = self.ns3.Seconds(0)
180         # flag to indicate that the event occured
181         # because bool is an inmutable object in python, in order to create a
182         # bool flag, a list is used as wrapper
183         has_event_occurred = [False]
184         condition.acquire()
185         if not self.ns3.Simulator.IsFinished():
186             self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event,
187                  condition, has_event_occurred, func, *args)
188             while not has_event_occurred[0] and not self.ns3.Simulator.IsFinished():
189                 condition.wait()
190                 condition.release()
191
192     def _set_attribute(self, name, ns3_value, element):
193         if self.status() == TESTBED_STATUS_STARTED:
194             # schedule the event in the Simulator
195             self._schedule_event(self._condition, self._set_ns3_attribute, 
196                     name, ns3_value, element)
197         else:
198             self._set_ns3_attribute(name, ns3_value, element)
199
200     def _get_attribute(self, name, ns3_value, element):
201         if self.status() == TESTBED_STATUS_STARTED:
202             # schedule the event in the Simulator
203             self._schedule_event(self._condition, self._get_ns3_attribute, 
204                     name, ns3_value, element)
205         else:
206             self._get_ns3_attribute(name, ns3_value, element)
207
208     def _set_ns3_attribute(self, name, ns3_value, element):
209         element.SetAttribute(name, ns3_value)
210
211     def _get_ns3_attribute(self, name, ns3_value, element):
212         element.GetAttribute(name, ns3_value)
213
214     def _stop_simulation(self, time):
215         if self.status() == TESTBED_STATUS_STARTED:
216             # schedule the event in the Simulator
217             self._schedule_event(self._condition, self._stop_ns3_simulation, 
218                     time)
219         else:
220             self._stop_ns3_simulation(time)
221
222     def _stop_simulation(self, time = TIME_NOW):
223         if not self.ns3:
224             return
225         if time == TIME_NOW:
226             self.ns3.Simulator.Stop()
227         else:
228             self.ns3.Simulator.Stop(self.ns3.Time(time))
229
230     def _to_ns3_value(self, guid, name, value):
231         factory_id = self._create[guid]
232         TypeId = self.ns3.TypeId()
233         typeid = TypeId.LookupByName(factory_id)
234         info = TypeId.AttributeInfo()
235         if not typeid.LookupAttributeByName(name, info):
236             raise RuntimeError("Attribute %s doesn't belong to element %s" \
237                    % (name, factory_id))
238         str_value = str(value)
239         if isinstance(value, bool):
240             str_value = str_value.lower()
241         checker = info.checker
242         ns3_value = checker.Create()
243         ns3_value.DeserializeFromString(str_value, checker)
244         return ns3_value
245
246     def _load_ns3_module(self):
247         import ctypes
248         import imp
249
250         simu_impl_type = self._attributes.get_attribute_value(
251                 "SimulatorImplementationType")
252         checksum = self._attributes.get_attribute_value("ChecksumEnabled")
253         stop_time = self._attributes.get_attribute_value("StopTime")
254
255         bindings = os.environ["NEPI_NS3BINDINGS"] \
256                 if "NEPI_NS3BINDINGS" in os.environ else None
257         libfile = os.environ["NEPI_NS3LIBRARY"] \
258                 if "NEPI_NS3LIBRARY" in os.environ else None
259
260         if libfile:
261             ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
262
263         path = [ os.path.dirname(__file__) ] + sys.path
264         if bindings:
265             path = [ bindings ] + path
266
267         try:
268             module = imp.find_module ('ns3', path)
269             mod = imp.load_module ('ns3', *module)
270         except ImportError:
271             # In some environments, ns3 per-se does not exist,
272             # only the low-level _ns3
273             module = imp.find_module ('_ns3', path)
274             mod = imp.load_module ('_ns3', *module)
275             sys.modules["ns3"] = mod # install it as ns3 too
276             
277             # When using _ns3, we have to make sure we destroy
278             # the simulator when the process finishes
279             import atexit
280             atexit.register(mod.Simulator.Destroy)
281     
282         if simu_impl_type:
283             value = mod.StringValue(simu_impl_type)
284             mod.GlobalValue.Bind ("SimulatorImplementationType", value)
285         if checksum:
286             value = mod.BooleanValue(checksum)
287             mod.GlobalValue.Bind ("ChecksumEnabled", value)
288         if stop_time:
289             value = mod.Time(stop_time)
290             mod.Simulator.Stop (value)
291         return mod
292
293     def _get_construct_parameters(self, guid):
294         params = self._get_parameters(guid)
295         construct_params = dict()
296         factory_id = self._create[guid]
297         TypeId = self.ns3.TypeId()
298         typeid = TypeId.LookupByName(factory_id)
299         for name, value in params.iteritems():
300             info = self.ns3.TypeId.AttributeInfo()
301             found = typeid.LookupAttributeByName(name, info)
302             if found and \
303                 (info.flags & TypeId.ATTR_CONSTRUCT == TypeId.ATTR_CONSTRUCT):
304                 construct_params[name] = value
305         return construct_params
306
307
308