cbe599e1c639c40910cb6babaed8bef06549b70e
[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
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.Design)
38
39         count = Attribute("c",
40             "Total number of pings",
41             type = Types.Double,
42             flags = Flags.Design)
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.Design)
48  
49         prefix = Attribute("prefix",
50             "Prefix to serve content (e.g. ccnx:/name/prefix)",
51             flags = Flags.Design)
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.get_rtype())
65         if ccnpingserver: return ccnpingserver[0]
66         return None
67
68     def do_start(self):
69         if not self.ccnpingserver or \
70                 self.ccnpingserver.state < ResourceState.STARTED:
71             self.debug("---- RESCHEDULING START----  ccnpingserver state %s " % \
72                     self.ccnpingserver.state )
73             self.ec.schedule(reschedule_delay, self.start)
74         else:
75             super(LinuxCCNPing, self).do_start()
76  
77     @property
78     def _start_command(self):
79         args = []
80         args.append("ccnping")
81         args.append(self.get("prefix"))
82         if self.get("c"):
83             args.append("-c %d" % self.get("c"))
84         if self.get("n"):
85             args.append("-n %d" % self.get("n"))
86         if self.get("i"):
87             args.append("-i %.2f" % self.get("i"))
88
89         command = " ".join(args)
90
91         return command
92
93     def valid_connection(self, guid):
94         # TODO: Validate!
95         return True
96