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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.ns3.ns3application import NS3BaseApplication
22 from nepi.execution.trace import TraceAttr
24 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID
31 class NS3BaseDceApplication(NS3BaseApplication):
32 _rtype = "abstract::ns3::DceApplication"
35 def _register_attributes(cls):
36 binary = Attribute("binary",
37 "Name of binary to execute",
40 stack_size = Attribute("stackSize",
46 arguments = Attribute("arguments",
47 "Semi-colon separated list of arguments for the application",
50 environment = Attribute("environment",
51 "Semi-colon separated list of 'key=value' pairs to set as "
52 "DCE environment variables.",
55 use_dlm = Attribute("useDlmLoader",
56 "Use ns3::DlmLoaderFactory as library loader",
60 starttime = Attribute("StartTime",
61 "Time at which the application will start",
63 flags = Flags.Reserved | Flags.Construct)
65 stoptime = Attribute("StopTime",
66 "Time at which the application will stop",
68 flags = Flags.Reserved | Flags.Construct)
70 cls._register_attribute(binary)
71 cls._register_attribute(stack_size)
72 cls._register_attribute(arguments)
73 cls._register_attribute(environment)
74 cls._register_attribute(use_dlm)
75 cls._register_attribute(stoptime)
76 cls._register_attribute(starttime)
78 def __init__(self, ec, guid):
79 super(NS3BaseDceApplication, self).__init__(ec, guid)
86 def _instantiate_object(self):
89 def _connect_object(self):
91 if node.uuid not in self.connected:
92 self._connected.add(node.uuid)
94 # Preventing concurrent access to the DceApplicationHelper
95 # from different DceApplication RMs
96 dce_helper = self.simulation.dce_helper
98 with dce_helper.dce_application_lock:
99 dce_app_uuid = dce_helper.dce_application_uuid
101 self.simulation.invoke(dce_app_uuid, "ResetArguments")
103 self.simulation.invoke(dce_app_uuid, "ResetEnvironment")
105 self.simulation.invoke(dce_app_uuid,
106 "SetBinary", self.get("binary"))
108 self.simulation.invoke(dce_app_uuid,
109 "SetStackSize", self.get("stackSize"))
111 arguments = self.get("arguments")
113 for arg in map(str.strip, arguments.split(";")):
114 self.simulation.invoke(dce_app_uuid,
117 environment = self.get("environment")
119 for env in map(str.strip, environment.split(";")):
120 key, val = env.split("=")
121 self.simulation.invoke(dce_app_uuid,
122 "AddEnvironment", key, val)
124 apps_uuid = self.simulation.invoke(dce_app_uuid,
125 "InstallInNode", self.node.uuid)
127 self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
129 if self.has_changed("StartTime"):
130 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
132 if self.has_changed("StopTime"):
133 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
136 if self.state == ResourceState.STARTED:
137 # No need to do anything, simulation.Destroy() will stop every object
138 self.info("Stopping command '%s'" % command)
139 self.simulation.invoke(self.uuid, "Stop")
143 if self.simulation.state < ResourceState.STARTED:
144 self.debug("---- RESCHEDULING START ----" )
145 self.ec.schedule(self.reschedule_delay, self.start)
147 is_app_started = self.simulation.invoke(self.uuid, "isAppStarted")
149 if is_app_started or self.simulation.state > ResourceState.STARTED:
150 super(NS3BaseApplication, self).do_start()
151 self._start_time = self.simulation.start_time
153 # Reschedule until dce application is actually started
154 self.debug("---- RESCHEDULING START ----" )
155 self.ec.schedule(self.reschedule_delay, self.start)
157 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
158 self._configure_traces()
159 return super(NS3BaseDceApplication, self).trace(name, attr = attr,
160 block = block, offset = offset)
162 def _configure_traces(self):
163 if self.pid is not None:
166 # Using lock to prevent concurrent access to the DceApplicationHelper
167 # from different DceApplication RMs
168 dce_helper = self.simulation.dce_helper
170 with dce_helper.dce_application_lock:
171 dce_app_uuid = dce_helper.dce_application_uuid
173 self._pid = self.simulation.invoke(dce_app_uuid,
176 node_id = self.node.node_id
177 self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, self.pid)
178 self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, self.pid)
179 self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, self.pid)
180 self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, self.pid)