ns-3 CCN tests
[nepi.git] / src / nepi / resources / ns3 / ns3dceapplication.py
index 5d34e84..acaf0e6 100644 (file)
@@ -21,6 +21,8 @@ from nepi.execution.attribute import Attribute, Flags, Types
 from nepi.execution.resource import clsinit_copy, ResourceState, reschedule_delay
 from nepi.resources.ns3.ns3application import NS3BaseApplication
 
+import os
+
 @clsinit_copy
 class NS3BaseDceApplication(NS3BaseApplication):
     _rtype = "abstract::ns3::DceApplication"
@@ -41,9 +43,15 @@ class NS3BaseDceApplication(NS3BaseApplication):
                 "Semi-colon separated list of arguments for the application",
                 flags = Flags.Design)
 
+        environment = Attribute("environment", 
+                "Semi-colon separated list of 'key=value' pairs to set as "
+                "DCE environment variables.",
+                flags = Flags.Design)
+
         cls._register_attribute(binary)
         cls._register_attribute(stack_size)
         cls._register_attribute(arguments)
+        cls._register_attribute(environment)
 
     @property
     def node(self):
@@ -59,31 +67,77 @@ class NS3BaseDceApplication(NS3BaseApplication):
             raise RuntimeError("DceApplication not connected to DCE enabled node")
 
         return nodes[0]
+    
+    def _instantiate_object(self):
+        pass
 
     def _connect_object(self):
         node = self.node
         if node.uuid not in self.connected:
             self._connected.add(node.uuid)
-            self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
-                    "ResetArguments") 
-
-            self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
-                    "SetBinary", self.get("binary")) 
-
-            self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
-                    "SetStackSize", self.get("stackSize")) 
 
-            arguments = self.get("arguments") or ""
-            for arg in map(str.strip, arguments.split(";")):
-                self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
-                    "AddArgument", arg) 
-
-            apps_uuid = self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
-                    "InstallInNode", self.node.uuid)
-
-            #start_time = self.get("StartTime") 
-            #time_uuid = self.simulation.create("Time", start_time)
-            #self.simulation.invoke(apps_uuid, "Start", time_uuid) 
+            # Preventing concurrent access to the DceApplicationHelper
+            # from different DceApplication RMs
+            with self.simulation.dce_application_lock:
+                self.simulation.invoke(
+                        self.simulation.dce_application_helper_uuid, 
+                        "ResetArguments") 
+
+                self.simulation.invoke(
+                        self.simulation.dce_application_helper_uuid, 
+                        "ResetEnvironment") 
+
+                self.simulation.invoke(
+                        self.simulation.dce_application_helper_uuid, 
+                        "SetBinary", self.get("binary")) 
+
+                self.simulation.invoke(
+                        self.simulation.dce_application_helper_uuid, 
+                        "SetStackSize", self.get("stackSize")) 
+
+                arguments = self.get("arguments") or ""
+                for arg in map(str.strip, arguments.split(";")):
+                    self.simulation.invoke(
+                            self.simulation.dce_application_helper_uuid, 
+                        "AddArgument", arg)
+
+                environment = self.get("environment") or ""
+                for env in map(str.strip, environment.split(";")):
+                    key, val = env.split("=")
+                    self.simulation.invoke(
+                            self.simulation.dce_application_helper_uuid, 
+                        "AddEnvironment", key, val)
+
+                if self.has_attribute("files"):
+                    files = self.get("files") or ""
+                    for files in map(str.strip, files.split(";")):
+                        remotepath, dcepath = env.split("=")
+                        localpath = "${SHARE}/" + os.path.basename(remotepath)
+                        self.simulation.invoke(
+                                self.simulation.dce_application_helper_uuid, 
+                            "AddFile", localpath, dcepath)
+
+                if self.has_attribute("stdinFile"):
+                    stdinfile = self.get("stdinFile")
+                    if stdinfile:
+                        if stdinfile != "":
+                            stdinfile = "${SHARE}/" + os.path.basename(stdinfile)
+        
+                        self.simulation.invoke(
+                                self.simulation.dce_application_helper_uuid, 
+                                "SetStdinFile", stdinfile)
+
+                apps_uuid = self.simulation.invoke(
+                        self.simulation.dce_application_helper_uuid, 
+                        "InstallInNode", self.node.uuid)
+
+            self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
+
+            if self.has_changed("StartTime"):
+                self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
+
+            if self.has_changed("StopTime"):
+                self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
 
     def do_stop(self):
         if self.state == ResourceState.STARTED:
@@ -97,6 +151,20 @@ class NS3BaseDceApplication(NS3BaseApplication):
             self.debug("---- RESCHEDULING START ----" )
             self.ec.schedule(reschedule_delay, self.start)
         else:
+            self._configure_traces()
             super(NS3BaseApplication, self).do_start()
             self._start_time = self.simulation.start_time
 
+    def _configure_traces(self):
+        # Preventing concurrent access to the DceApplicationHelper
+        # from different DceApplication RMs
+        with self.simulation.dce_application_lock:
+            pid = self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
+                    "GetPid", self._uuid)
+        node_id = self.simulation.invoke(self.node.uuid, "GetId")
+        self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
+        self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
+        self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
+        self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)
+
+