Added TCP-handshake for TunChannel and tun_connect.py
[nepi.git] / src / nepi / testbeds / ns3 / execute.py
index 5c3045b..0635f26 100644 (file)
@@ -45,12 +45,9 @@ def load_ns3_module():
                 raise RuntimeError("Imposible to load shared libraries %s" % str(libs))
             libscp = list(libs)
 
-    if not bindings:
-        import ns3
-        sys.modules["ns3"] = ns3
-        return
-    
-    sys.path.append(bindings)
+    if bindings:
+        sys.path.append(bindings)
+
     import ns3_bindings_import as mod
     sys.modules["ns3"] = mod
 
@@ -101,7 +98,6 @@ class TestbedController(testbed_impl.TestbedController):
 
     def stop(self, time = TIME_NOW):
         super(TestbedController, self).stop(time)
-        #self.ns3.Simulator.Stop()
         self._stop_simulation(time)
 
     def set(self, guid, name, value, time = TIME_NOW):
@@ -156,6 +152,9 @@ class TestbedController(testbed_impl.TestbedController):
         filename = self._traces[guid][trace_id]
         return os.path.join(self.home_directory, filename)
 
+    def trace_filename(self, guid, trace_id):
+        return self._traces[guid][trace_id]
+
     def follow_trace(self, guid, trace_id, filename):
         if not guid in self._traces:
             self._traces[guid] = dict()
@@ -165,25 +164,19 @@ class TestbedController(testbed_impl.TestbedController):
         for element in self._elements.itervalues():
             if isinstance(element, self.LOCAL_TYPES):
                 # graceful shutdown of locally-implemented objects
-                element.Cleanup()
+                element.cleanup()
         if self.ns3:
-            self.ns3.Simulator.Stop()
+            if not self.ns3.Simulator.IsFinished():
+                self.stop()
             
-            # Wait for it to stop, with a 30s timeout
-            for i in xrange(300):
-                if self.ns3.Simulator.IsFinished():
-                    break
-                time.sleep(0.1)
-            #self._stop_simulation("0s")
-        
-        self._elements.clear()
-        
-        if self.ns3:
             # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
-            #   if self._simulator_thread:
-            #       self._simulator_thread.join()
+            if self._simulator_thread:
+                self._simulator_thread.join()
+            
             self.ns3.Simulator.Destroy()
         
+        self._elements.clear()
+        
         self._ns3 = None
         sys.stdout.flush()
         sys.stderr.flush()
@@ -219,12 +212,14 @@ class TestbedController(testbed_impl.TestbedController):
         # bool flag, a list is used as wrapper
         has_event_occurred = [False]
         condition.acquire()
-        if not self.ns3.Simulator.IsFinished():
-            self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event,
-                 condition, has_event_occurred, func, *args)
-            while not has_event_occurred[0] and not self.ns3.Simulator.IsFinished():
-                condition.wait()
-                condition.release()
+        try:
+            if not self.ns3.Simulator.IsFinished():
+                self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event,
+                     condition, has_event_occurred, func, *args)
+                while not has_event_occurred[0] and not self.ns3.Simulator.IsFinished():
+                    condition.wait()
+        finally:
+            condition.release()
 
     def _set_attribute(self, name, ns3_value, element):
         if self.status() == TS.STATUS_STARTED:
@@ -283,6 +278,8 @@ class TestbedController(testbed_impl.TestbedController):
     def _configure_ns3_module(self):
         simu_impl_type = self._attributes.get_attribute_value(
                 "SimulatorImplementationType")
+        sched_impl_type = self._attributes.get_attribute_value(
+                "SchedulerType")
         checksum = self._attributes.get_attribute_value("ChecksumEnabled")
         stop_time = self._attributes.get_attribute_value("StopTime")
 
@@ -293,6 +290,9 @@ class TestbedController(testbed_impl.TestbedController):
         if simu_impl_type:
             value = mod.StringValue(simu_impl_type)
             mod.GlobalValue.Bind ("SimulatorImplementationType", value)
+        if sched_impl_type:
+            value = mod.StringValue(sched_impl_type)
+            mod.GlobalValue.Bind ("SchedulerType", value)
         if checksum:
             value = mod.BooleanValue(checksum)
             mod.GlobalValue.Bind ("ChecksumEnabled", value)