b7904166a1193ca43674699294e8ad9cbc28d3b0
[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.execution.resource import clsinit_copy
22 from nepi.resources.netns.netnsbase import NetNSBase
23 from nepi.execution.resource import clsinit_copy, ResourceState, \
24         reschedule_delay
25
26 import shlex
27
28 @clsinit_copy
29 class NetNSApplication(NetNSBase):
30     _rtype = "netns::Application"
31
32     def __init__(self, ec, guid):
33         super(NetNSApplication, self).__init__(ec, guid)
34         self._traces = dict()
35
36     @classmethod
37     def _register_attributes(cls):
38         command = Attribute("command", "Command to execute", flags=Flags.Design)
39         cls._register_attribute(command)
40
41     @property
42     def emulation(self):
43         return self.node.emulation
44
45     @property
46     def node(self):
47         from nepi.resources.netns.netnsnode import NetNSNode
48         node = self.get_connected(NetNSNode.get_rtype())
49
50         if not node: 
51             msg = "Route not connected to Node!!"
52             self.error(msg)
53             raise RuntimeError, msg
54
55         return node[0]
56
57     @property
58     def _rms_to_wait(self):
59         return [self.node]
60
61     def do_start(self):
62         if self.simulation.state < ResourceState.STARTED:
63             self.debug("---- RESCHEDULING START ----" )
64             self.ec.schedule(reschedule_delay, self.start)
65         else:
66             self._configure_traces()
67
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, 
73                     stderr = stderr)
74
75             super(NetNSApplication, self).do_start()
76             self._start_time = self.emulation.start_time
77
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")
85
86     @property
87     def state(self):
88         if self._state == ResourceState.STARTED:
89             retcode = self.emulation.invoke(self.uuid, "poll")
90    
91             if retcode:
92                 if ret == 0:
93                     self.set_stopped()
94                 else:
95                     out = ""
96                     msg = " Failed to execute command '%s'" % self.get("command")
97                     err = self.trace("stderr")
98                     self.error(msg, out, err)
99                     self.do_fail()
100
101         return self._state
102