2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState
22 from nepi.resources.ns3.ns3application import NS3BaseApplication
23 from nepi.execution.trace import TraceAttr
25 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID
32 class NS3BaseDceApplication(NS3BaseApplication):
33 _rtype = "abstract::ns3::DceApplication"
36 def _register_attributes(cls):
37 binary = Attribute("binary",
38 "Name of binary to execute",
41 stack_size = Attribute("stackSize",
47 arguments = Attribute("arguments",
48 "Semi-colon separated list of arguments for the application",
51 environment = Attribute("environment",
52 "Semi-colon separated list of 'key=value' pairs to set as "
53 "DCE environment variables.",
56 use_dlm = Attribute("useDlmLoader",
57 "Use ns3::DlmLoaderFactory as library loader",
61 starttime = Attribute("StartTime",
62 "Time at which the application will start",
64 flags = Flags.Reserved | Flags.Construct)
66 stoptime = Attribute("StopTime",
67 "Time at which the application will stop",
69 flags = Flags.Reserved | Flags.Construct)
71 cls._register_attribute(binary)
72 cls._register_attribute(stack_size)
73 cls._register_attribute(arguments)
74 cls._register_attribute(environment)
75 cls._register_attribute(use_dlm)
76 cls._register_attribute(stoptime)
77 cls._register_attribute(starttime)
79 def __init__(self, ec, guid):
80 super(NS3BaseDceApplication, self).__init__(ec, guid)
87 def _instantiate_object(self):
90 def _connect_object(self):
92 if node.uuid not in self.connected:
93 self._connected.add(node.uuid)
95 # Preventing concurrent access to the DceApplicationHelper
96 # from different DceApplication RMs
97 dce_helper = self.simulation.dce_helper
99 with dce_helper.dce_application_lock:
100 dce_app_uuid = dce_helper.dce_application_uuid
102 self.simulation.invoke(dce_app_uuid, "ResetArguments")
104 self.simulation.invoke(dce_app_uuid, "ResetEnvironment")
106 self.simulation.invoke(dce_app_uuid,
107 "SetBinary", self.get("binary"))
109 self.simulation.invoke(dce_app_uuid,
110 "SetStackSize", self.get("stackSize"))
112 arguments = self.get("arguments")
114 for arg in map(str.strip, arguments.split(";")):
115 self.simulation.invoke(dce_app_uuid,
118 environment = self.get("environment")
120 for env in map(str.strip, environment.split(";")):
121 key, val = env.split("=")
122 self.simulation.invoke(dce_app_uuid,
123 "AddEnvironment", key, val)
125 apps_uuid = self.simulation.invoke(dce_app_uuid,
126 "InstallInNode", self.node.uuid)
128 self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
130 if self.has_changed("StartTime"):
131 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
133 if self.has_changed("StopTime"):
134 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
137 if self.state == ResourceState.STARTED:
138 # No need to do anything, simulation.Destroy() will stop every object
139 self.info("Stopping command '%s'" % command)
140 self.simulation.invoke(self.uuid, "Stop")
144 if self.simulation.state < ResourceState.STARTED:
145 self.debug("---- RESCHEDULING START ----" )
146 self.ec.schedule(self.reschedule_delay, self.start)
148 is_app_started = self.simulation.invoke(self.uuid, "isAppStarted")
150 if is_app_started or self.simulation.state > ResourceState.STARTED:
151 super(NS3BaseApplication, self).do_start()
152 self._start_time = self.simulation.start_time
154 # Reschedule until dce application is actually started
155 self.debug("---- RESCHEDULING START ----" )
156 self.ec.schedule(self.reschedule_delay, self.start)
158 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
159 self._configure_traces()
160 return super(NS3BaseDceApplication, self).trace(name, attr = attr,
161 block = block, offset = offset)
163 def _configure_traces(self):
164 if self.pid is not None:
167 # Using lock to prevent concurrent access to the DceApplicationHelper
168 # from different DceApplication RMs
169 dce_helper = self.simulation.dce_helper
171 with dce_helper.dce_application_lock:
172 dce_app_uuid = dce_helper.dce_application_uuid
174 self._pid = self.simulation.invoke(dce_app_uuid,
177 node_id = self.node.node_id
178 self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, self.pid)
179 self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, self.pid)
180 self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, self.pid)
181 self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, self.pid)