rename src/nepi/ into just nepi/
[nepi.git] / nepi / resources / linux / ccn / ccnpingserver.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.ccnapplication import LinuxCCNApplication
23 from nepi.util.timefuncs import tnow, tdiffsec
24
25 import os
26
27 @clsinit_copy
28 class LinuxCCNPingServer(LinuxCCNApplication):
29     _rtype = "linux::CCNPingServer"
30
31     @classmethod
32     def _register_attributes(cls):
33         daemon = Attribute("d",
34             "Run ccnping server as a daemon in background",
35             type = Types.Bool,
36             default = False,
37             flags = Flags.Design)
38
39         freshness = Attribute("x",
40             "Set FreshnessSeconds",
41             type = Types.Integer,
42             flags = Flags.Design)
43
44         prefix = Attribute("prefix",
45             "Prefix to serve content (e.g. ccnx:/name/prefix)",
46             flags = Flags.Design)
47
48         cls._register_attribute(daemon)
49         cls._register_attribute(freshness)
50         cls._register_attribute(prefix)
51
52     def __init__(self, ec, guid):
53         super(LinuxCCNPingServer, self).__init__(ec, guid)
54         self._home = "ccnping-serv-%s" % self.guid
55
56     def do_deploy(self):
57         if not self.get("command"):
58             self.set("command", self._start_command)
59
60         if not self.get("env"):
61             self.set("env", self._environment)
62
63         if not self.get("depends"):
64             self.set("depends", self._dependencies)
65
66         if not self.get("build"):
67             self.set("build", self._build)
68
69         if not self.get("install"):
70             self.set("install", self._install)
71
72         super(LinuxCCNPingServer, self).do_deploy()
73
74     @property
75     def _start_command(self):
76         args = []
77         args.append("ccnpingserver")
78         args.append(self.get("prefix"))
79         if self.get("d") == True:
80             args.append("-d")
81         if self.get("x"):
82             args.append("-x %d" % self.get("x"))
83
84         command = " ".join(args)
85
86         return command
87
88     @property
89     def _dependencies(self):
90         return "git"
91
92     @property
93     def _build(self):
94         return (
95             # Evaluate if ccnx binaries are already installed
96             " ( "
97                 " test -f ${BIN}/ccnping && "
98                 " echo 'binaries found, nothing to do' "
99             " ) || ( "
100             # If not, untar and build
101                 " ( "
102                     " git clone git://github.com/NDN-Routing/ccnping ${SRC}/ccnping "
103                  " ) && "
104                     # build
105                     "cd ${SRC}/ccnping && "
106                     " ( "
107                     " ./configure LDFLAGS=-L${SRC}/ccnx-0.7.2/lib CFLAGS=-I${SRC}/ccnx-0.7.2/include "
108                     " --prefix=${BIN}/ccnping && make "
109                     " ) "
110              " )") 
111
112     @property
113     def _install(self):
114         return (
115             # Evaluate if ccnx binaries are already installed
116             " ( "
117                 " test -f ${BIN}/ccnping && "
118                 " echo 'binaries found, nothing to do' "
119             " ) || ( "
120             # If not, install
121                 "  mkdir -p ${BIN}/ccnping && "
122                 "  mv ${SRC}/ccnping/ccnping ${BIN}/ccnping/ && "
123                 "  mv ${SRC}/ccnping/ccnpingserver ${BIN}/ccnping/ "
124             " )"
125             )
126
127     @property
128     def _environment(self):
129         return "%s:%s" % (self.ccnd.path, "${BIN}/ccnping")
130        
131     def valid_connection(self, guid):
132         # TODO: Validate!
133         return True
134