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 starttime = Attribute("StartTime",
64 "Time at which the application will start",
66 flags = Flags.Reserved | Flags.Construct)
68 stoptime = Attribute("StopTime",
69 "Time at which the application will stop",
71 flags = Flags.Reserved | Flags.Construct)
73 cls._register_attribute(binary)
74 cls._register_attribute(stack_size)
75 cls._register_attribute(arguments)
76 cls._register_attribute(environment)
77 cls._register_attribute(stoptime)
78 cls._register_attribute(starttime)
82 from nepi.resources.ns3.ns3node import NS3BaseNode
83 nodes = self.get_connected(NS3BaseNode.get_rtype())
86 msg = "DceApplication not connected to node"
88 raise RuntimeError, msg
93 def dce_manager_helper_uuid(self):
94 if not self._dce_manager_helper_uuid:
95 self._dce_manager_helper_uuid = self.simulation.create("DceManagerHelper")
96 return self._dce_manager_helper_uuid
99 def dce_application_helper_uuid(self):
100 if not self._dce_application_helper_uuid:
101 self._dce_application_helper_uuid = self.simulation.create("DceApplicationHelper")
102 return self._dce_application_helper_uuid
104 def _instantiate_object(self):
107 def _connect_object(self):
109 if node.uuid not in self.connected:
110 self._connected.add(node.uuid)
112 # Preventing concurrent access to the DceApplicationHelper
113 # from different DceApplication RMs
114 with self.dce_application_lock:
115 self.simulation.invoke(
116 self.dce_application_helper_uuid,
119 self.simulation.invoke(
120 self.dce_application_helper_uuid,
123 self.simulation.invoke(
124 self.dce_application_helper_uuid,
125 "SetBinary", self.get("binary"))
127 self.simulation.invoke(
128 self.dce_application_helper_uuid,
129 "SetStackSize", self.get("stackSize"))
131 arguments = self.get("arguments")
133 for arg in map(str.strip, arguments.split(";")):
134 self.simulation.invoke(
135 self.dce_application_helper_uuid,
138 environment = self.get("environment")
140 for env in map(str.strip, environment.split(";")):
141 key, val = env.split("=")
142 self.simulation.invoke(
143 self.dce_application_helper_uuid,
144 "AddEnvironment", key, val)
146 apps_uuid = self.simulation.invoke(
147 self.dce_application_helper_uuid,
148 "InstallInNode", self.node.uuid)
151 container_uuid = self.simulation.create("NodeContainer")
152 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
153 apps_uuid = self.simulation.invoke(
154 self.dce_application_helper_uuid,
155 "Install", container_uuid)
158 self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
160 if self.has_changed("StartTime"):
161 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
163 if self.has_changed("StopTime"):
164 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
167 if self.state == ResourceState.STARTED:
168 # No need to do anything, simulation.Destroy() will stop every object
169 self.info("Stopping command '%s'" % command)
170 self.simulation.invoke(self.uuid, "Stop")
174 if self.simulation.state < ResourceState.STARTED:
175 self.debug("---- RESCHEDULING START ----" )
176 self.ec.schedule(reschedule_delay, self.start)
178 self._configure_traces()
179 super(NS3BaseApplication, self).do_start()
180 self._start_time = self.simulation.start_time
182 def _configure_traces(self):
183 # Waiting until dce application is actually started
185 for i in xrange(200):
186 is_running = self.simulation.invoke(self.uuid, "isAppRunning")
187 is_finished = self.simulation.invoke(SIMULATOR_UUID, "isFinished")
189 if is_running or is_finished:
195 msg = " Application did not start"
199 # Using lock to prevent concurrent access to the DceApplicationHelper
200 # from different DceApplication RMs
201 with self.dce_application_lock:
202 pid = self.simulation.invoke(self.dce_application_helper_uuid,
205 node_id = self.simulation.invoke(self.node.uuid, "GetId")
206 self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
207 self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
208 self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
209 self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)