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, reschedule_delay
22 from nepi.resources.ns3.ns3application import NS3BaseApplication
24 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID
31 class NS3BaseDceApplication(NS3BaseApplication):
32 _rtype = "abstract::ns3::DceApplication"
34 # Lock used to synchronize usage of DceManagerHelper
35 dce_manager_lock = threading.Lock()
36 # Lock used to synchronize usage of DceApplicationHelper
37 dce_application_lock = threading.Lock()
39 _dce_manager_helper_uuid = None
40 _dce_application_helper_uuid = None
43 def _register_attributes(cls):
44 binary = Attribute("binary",
45 "Name of binary to execute",
48 stack_size = Attribute("stackSize",
54 arguments = Attribute("arguments",
55 "Semi-colon separated list of arguments for the application",
58 environment = Attribute("environment",
59 "Semi-colon separated list of 'key=value' pairs to set as "
60 "DCE environment variables.",
64 use_dlm = Attribute("useDlmLoader",
65 "Use ns3::DlmLoaderFactory as library loader",
70 starttime = Attribute("StartTime",
71 "Time at which the application will start",
73 flags = Flags.Reserved | Flags.Construct)
75 stoptime = Attribute("StopTime",
76 "Time at which the application will stop",
78 flags = Flags.Reserved | Flags.Construct)
80 cls._register_attribute(binary)
81 cls._register_attribute(stack_size)
82 cls._register_attribute(arguments)
83 cls._register_attribute(environment)
85 cls._register_attribute(use_dlm)
87 cls._register_attribute(stoptime)
88 cls._register_attribute(starttime)
92 from nepi.resources.ns3.ns3node import NS3BaseNode
93 nodes = self.get_connected(NS3BaseNode.get_rtype())
96 msg = "DceApplication not connected to node"
98 raise RuntimeError, msg
103 def dce_manager_helper_uuid(self):
104 if not self._dce_manager_helper_uuid:
105 self._dce_manager_helper_uuid = self.simulation.create(
108 if self.get("useDlmLoader"):
109 self.simulation.invoke(
110 self._dce_manager_helper_uuid, "SetLoader",
111 "ns3::DlmLoaderFactory")
113 return self._dce_manager_helper_uuid
116 def dce_application_helper_uuid(self):
117 if not self._dce_application_helper_uuid:
118 self._dce_application_helper_uuid = self.simulation.create("DceApplicationHelper")
119 return self._dce_application_helper_uuid
121 def _instantiate_object(self):
124 def _connect_object(self):
126 if node.uuid not in self.connected:
127 self._connected.add(node.uuid)
129 # Preventing concurrent access to the DceApplicationHelper
130 # from different DceApplication RMs
131 with self.dce_application_lock:
132 self.simulation.invoke(
133 self.dce_application_helper_uuid,
136 self.simulation.invoke(
137 self.dce_application_helper_uuid,
140 self.simulation.invoke(
141 self.dce_application_helper_uuid,
142 "SetBinary", self.get("binary"))
144 self.simulation.invoke(
145 self.dce_application_helper_uuid,
146 "SetStackSize", self.get("stackSize"))
148 arguments = self.get("arguments")
150 for arg in map(str.strip, arguments.split(";")):
151 self.simulation.invoke(
152 self.dce_application_helper_uuid,
155 environment = self.get("environment")
157 for env in map(str.strip, environment.split(";")):
158 key, val = env.split("=")
159 self.simulation.invoke(
160 self.dce_application_helper_uuid,
161 "AddEnvironment", key, val)
163 apps_uuid = self.simulation.invoke(
164 self.dce_application_helper_uuid,
165 "InstallInNode", self.node.uuid)
168 container_uuid = self.simulation.create("NodeContainer")
169 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
170 apps_uuid = self.simulation.invoke(
171 self.dce_application_helper_uuid,
172 "Install", container_uuid)
175 self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
177 if self.has_changed("StartTime"):
178 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
180 if self.has_changed("StopTime"):
181 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
184 if self.state == ResourceState.STARTED:
185 # No need to do anything, simulation.Destroy() will stop every object
186 self.info("Stopping command '%s'" % command)
187 self.simulation.invoke(self.uuid, "Stop")
191 if self.simulation.state < ResourceState.STARTED:
192 self.debug("---- RESCHEDULING START ----" )
193 self.ec.schedule(reschedule_delay, self.start)
195 self._configure_traces()
196 super(NS3BaseApplication, self).do_start()
197 self._start_time = self.simulation.start_time
199 def _configure_traces(self):
200 # Waiting until dce application is actually started
202 for i in xrange(200):
203 is_running = self.simulation.invoke(self.uuid, "isAppRunning")
204 is_finished = self.simulation.invoke(SIMULATOR_UUID, "isFinished")
206 if is_running or is_finished:
212 msg = " Application did not start"
216 # Using lock to prevent concurrent access to the DceApplicationHelper
217 # from different DceApplication RMs
218 with self.dce_application_lock:
219 pid = self.simulation.invoke(self.dce_application_helper_uuid,
222 node_id = self.simulation.invoke(self.node.uuid, "GetId")
223 self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
224 self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
225 self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
226 self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)