c7ce3bf8ca13f17f786cc05d011b84eed3e1a0eb
[nepi.git] / src / nepi / resources / linux / ccn / ccncontent.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 clsinit_copy, ResourceState, \
22     ResourceAction, reschedule_delay
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.linux.ccn.ccnr import LinuxCCNR
25 from nepi.util.timefuncs import tnow
26
27 import os
28
29 @clsinit_copy
30 class LinuxCCNContent(LinuxApplication):
31     _rtype = "LinuxCCNContent"
32
33     @classmethod
34     def _register_attributes(cls):
35         content_name = Attribute("contentName",
36                 "The name of the content to publish (e.g. ccn:/VIDEO) ",
37                 flags = Flags.Design)
38
39         content = Attribute("content",
40                 "The content to publish. It can be a path to a file or plain text ",
41                 flags = Flags.Design)
42
43         scope = Attribute("scope",
44                 "Use the given scope on the start-write request (if -r specified). "
45                 "scope can be 1 (local), 2 (neighborhood), or 3 (unlimited). "
46                 "Note that a scope of 3 is encoded as the absence of any scope in the interest. ",
47                 type = Types.Integer,
48                 default = 1,
49                 flags = Flags.Design)
50
51         cls._register_attribute(content_name)
52         cls._register_attribute(content)
53         cls._register_attribute(scope)
54
55     def __init__(self, ec, guid):
56         super(LinuxCCNContent, self).__init__(ec, guid)
57         self._home = "content-%s" % self.guid
58         
59     @property
60     def ccnr(self):
61         ccnr = self.get_connected(LinuxCCNR.get_rtype())
62         if ccnr: return ccnr[0]
63         return None
64
65     @property
66     def ccnd(self):
67         if self.ccnr: return self.ccnr.ccnd
68         return None
69
70     @property
71     def node(self):
72         if self.ccnr: return self.ccnr.node
73         return None
74
75     def do_deploy(self):
76         if not self.ccnr or self.ccnr.state < ResourceState.READY:
77             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
78             
79             # ccnr needs to wait until ccnd is deployed and running
80             self.ec.schedule(reschedule_delay, self.deploy)
81         else:
82             if not self.get("command"):
83                 self.set("command", self._start_command)
84
85             if not self.get("env"):
86                 self.set("env", self._environment)
87
88             # set content to stdin, so the content will be
89             # uploaded during provision
90             self.set("stdin", self.get("content"))
91
92             command = self.get("command")
93
94             self.info("Deploying command '%s' " % command)
95
96             self.do_discover()
97             self.do_provision()
98
99             self.set_ready()
100
101     def upload_start_command(self):
102         command = self.get("command")
103         env = self.get("env")
104
105         self.info("Uploading command '%s'" % command)
106
107         # We want to make sure the content is published
108         # before the experiment starts.
109         # Run the command as a bash script in the background, 
110         # in the host ( but wait until the command has
111         # finished to continue )
112         env = self.replace_paths(env)
113         command = self.replace_paths(command)
114
115         (out, err), proc = self.execute_command(command, 
116                 env, blocking = True)
117
118         if proc.poll():
119             msg = "Failed to execute command"
120             self.error(msg, out, err)
121             raise RuntimeError, msg
122
123     def do_start(self):
124         if self.state == ResourceState.READY:
125             command = self.get("command")
126             self.info("Starting command '%s'" % command)
127
128             self.set_started()
129         else:
130             msg = " Failed to execute command '%s'" % command
131             self.error(msg, out, err)
132             raise RuntimeError, msg
133
134     @property
135     def _start_command(self):
136         command = ["ccnseqwriter"]
137         command.append("-r %s" % self.get("contentName"))
138         command.append("-s %d" % self.get("scope"))
139         command.append("< %s" % os.path.join(self.app_home, 'stdin'))
140
141         command = " ".join(command)
142         return command
143
144     @property
145     def _environment(self):
146         return self.ccnd.path
147        
148     def valid_connection(self, guid):
149         # TODO: Validate!
150         return True
151