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
22 from nepi.resources.netns.netnsbase import NetNSBase
23 from nepi.execution.resource import clsinit_copy, ResourceState, \
29 class NetNSApplication(NetNSBase):
30 _rtype = "netns::Application"
32 def __init__(self, ec, guid):
33 super(NetNSApplication, self).__init__(ec, guid)
37 def _register_attributes(cls):
38 command = Attribute("command", "Command to execute", flags=Flags.Design)
39 cls._register_attribute(command)
43 return self.node.emulation
47 from nepi.resources.netns.netnsnode import NetNSNode
48 node = self.get_connected(NetNSNode.get_rtype())
51 msg = "Route not connected to Node!!"
53 raise RuntimeError, msg
58 def _rms_to_wait(self):
62 if self.simulation.state < ResourceState.STARTED:
63 self.debug("---- RESCHEDULING START ----" )
64 self.ec.schedule(reschedule_delay, self.start)
66 self._configure_traces()
68 command = shlex.split(self.get("command"))
69 stdout = self._traces["stdout"]
70 stderr = self._traces["stderr"]
71 self._uuid = self.emulation.invoke(self.node.uuid,
72 "Popen", command, stdout = stdout,
75 super(NetNSApplication, self).do_start()
76 self._start_time = self.emulation.start_time
78 def _configure_traces(self):
79 stdout = "%s/%d.stdout " % (self.emulation.run_home, self.pid)
80 stderr = "%s/%d.stderr " % (self.emulation.run_home, self.pid)
81 self._trace_filename["stdout"] = stdout
82 self._trace_filename["stderr"] = stderr
83 self._traces["stdout"] = self.emulation.create("open", stdout, "w")
84 self._traces["stderr"] = self.emulation.create("open", stderr, "w")
88 if self._state == ResourceState.STARTED:
89 retcode = self.emulation.invoke(self.uuid, "poll")
96 msg = " Failed to execute command '%s'" % self.get("command")
97 err = self.trace("stderr")
98 self.error(msg, out, err)