ns-3 simulator synchronizing start
[nepi.git] / src / nepi / resources / ns3 / ns3wrapper.py
index 69e2b86..f6075b0 100644 (file)
@@ -28,12 +28,12 @@ SINGLETON = "singleton::"
 SIMULATOR_UUID = "singleton::Simulator"
 CONFIG_UUID = "singleton::Config"
 GLOBAL_VALUE_UUID = "singleton::GlobalValue"
+IPV4_GLOBAL_ROUTING_HELPER_UUID = "singleton::Ipv4GlobalRoutingHelper"
 
-def load_ns3_module():
+def load_ns3_libraries():
     import ctypes
     import re
 
-    bindings = os.environ.get("NS3BINDINGS")
     libdir = os.environ.get("NS3LIBRARIES")
 
     # Load the ns-3 modules shared libraries
@@ -61,9 +61,13 @@ def load_ns3_module():
             # to prevent infinit loop
             if initial_size == len(libs):
                 raise RuntimeError("Imposible to load shared libraries %s" % str(libs))
-            initial_size = list(libs)
+            initial_size = len(libs)
+
+def load_ns3_module():
+    load_ns3_libraries()
 
     # import the python bindings for the ns-3 modules
+    bindings = os.environ.get("NS3BINDINGS")
     if bindings:
         sys.path.append(bindings)
 
@@ -208,6 +212,8 @@ class NS3Wrapper(object):
         return uuid
 
     def invoke(self, uuid, operation, *args, **kwargs):
+        if operation == "isRunning":
+            return self._is_running()
         if operation == "isAppRunning":
             return self._is_app_running(uuid)
         if operation == "addStaticRoute":
@@ -227,8 +233,9 @@ class NS3Wrapper(object):
 
         result = method(*realargs, **realkwargs)
 
-        if result is None or \
-                isinstance(result, bool):
+        # If the result is not an object, no need to 
+        # keep a reference. Directly return value.
+        if result is None or type(result) in [bool, float, long, str, int]:
             return result
       
         newuuid = self.make_uuid()
@@ -442,6 +449,16 @@ class NS3Wrapper(object):
 
         return realkwargs
 
+    def _is_running(self): 
+        if self.ns3.Simulator.IsFinished():
+            return False
+
+        now = self.ns3.Simulator.Now()
+        if now.IsZero():
+            return False
+        
+        return True
+
     def _is_app_running(self, uuid): 
         now = self.ns3.Simulator.Now()
         if now.IsZero():