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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.resources.netns.netnsbase import NetNSBase
21 from nepi.execution.resource import clsinit_copy, ResourceState
26 class NetNSApplication(NetNSBase):
27 _rtype = "netns::Application"
29 def __init__(self, ec, guid):
30 super(NetNSApplication, self).__init__(ec, guid)
34 def _register_attributes(cls):
35 command = Attribute("command", "Command to execute", flags=Flags.Design)
36 cls._register_attribute(command)
40 return self.node.emulation
44 from nepi.resources.netns.netnsnode import NetNSNode
45 node = self.get_connected(NetNSNode.get_rtype())
48 msg = "Route not connected to Node!!"
50 raise RuntimeError(msg)
55 def _rms_to_wait(self):
59 if self.emulation.state < ResourceState.STARTED:
60 self.debug("---- RESCHEDULING START ----" )
61 self.ec.schedule(self.reschedule_delay, self.start)
63 self._configure_traces()
65 command = shlex.split(self.get("command"))
66 stdout = self._traces["stdout"]
67 stderr = self._traces["stderr"]
68 self._uuid = self.emulation.invoke(self.node.uuid,
69 "Popen", command, stdout = stdout,
72 super(NetNSApplication, self).do_start()
73 self._start_time = self.emulation.start_time
75 def _configure_traces(self):
76 stdout = "%s/%d.stdout" % (self.emulation.run_home, self.guid)
77 stderr = "%s/%d.stderr" % (self.emulation.run_home, self.guid)
78 self._trace_filename["stdout"] = stdout
79 self._trace_filename["stderr"] = stderr
80 self._traces["stdout"] = self.emulation.create("open", stdout, "w")
81 self._traces["stderr"] = self.emulation.create("open", stderr, "w")
85 if self._state == ResourceState.STARTED:
86 retcode = self.emulation.invoke(self.uuid, "poll")
88 if retcode is not None:
93 msg = " Failed to execute command '%s'" % self.get("command")
94 err = self.trace("stderr")
95 self.error(msg, out, err)