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"
35 # Lock used to synchronize usage of DceManagerHelper
36 _dce_manager_lock = threading.Lock()
37 # Lock used to synchronize usage of DceApplicationHelper
38 _dce_application_lock = threading.Lock()
40 _dce_manager_helper_uuid = None
41 _dce_application_helper_uuid = None
44 def _register_attributes(cls):
45 binary = Attribute("binary",
46 "Name of binary to execute",
49 stack_size = Attribute("stackSize",
55 arguments = Attribute("arguments",
56 "Semi-colon separated list of arguments for the application",
59 environment = Attribute("environment",
60 "Semi-colon separated list of 'key=value' pairs to set as "
61 "DCE environment variables.",
64 use_dlm = Attribute("useDlmLoader",
65 "Use ns3::DlmLoaderFactory as library loader",
69 starttime = Attribute("StartTime",
70 "Time at which the application will start",
72 flags = Flags.Reserved | Flags.Construct)
74 stoptime = Attribute("StopTime",
75 "Time at which the application will stop",
77 flags = Flags.Reserved | Flags.Construct)
79 cls._register_attribute(binary)
80 cls._register_attribute(stack_size)
81 cls._register_attribute(arguments)
82 cls._register_attribute(environment)
83 cls._register_attribute(use_dlm)
84 cls._register_attribute(stoptime)
85 cls._register_attribute(starttime)
87 def __init__(self, ec, guid):
88 super(NS3BaseDceApplication, self).__init__(ec, guid)
96 def dce_manager_helper_uuid(self):
97 if not NS3BaseDceApplication._dce_manager_helper_uuid:
98 NS3BaseDceApplication._dce_manager_helper_uuid = \
99 self.simulation.create("DceManagerHelper")
101 if self.get("useDlmLoader"):
102 self.simulation.invoke(
103 NS3BaseDceApplication._dce_manager_helper_uuid,
105 "ns3::DlmLoaderFactory")
107 return NS3BaseDceApplication._dce_manager_helper_uuid
110 def dce_application_helper_uuid(self):
111 if not NS3BaseDceApplication._dce_application_helper_uuid:
112 NS3BaseDceApplication._dce_application_helper_uuid = \
113 self.simulation.create("DceApplicationHelper")
115 return NS3BaseDceApplication._dce_application_helper_uuid
118 def dce_manager_lock(self):
119 return NS3BaseDceApplication._dce_manager_lock
122 def dce_application_lock(self):
123 return NS3BaseDceApplication._dce_application_lock
125 def _instantiate_object(self):
128 def _connect_object(self):
130 if node.uuid not in self.connected:
131 self._connected.add(node.uuid)
133 # Preventing concurrent access to the DceApplicationHelper
134 # from different DceApplication RMs
135 with self.dce_application_lock:
136 self.simulation.invoke(
137 self.dce_application_helper_uuid,
140 self.simulation.invoke(
141 self.dce_application_helper_uuid,
144 self.simulation.invoke(
145 self.dce_application_helper_uuid,
146 "SetBinary", self.get("binary"))
148 self.simulation.invoke(
149 self.dce_application_helper_uuid,
150 "SetStackSize", self.get("stackSize"))
152 arguments = self.get("arguments")
154 for arg in map(str.strip, arguments.split(";")):
155 self.simulation.invoke(
156 self.dce_application_helper_uuid,
159 environment = self.get("environment")
161 for env in map(str.strip, environment.split(";")):
162 key, val = env.split("=")
163 self.simulation.invoke(
164 self.dce_application_helper_uuid,
165 "AddEnvironment", key, val)
167 apps_uuid = self.simulation.invoke(
168 self.dce_application_helper_uuid,
169 "InstallInNode", self.node.uuid)
173 container_uuid = self.simulation.create("NodeContainer")
174 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
175 apps_uuid = self.simulation.invoke(
176 self.dce_application_helper_uuid,
177 "Install", container_uuid)
180 self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
182 if self.has_changed("StartTime"):
183 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
185 if self.has_changed("StopTime"):
186 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
189 if self.state == ResourceState.STARTED:
190 # No need to do anything, simulation.Destroy() will stop every object
191 self.info("Stopping command '%s'" % command)
192 self.simulation.invoke(self.uuid, "Stop")
196 if self.simulation.state < ResourceState.STARTED:
197 self.debug("---- RESCHEDULING START ----" )
198 self.ec.schedule(self.reschedule_delay, self.start)
200 is_app_running = self.simulation.invoke(self.uuid, "isAppRunning")
202 if is_app_running or self.simulation.state > Resource.STARTED:
203 super(NS3BaseApplication, self).do_start()
204 self._start_time = self.simulation.start_time
206 # Reschedule until dce application is actually started
207 self.debug("---- RESCHEDULING START ----" )
208 self.ec.schedule(self.reschedule_delay, self.start)
210 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
211 self._configure_traces()
212 return super(NS3BaseDceApplication, self).trace(name, attr = attr,
213 block = block, offset = offset)
215 def _configure_traces(self):
216 if self.pid is not None:
219 # Using lock to prevent concurrent access to the DceApplicationHelper
220 # from different DceApplication RMs
221 with self.dce_application_lock:
222 self._pid = self.simulation.invoke(self.dce_application_helper_uuid,
225 node_id = self.node.node_id
226 self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, self.pid)
227 self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, self.pid)
228 self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, self.pid)
229 self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, self.pid)