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
28 class NS3BaseDceApplication(NS3BaseApplication):
29 _rtype = "abstract::ns3::DceApplication"
31 # Lock used to synchronize usage of DceManagerHelper
32 dce_manager_lock = threading.Lock()
33 # Lock used to synchronize usage of DceApplicationHelper
34 dce_application_lock = threading.Lock()
36 _dce_manager_helper_uuid = None
37 _dce_application_helper_uuid = None
40 def _register_attributes(cls):
41 binary = Attribute("binary",
42 "Name of binary to execute",
45 stack_size = Attribute("stackSize",
51 arguments = Attribute("arguments",
52 "Semi-colon separated list of arguments for the application",
55 environment = Attribute("environment",
56 "Semi-colon separated list of 'key=value' pairs to set as "
57 "DCE environment variables.",
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(stoptime)
75 cls._register_attribute(starttime)
79 from nepi.resources.ns3.ns3node import NS3BaseNode
80 nodes = self.get_connected(NS3BaseNode.get_rtype())
83 msg = "DceApplication not connected to node"
85 raise RuntimeError, msg
90 def dce_manager_helper_uuid(self):
91 if not self._dce_manager_helper_uuid:
92 self._dce_manager_helper_uuid = self.simulation.create("DceManagerHelper")
93 return self._dce_manager_helper_uuid
96 def dce_application_helper_uuid(self):
97 if not self._dce_application_helper_uuid:
98 self._dce_application_helper_uuid = self.simulation.create("DceApplicationHelper")
99 return self._dce_application_helper_uuid
101 def _instantiate_object(self):
104 def _connect_object(self):
106 if node.uuid not in self.connected:
107 self._connected.add(node.uuid)
109 # Preventing concurrent access to the DceApplicationHelper
110 # from different DceApplication RMs
111 with self.dce_application_lock:
112 self.simulation.invoke(
113 self.dce_application_helper_uuid,
116 self.simulation.invoke(
117 self.dce_application_helper_uuid,
120 self.simulation.invoke(
121 self.dce_application_helper_uuid,
122 "SetBinary", self.get("binary"))
124 self.simulation.invoke(
125 self.dce_application_helper_uuid,
126 "SetStackSize", self.get("stackSize"))
128 arguments = self.get("arguments")
130 for arg in map(str.strip, arguments.split(";")):
131 self.simulation.invoke(
132 self.dce_application_helper_uuid,
135 environment = self.get("environment")
137 for env in map(str.strip, environment.split(";")):
138 key, val = env.split("=")
139 self.simulation.invoke(
140 self.dce_application_helper_uuid,
141 "AddEnvironment", key, val)
143 apps_uuid = self.simulation.invoke(
144 self.dce_application_helper_uuid,
145 "InstallInNode", self.node.uuid)
148 container_uuid = self.simulation.create("NodeContainer")
149 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
150 apps_uuid = self.simulation.invoke(
151 self.dce_application_helper_uuid,
152 "Install", container_uuid)
155 self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
157 if self.has_changed("StartTime"):
158 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
160 if self.has_changed("StopTime"):
161 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
164 if self.state == ResourceState.STARTED:
165 # No need to do anything, simulation.Destroy() will stop every object
166 self.info("Stopping command '%s'" % command)
167 self.simulation.invoke(self.uuid, "Stop")
171 if self.simulation.state < ResourceState.STARTED:
172 self.debug("---- RESCHEDULING START ----" )
173 self.ec.schedule(reschedule_delay, self.start)
175 self._configure_traces()
176 super(NS3BaseApplication, self).do_start()
177 self._start_time = self.simulation.start_time
179 def _configure_traces(self):
180 # Preventing concurrent access to the DceApplicationHelper
181 # from different DceApplication RMs
182 with self.dce_application_lock:
183 pid = self.simulation.invoke(self.dce_application_helper_uuid,
186 node_id = self.simulation.invoke(self.node.uuid, "GetId")
187 self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
188 self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
189 self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
190 self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)