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.resources.netns.netnsbase import NetNSBase
22 from nepi.execution.resource import clsinit_copy, ResourceState
27 class NetNSApplication(NetNSBase):
28 _rtype = "netns::Application"
30 def __init__(self, ec, guid):
31 super(NetNSApplication, self).__init__(ec, guid)
35 def _register_attributes(cls):
36 command = Attribute("command", "Command to execute", flags=Flags.Design)
37 cls._register_attribute(command)
41 return self.node.emulation
45 from nepi.resources.netns.netnsnode import NetNSNode
46 node = self.get_connected(NetNSNode.get_rtype())
49 msg = "Route not connected to Node!!"
51 raise RuntimeError, msg
56 def _rms_to_wait(self):
60 if self.emulation.state < ResourceState.STARTED:
61 self.debug("---- RESCHEDULING START ----" )
62 self.ec.schedule(self.reschedule_delay, self.start)
64 self._configure_traces()
66 command = shlex.split(self.get("command"))
67 stdout = self._traces["stdout"]
68 stderr = self._traces["stderr"]
69 self._uuid = self.emulation.invoke(self.node.uuid,
70 "Popen", command, stdout = stdout,
73 super(NetNSApplication, self).do_start()
74 self._start_time = self.emulation.start_time
76 def _configure_traces(self):
77 stdout = "%s/%d.stdout" % (self.emulation.run_home, self.guid)
78 stderr = "%s/%d.stderr" % (self.emulation.run_home, self.guid)
79 self._trace_filename["stdout"] = stdout
80 self._trace_filename["stderr"] = stderr
81 self._traces["stdout"] = self.emulation.create("open", stdout, "w")
82 self._traces["stderr"] = self.emulation.create("open", stderr, "w")
86 if self._state == ResourceState.STARTED:
87 retcode = self.emulation.invoke(self.uuid, "poll")
89 if retcode is not None:
94 msg = " Failed to execute command '%s'" % self.get("command")
95 err = self.trace("stderr")
96 self.error(msg, out, err)