2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.trace import Trace, TraceAttr
22 from nepi.execution.resource import clsinit_copy, ResourceState, \
24 from nepi.resources.linux.ccn.ccnapplication import LinuxCCNApplication
25 from nepi.util.timefuncs import tnow
29 reschedule_delay = "0.5s"
31 # TODO: Add rest of options for ccndc!!!
32 # Implement ENTRY DELETE!!
35 class LinuxFIBEntry(LinuxCCNApplication):
36 _rtype = "LinuxFIBEntry"
39 def _register_attributes(cls):
40 uri = Attribute("uri",
41 "URI prefix to match and route for this FIB entry",
43 flags = Flags.ExecReadOnly)
45 protocol = Attribute("protocol",
46 "Transport protocol used in network connection to peer "
47 "for this FIB entry. One of 'udp' or 'tcp'.",
48 type = Types.Enumerate,
50 allowed = ["udp", "tcp"],
51 flags = Flags.ExecReadOnly)
53 host = Attribute("host",
54 "Peer host used in network connection for this FIB entry. ",
55 flags = Flags.ExecReadOnly)
57 port = Attribute("port",
58 "Peer port address used in network connection to peer "
59 "for this FIB entry.",
60 flags = Flags.ExecReadOnly)
62 cls._register_attribute(uri)
63 cls._register_attribute(protocol)
64 cls._register_attribute(host)
65 cls._register_attribute(port)
67 def __init__(self, ec, guid):
68 super(LinuxFIBEntry, self).__init__(ec, guid)
69 self._home = "fib-%s" % self.guid
72 # Wait until associated ccnd is provisioned
73 if not self.ccnd or self.ccnd.state < ResourceState.READY:
74 # ccnr needs to wait until ccnd is deployed and running
75 self.ec.schedule(reschedule_delay, self.deploy)
77 command = self._start_command
78 env = self._environment
80 self.set("command", command)
83 self.info("Deploying command '%s' " % command)
85 self.node.mkdir(self.app_home)
86 self.execute_command(command, env)
88 self.debug("----- READY ---- ")
89 self._ready_time = tnow()
90 self._state = ResourceState.READY
93 if self._state in [ResourceState.READY, ResourceState.STARTED]:
94 command = self.get("command")
95 self.info("Starting command '%s'" % command)
97 self._start_time = tnow()
98 self._state = ResourceState.STARTED
100 msg = " Failed to execute command '%s'" % command
101 self.error(msg, out, err)
102 self._state = ResourceState.FAILED
103 raise RuntimeError, msg
106 command = self.get('command')
107 env = self.get('env')
109 if self.state == ResourceState.STARTED:
110 self.info("Stopping command '%s'" % command)
112 command = self._stop_command
113 self.execute_command(command, env)
115 self._stop_time = tnow()
116 self._state = ResourceState.STOPPED
123 def _start_command(self):
124 uri = self.get("uri") or ""
125 protocol = self.get("protocol") or ""
126 host = self.get("host") or ""
127 port = self.get("port") or ""
129 # add ccnx:/example.com/ udp 224.0.0.204 52428
130 return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
132 "protocol": protocol,
138 def _stop_command(self):
139 uri = self.get("uri") or ""
140 protocol = self.get("protocol") or ""
141 host = self.get("host") or ""
142 port = self.get("port") or ""
144 # add ccnx:/example.com/ udp 224.0.0.204 52428
145 return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
147 "protocol": protocol,
152 def valid_connection(self, guid):