# Author: Alina Quereilhac <alina.quereilhac@inria.fr>
from nepi.execution.attribute import Attribute, Flags, Types
-from nepi.execution.resource import clsinit_copy, ResourceState, reschedule_delay
+from nepi.execution.resource import clsinit_copy, ResourceState
from nepi.resources.ns3.ns3application import NS3BaseApplication
+from nepi.execution.trace import TraceAttr
-import os
+from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID
+import os
+import time
+import threading
+
@clsinit_copy
class NS3BaseDceApplication(NS3BaseApplication):
_rtype = "abstract::ns3::DceApplication"
+ # Lock used to synchronize usage of DceManagerHelper
+ _dce_manager_lock = threading.Lock()
+ # Lock used to synchronize usage of DceApplicationHelper
+ _dce_application_lock = threading.Lock()
+
+ _dce_manager_helper_uuid = None
+ _dce_application_helper_uuid = None
+
@classmethod
def _register_attributes(cls):
binary = Attribute("binary",
"DCE environment variables.",
flags = Flags.Design)
+ use_dlm = Attribute("useDlmLoader",
+ "Use ns3::DlmLoaderFactory as library loader",
+ type = Types.Bool,
+ flags = Flags.Design)
+
starttime = Attribute("StartTime",
"Time at which the application will start",
default = "+0.0ns",
cls._register_attribute(stack_size)
cls._register_attribute(arguments)
cls._register_attribute(environment)
+ cls._register_attribute(use_dlm)
cls._register_attribute(stoptime)
cls._register_attribute(starttime)
+ def __init__(self, ec, guid):
+ super(NS3BaseDceApplication, self).__init__(ec, guid)
+ self._pid = None
+
@property
- def node(self):
- from nepi.resources.ns3.ns3node import NS3BaseNode
- nodes = self.get_connected(NS3BaseNode.get_rtype())
+ def pid(self):
+ return self._pid
- if not nodes:
- msg = "DceApplication not connected to node"
- self.error(msg)
- raise RuntimeError, msg
+ @property
+ def dce_manager_helper_uuid(self):
+ if not NS3BaseDceApplication._dce_manager_helper_uuid:
+ NS3BaseDceApplication._dce_manager_helper_uuid = \
+ self.simulation.create("DceManagerHelper")
+
+ if self.get("useDlmLoader"):
+ self.simulation.invoke(
+ NS3BaseDceApplication._dce_manager_helper_uuid,
+ "SetLoader",
+ "ns3::DlmLoaderFactory")
- if nodes[0].get("enableDCE") == False:
- raise RuntimeError("DceApplication not connected to DCE enabled node")
+ return NS3BaseDceApplication._dce_manager_helper_uuid
+
+ @property
+ def dce_application_helper_uuid(self):
+ if not NS3BaseDceApplication._dce_application_helper_uuid:
+ NS3BaseDceApplication._dce_application_helper_uuid = \
+ self.simulation.create("DceApplicationHelper")
+
+ return NS3BaseDceApplication._dce_application_helper_uuid
+
+ @property
+ def dce_manager_lock(self):
+ return NS3BaseDceApplication._dce_manager_lock
+
+ @property
+ def dce_application_lock(self):
+ return NS3BaseDceApplication._dce_application_lock
- return nodes[0]
-
def _instantiate_object(self):
pass
# Preventing concurrent access to the DceApplicationHelper
# from different DceApplication RMs
- with self.simulation.dce_application_lock:
+ with self.dce_application_lock:
self.simulation.invoke(
- self.simulation.dce_application_helper_uuid,
+ self.dce_application_helper_uuid,
"ResetArguments")
self.simulation.invoke(
- self.simulation.dce_application_helper_uuid,
+ self.dce_application_helper_uuid,
"ResetEnvironment")
self.simulation.invoke(
- self.simulation.dce_application_helper_uuid,
+ self.dce_application_helper_uuid,
"SetBinary", self.get("binary"))
self.simulation.invoke(
- self.simulation.dce_application_helper_uuid,
+ self.dce_application_helper_uuid,
"SetStackSize", self.get("stackSize"))
arguments = self.get("arguments")
if arguments:
for arg in map(str.strip, arguments.split(";")):
self.simulation.invoke(
- self.simulation.dce_application_helper_uuid,
+ self.dce_application_helper_uuid,
"AddArgument", arg)
environment = self.get("environment")
for env in map(str.strip, environment.split(";")):
key, val = env.split("=")
self.simulation.invoke(
- self.simulation.dce_application_helper_uuid,
+ self.dce_application_helper_uuid,
"AddEnvironment", key, val)
apps_uuid = self.simulation.invoke(
- self.simulation.dce_application_helper_uuid,
+ self.dce_application_helper_uuid,
"InstallInNode", self.node.uuid)
+
+ """
+ container_uuid = self.simulation.create("NodeContainer")
+ self.simulation.invoke(container_uuid, "Add", self.node.uuid)
+ apps_uuid = self.simulation.invoke(
+ self.dce_application_helper_uuid,
+ "Install", container_uuid)
+ """
+
self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
if self.has_changed("StartTime"):
def do_start(self):
if self.simulation.state < ResourceState.STARTED:
self.debug("---- RESCHEDULING START ----" )
- self.ec.schedule(reschedule_delay, self.start)
+ self.ec.schedule(self.reschedule_delay, self.start)
else:
- self._configure_traces()
- super(NS3BaseApplication, self).do_start()
- self._start_time = self.simulation.start_time
+ is_app_running = self.simulation.invoke(self.uuid, "isAppRunning")
+ is_simu_finished = self.simulation.invoke(SIMULATOR_UUID, "isFinished")
+
+ if is_app_running or is_simu_finished:
+ super(NS3BaseApplication, self).do_start()
+ self._start_time = self.simulation.start_time
+ else:
+ # Reschedule until dce application is actually started
+ self.debug("---- RESCHEDULING START ----" )
+ self.ec.schedule(self.reschedule_delay, self.start)
+
+ def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
+ self._configure_traces()
+ return super(NS3BaseDceApplication, self).trace(name, attr = attr,
+ block = block, offset = offset)
def _configure_traces(self):
- # Preventing concurrent access to the DceApplicationHelper
+ if self.pid is not None:
+ return
+
+ # Using lock to prevent 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)
+ with self.dce_application_lock:
+ self._pid = self.simulation.invoke(self.dce_application_helper_uuid,
+ "GetPid", self.uuid)
+
+ node_id = self.node.node_id
+ self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, self.pid)
+ self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, self.pid)
+ self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, self.pid)
+ self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, self.pid)