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.",
63 use_dlm = Attribute("useDlmLoader",
64 "Use ns3::DlmLoaderFactory as library loader",
68 starttime = Attribute("StartTime",
69 "Time at which the application will start",
71 flags = Flags.Reserved | Flags.Construct)
73 stoptime = Attribute("StopTime",
74 "Time at which the application will stop",
76 flags = Flags.Reserved | Flags.Construct)
78 cls._register_attribute(binary)
79 cls._register_attribute(stack_size)
80 cls._register_attribute(arguments)
81 cls._register_attribute(environment)
82 cls._register_attribute(use_dlm)
83 cls._register_attribute(stoptime)
84 cls._register_attribute(starttime)
88 from nepi.resources.ns3.ns3node import NS3BaseNode
89 nodes = self.get_connected(NS3BaseNode.get_rtype())
92 msg = "DceApplication not connected to node"
94 raise RuntimeError, msg
99 def dce_manager_helper_uuid(self):
100 if not self._dce_manager_helper_uuid:
101 self._dce_manager_helper_uuid = self.simulation.create(
104 if self.get("useDlmLoader"):
105 self.simulation.invoke(
106 self._dce_manager_helper_uuid, "SetLoader",
107 "ns3::DlmLoaderFactory")
109 return self._dce_manager_helper_uuid
112 def dce_application_helper_uuid(self):
113 if not self._dce_application_helper_uuid:
114 self._dce_application_helper_uuid = self.simulation.create("DceApplicationHelper")
115 return self._dce_application_helper_uuid
117 def _instantiate_object(self):
120 def _connect_object(self):
122 if node.uuid not in self.connected:
123 self._connected.add(node.uuid)
125 # Preventing concurrent access to the DceApplicationHelper
126 # from different DceApplication RMs
127 with self.dce_application_lock:
128 self.simulation.invoke(
129 self.dce_application_helper_uuid,
132 self.simulation.invoke(
133 self.dce_application_helper_uuid,
136 self.simulation.invoke(
137 self.dce_application_helper_uuid,
138 "SetBinary", self.get("binary"))
140 self.simulation.invoke(
141 self.dce_application_helper_uuid,
142 "SetStackSize", self.get("stackSize"))
144 arguments = self.get("arguments")
146 for arg in map(str.strip, arguments.split(";")):
147 self.simulation.invoke(
148 self.dce_application_helper_uuid,
151 environment = self.get("environment")
153 for env in map(str.strip, environment.split(";")):
154 key, val = env.split("=")
155 self.simulation.invoke(
156 self.dce_application_helper_uuid,
157 "AddEnvironment", key, val)
159 apps_uuid = self.simulation.invoke(
160 self.dce_application_helper_uuid,
161 "InstallInNode", self.node.uuid)
164 container_uuid = self.simulation.create("NodeContainer")
165 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
166 apps_uuid = self.simulation.invoke(
167 self.dce_application_helper_uuid,
168 "Install", container_uuid)
171 self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
173 if self.has_changed("StartTime"):
174 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
176 if self.has_changed("StopTime"):
177 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
180 if self.state == ResourceState.STARTED:
181 # No need to do anything, simulation.Destroy() will stop every object
182 self.info("Stopping command '%s'" % command)
183 self.simulation.invoke(self.uuid, "Stop")
187 if self.simulation.state < ResourceState.STARTED:
188 self.debug("---- RESCHEDULING START ----" )
189 self.ec.schedule(reschedule_delay, self.start)
191 self._configure_traces()
192 super(NS3BaseApplication, self).do_start()
193 self._start_time = self.simulation.start_time
195 def _configure_traces(self):
196 # Waiting until dce application is actually started
198 for i in xrange(200):
199 is_running = self.simulation.invoke(self.uuid, "isAppRunning")
200 is_finished = self.simulation.invoke(SIMULATOR_UUID, "isFinished")
202 if is_running or is_finished:
208 msg = " Application did not start"
212 # Using lock to prevent concurrent access to the DceApplicationHelper
213 # from different DceApplication RMs
214 with self.dce_application_lock:
215 pid = self.simulation.invoke(self.dce_application_helper_uuid,
218 node_id = self.simulation.invoke(self.node.uuid, "GetId")
219 self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
220 self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
221 self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
222 self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)