2ed1363cbc7cc6dd00ec67d143c3b8e7d5c62021
[nepi.git] / src / nepi / resources / netns / netnsapplication.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
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
23
24 import shlex
25
26 @clsinit_copy
27 class NetNSApplication(NetNSBase):
28     _rtype = "netns::Application"
29
30     def __init__(self, ec, guid):
31         super(NetNSApplication, self).__init__(ec, guid)
32         self._traces = dict()
33
34     @classmethod
35     def _register_attributes(cls):
36         command = Attribute("command", "Command to execute", flags=Flags.Design)
37         cls._register_attribute(command)
38
39     @property
40     def emulation(self):
41         return self.node.emulation
42
43     @property
44     def node(self):
45         from nepi.resources.netns.netnsnode import NetNSNode
46         node = self.get_connected(NetNSNode.get_rtype())
47
48         if not node: 
49             msg = "Route not connected to Node!!"
50             self.error(msg)
51             raise RuntimeError, msg
52
53         return node[0]
54
55     @property
56     def _rms_to_wait(self):
57         return [self.node]
58
59     def do_start(self):
60         if self.emulation.state < ResourceState.STARTED:
61             self.debug("---- RESCHEDULING START ----" )
62             self.ec.schedule(self.reschedule_delay, self.start)
63         else:
64             self._configure_traces()
65
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, 
71                     stderr = stderr)
72
73             super(NetNSApplication, self).do_start()
74             self._start_time = self.emulation.start_time
75
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")
83
84     @property
85     def state(self):
86         if self._state == ResourceState.STARTED:
87             retcode = self.emulation.invoke(self.uuid, "poll")
88    
89             if retcode is not None:
90                 if retcode == 0:
91                     self.set_stopped()
92                 else:
93                     out = ""
94                     msg = " Failed to execute command '%s'" % self.get("command")
95                     err = self.trace("stderr")
96                     self.error(msg, out, err)
97                     self.do_fail()
98
99         return self._state
100