rename src/nepi/ into just nepi/
[nepi.git] / 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 version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import ResourceManager, clsinit_copy, \
21         ResourceState
22 from nepi.resources.linux.ccn.ccnpingserver import LinuxCCNPingServer
23 from nepi.util.timefuncs import tnow, tdiffsec
24
25 import os
26
27 @clsinit_copy
28 class LinuxCCNPing(LinuxCCNPingServer):
29     _rtype = "linux::CCNPing"
30
31     @classmethod
32     def _register_attributes(cls):
33         interval = Attribute("i",
34             "Set ping interval in seconds (minimum 0.10 second) ",
35             type = Types.Integer,
36             flags = Flags.Design)
37
38         count = Attribute("c",
39             "Total number of pings",
40             type = Types.Double,
41             flags = Flags.Design)
42
43         number = Attribute("n",
44             "Set the starting number, the number is incremented by 1 after each Interest ",
45             type = Types.Integer,
46             flags = Flags.Design)
47  
48         prefix = Attribute("prefix",
49             "Prefix to serve content (e.g. ccnx:/name/prefix)",
50             flags = Flags.Design)
51
52         cls._register_attribute(interval)
53         cls._register_attribute(count)
54         cls._register_attribute(number)
55         cls._register_attribute(prefix)
56
57     def __init__(self, ec, guid):
58         super(LinuxCCNPing, self).__init__(ec, guid)
59         self._home = "ccnping-%s" % self.guid
60
61     @property
62     def ccnpingserver(self):
63         ccnpingserver = self.get_connected(LinuxCCNPingServer.get_rtype())
64         if ccnpingserver: return ccnpingserver[0]
65         return None
66
67     def do_start(self):
68         if not self.ccnpingserver or \
69                 self.ccnpingserver.state < ResourceState.STARTED:
70             self.debug("---- RESCHEDULING START----  ccnpingserver state %s " % \
71                     self.ccnpingserver.state )
72             self.ec.schedule(self.reschedule_delay, self.start)
73         else:
74             super(LinuxCCNPing, self).do_start()
75  
76     @property
77     def _start_command(self):
78         args = []
79         args.append("ccnping")
80         args.append(self.get("prefix"))
81         if self.get("c"):
82             args.append("-c %d" % self.get("c"))
83         if self.get("n"):
84             args.append("-n %d" % self.get("n"))
85         if self.get("i"):
86             args.append("-i %.2f" % self.get("i"))
87
88         command = " ".join(args)
89
90         return command
91
92     def valid_connection(self, guid):
93         # TODO: Validate!
94         return True
95