Modified FailureManager to abort only when critical resources fail
[nepi.git] / src / nepi / resources / linux / ccn / ccnping.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 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 ResourceManager, clsinit_copy, \
22         ResourceState, reschedule_delay, failtrap
23 from nepi.resources.linux.ccn.ccnpingserver import LinuxCCNPingServer
24 from nepi.util.timefuncs import tnow, tdiffsec
25
26 import os
27
28 @clsinit_copy
29 class LinuxCCNPing(LinuxCCNPingServer):
30     _rtype = "LinuxCCNPing"
31
32     @classmethod
33     def _register_attributes(cls):
34         interval = Attribute("i",
35             "Set ping interval in seconds (minimum 0.10 second) ",
36             type = Types.Integer,
37             flags = Flags.ExecReadOnly)
38
39         count = Attribute("c",
40             "Total number of pings",
41             type = Types.Double,
42             flags = Flags.ExecReadOnly)
43
44         number = Attribute("n",
45             "Set the starting number, the number is incremented by 1 after each Interest ",
46             type = Types.Integer,
47             flags = Flags.ExecReadOnly)
48  
49         prefix = Attribute("prefix",
50             "Prefix to serve content (e.g. ccnx:/name/prefix)",
51             flags = Flags.ExecReadOnly)
52
53         cls._register_attribute(interval)
54         cls._register_attribute(count)
55         cls._register_attribute(number)
56         cls._register_attribute(prefix)
57
58     def __init__(self, ec, guid):
59         super(LinuxCCNPing, self).__init__(ec, guid)
60         self._home = "ccnping-%s" % self.guid
61
62     @property
63     def ccnpingserver(self):
64         ccnpingserver = self.get_connected(LinuxCCNPingServer.rtype())
65         if ccnpingserver: return ccnpingserver[0]
66         return None
67
68     @failtrap
69     def start(self):
70         if not self.ccnpingserver or \
71                 self.ccnpingserver.state < ResourceState.STARTED:
72             self.debug("---- RESCHEDULING START----  ccnpingserver state %s " % \
73                     self.ccnpingserver.state )
74             self.ec.schedule(reschedule_delay, self.start)
75         else:
76             super(LinuxCCNPing, self).start()
77  
78     @property
79     def _start_command(self):
80         args = []
81         args.append("ccnping")
82         args.append(self.get("prefix"))
83         if self.get("c"):
84             args.append("-c %d" % self.get("c"))
85         if self.get("n"):
86             args.append("-n %d" % self.get("n"))
87         if self.get("i"):
88             args.append("-i %.2f" % self.get("i"))
89
90         command = " ".join(args)
91
92         return command
93
94     def valid_connection(self, guid):
95         # TODO: Validate!
96         return True
97