4 # NEPI, a framework to manage network experiments
5 # Copyright (C) 2013 INRIA
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 as
9 # published by the Free Software Foundation;
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 # NOTE: This experiment example uses the generic LinuxApplication
22 # ResourceManager to do the CCN set up in the hosts.
23 # Alternatively, CCN specific ResourceManagers can be used
24 # (i.e. LinuxCCND, LinuxCCNR, etc...), and those require less
25 # manual configuration.
34 # Linux host Linux host
35 # 0 ------- Internet ------ 0
38 # Example of how to run this experiment (replace with your information):
41 # python examples/linux/ccn_advanced_transfer.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
43 from __future__ import print_function
45 from nepi.execution.ec import ExperimentController
46 from nepi.execution.resource import ResourceAction, ResourceState
48 from optparse import OptionParser
51 usage = ("usage: %prog -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>")
53 parser = OptionParser(usage = usage)
54 parser.add_option("-a", "--hostname1", dest="hostname1",
55 help="Remote host 1", type="str")
56 parser.add_option("-b", "--hostname2", dest="hostname2",
57 help="Remote host 2", type="str")
58 parser.add_option("-u", "--username", dest="username",
59 help="Username to SSH to remote host", type="str")
60 parser.add_option("-i", "--ssh-key", dest="ssh_key",
61 help="Path to private SSH key to be used for connection",
63 (options, args) = parser.parse_args()
65 hostname1 = options.hostname1
66 hostname2 = options.hostname2
67 username = options.username
68 ssh_key = options.ssh_key
70 def add_node(ec, host, user, ssh_key = None):
71 node = ec.register_resource("linux::Node")
72 ec.set(node, "hostname", host)
73 ec.set(node, "username", user)
74 ec.set(node, "identity", ssh_key)
75 ec.set(node, "cleanHome", True)
76 ec.set(node, "cleanProcesses", True)
79 def add_ccnd(ec, peers):
80 # Dependencies for Fedora
81 depends = ( " autoconf openssl-devel expat-devel libpcap-devel "
82 " ecryptfs-utils-devel libxml2-devel automake gawk "
83 " gcc gcc-c++ git pcre-devel make ")
86 # depends = ( " autoconf libssl-dev libexpat-dev libpcap-dev "
87 # " libecryptfs0 libxml2-utils automake gawk gcc g++ "
88 # " git-core pkg-config libpcre3-dev make ")
90 sources = "http://www.ccnx.org/releases/ccnx-0.8.2.tar.gz"
93 # Evaluate if ccnx binaries are already installed
95 " test -f ${BIN}/ccnx-0.8.2/bin/ccnd"
97 # If not, untar and build
99 " mkdir -p ${SRC}/ccnx-0.8.2 && "
100 " tar xf ${SRC}/ccnx-0.8.2.tar.gz --strip-components=1 -C ${SRC}/ccnx-0.8.2 "
102 "cd ${SRC}/ccnx-0.8.2 && "
103 # Just execute and silence warnings...
104 "( ./configure && make ) "
108 # Evaluate if ccnx binaries are already installed
110 " test -f ${BIN}/ccnx-0.8.2/bin/ccnd"
112 " mkdir -p ${BIN}/ccnx-0.8.2/bin && "
113 " cp -r ${SRC}/ccnx-0.8.2/bin ${BIN}/ccnx-0.8.2"
117 env = "PATH=$PATH:${BIN}/ccnx-0.8.2/bin"
119 # BASH command -> ' ccndstart ; ccndc add ccnx:/ udp host ; ccnr '
120 command = "ccndstart && "
121 peers = ["ccndc add ccnx:/ udp %s" % peer for peer in peers]
122 command += " ; ".join(peers) + " && "
123 command += " ccnr & "
125 app = ec.register_resource("linux::Application")
126 ec.set(app, "depends", depends)
127 ec.set(app, "sources", sources)
128 ec.set(app, "install", install)
129 ec.set(app, "build", build)
130 ec.set(app, "env", env)
131 ec.set(app, "command", command)
135 def add_publish(ec, movie, content_name):
136 env = "PATH=$PATH:${BIN}/ccnx-0.8.2/bin"
137 command = "ccnseqwriter -r %s" % content_name
139 app = ec.register_resource("linux::Application")
140 ec.set(app, "stdin", movie)
141 ec.set(app, "env", env)
142 ec.set(app, "command", command)
146 def add_ccncat(ec, content_name):
147 env = "PATH=$PATH:${BIN}/ccnx-0.8.2/bin"
148 command = "ccncat %s" % content_name
150 app = ec.register_resource("linux::Application")
151 ec.set(app, "env", env)
152 ec.set(app, "command", command)
156 ## Create the experiment controller
157 ec = ExperimentController(exp_id = "ccn_advanced_transfer")
159 # Register first PlanetLab host
160 node1 = add_node(ec, hostname1, username, ssh_key)
162 # Register CCN setup for host
164 ccnd1 = add_ccnd(ec, peers)
165 ec.register_connection(ccnd1, node1)
167 # Register content producer application (ccnseqwriter)
168 ## Push the file into the repository
169 local_path_to_content = os.path.join(
170 os.path.dirname(os.path.realpath(__file__)),
171 "..", "big_buck_bunny_240p_mpeg4_lq.ts")
173 content_name = "ccnx:/test/FILE"
175 pub = add_publish(ec, local_path_to_content, content_name)
176 ec.register_connection(pub, node1)
178 # The movie can only be published after ccnd is running
179 ec.register_condition(pub, ResourceAction.START,
180 ccnd1, ResourceState.STARTED)
182 # Register Linux host
183 node2 = add_node(ec, hostname2, username, ssh_key)
185 # Register CCN setup for Linux host
187 ccnd2 = add_ccnd(ec, peers)
188 ec.register_connection(ccnd2, node2)
190 # Register consumer application (ccncat)
191 ccncat = add_ccncat(ec, content_name)
192 ec.register_connection(ccncat, node2)
194 # The file can only be retrieved after ccnd is running
195 ec.register_condition(ccncat, ResourceAction.START,
196 ccnd2, ResourceState.STARTED)
198 # And also, the file can only be retrieved after it was published
199 ec.register_condition(ccncat, ResourceAction.START,
200 pub, ResourceState.STARTED)
202 # Deploy all ResourceManagers
205 # Wait until the applications are finished
207 ec.wait_finished(apps)
209 stdout = ec.trace(ccncat, "stdout")
210 with open("video.ts", "w") as f:
213 # Shutdown the experiment controller
216 print("Transfered FILE stored localy at video.ts")