4b5d6f40d8defd231917700f50a852ce9095ca58
[nepi.git] / examples / linux / ccn_simple_transfer.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 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
20 # Example of how to run this experiment (replace with your information):
21 #
22 # $ cd <path-to-nepi>
23 # python examples/linux/ccn_simple_transfer.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
24
25 # CCN topology:
26 #
27 #                
28 #                 
29 #  content                  ccncat
30 #  Linux host               Linux host
31 #     0 ------- network -------- 1
32 #
33
34 from nepi.execution.ec import ExperimentController
35
36 from optparse import OptionParser
37 import os
38
39 usage = ("usage: %prog -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>")
40
41 parser = OptionParser(usage = usage)
42 parser.add_option("-a", "--hostname1", dest="hostname1", 
43         help="Remote host 1", type="str")
44 parser.add_option("-b", "--hostname2", dest="hostname2", 
45         help="Remote host 2", type="str")
46 parser.add_option("-u", "--username", dest="username", 
47         help="Username to SSH to remote host", type="str")
48 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
49         help="Path to private SSH key to be used for connection", 
50         type="str")
51 (options, args) = parser.parse_args()
52
53 hostname1 = options.hostname1
54 hostname2 = options.hostname2
55 username = options.username
56 ssh_key = options.ssh_key
57
58 ## Create the experiment controller
59 ec = ExperimentController(exp_id = "ccn_simple_transfer")
60
61 ##### CONFIGURING NODE 1
62
63 ## Register node 1
64 node1 = ec.register_resource("linux::Node")
65 # Set the hostname of the first node to use for the experiment
66 ec.set(node1, "hostname", hostname1)
67 # username should be your SSH user 
68 ec.set(node1, "username", username)
69 # Absolute path to the SSH private key
70 ec.set(node1, "identity", ssh_key)
71 # Clean all files, results, etc, from previous experiments wit the same exp_id
72 ec.set(node1, "cleanExperiment", True)
73 # Kill all running processes in the node before running the experiment
74 ec.set(node1, "cleanProcesses", True)
75
76 ## Register a CCN daemon in node 1
77 ccnd1 = ec.register_resource("linux::CCND")
78 # Set ccnd log level to 7
79 ec.set(ccnd1, "debug", 7)
80 ec.register_connection(ccnd1, node1)
81
82 ## Register a repository in node 1
83 ccnr1 = ec.register_resource("linux::CCNR")
84 ec.register_connection(ccnr1, ccnd1)
85
86 ## Push the file into the repository
87 local_path_to_content = os.path.join(
88         os.path.dirname(os.path.realpath(__file__)),
89             "..", "big_buck_bunny_240p_mpeg4_lq.ts")
90
91 content_name = "ccnx:/test/FILE"
92
93 # Add a content to the repository
94 co = ec.register_resource("linux::CCNContent")
95 ec.set(co, "contentName", content_name)
96 # NEPI will upload the specified file to the remote node and write it
97 # into the CCN repository
98 ec.set(co, "content", local_path_to_content)
99 ec.register_connection(co, ccnr1)
100
101 ##### CONFIGURING NODE 2
102
103 ## Register node 2 
104 node2 = ec.register_resource("linux::Node")
105 # Set the hostname of the first node to use for the experiment
106 ec.set(node2, "hostname", hostname2)
107 # username should be your SSH user 
108 ec.set(node2, "username", username)
109 # Absolute path to the SSH private key
110 ec.set(node2, "identity", ssh_key)
111 # Clean all files, results, etc, from previous experiments wit the same exp_id
112 ec.set(node2, "cleanExperiment", True)
113 # Kill all running processes in the node before running the experiment
114 ec.set(node2, "cleanProcesses", True)
115
116 ## Register a CCN daemon in node 2
117 ccnd2 = ec.register_resource("linux::CCND")
118 # Set ccnd log level to 7
119 ec.set(ccnd2, "debug", 7)
120 ec.register_connection(ccnd2, node2)
121
122 ## Retrieve the file stored in node 1 from node 2
123 ccncat = ec.register_resource("linux::CCNCat")
124 ec.set(ccncat, "contentName", content_name)
125 ec.register_connection(ccncat, ccnd2)
126
127 ##### INTERCONNECTING CCN NODES ...
128
129 # Register a FIB entry from node 1 to node 2
130 entry1 = ec.register_resource("linux::FIBEntry")
131 ec.set(entry1, "host", hostname2)
132 ec.register_connection(entry1, ccnd1)
133
134 # Register a FIB entry from node 2 to node 1
135 entry2 = ec.register_resource("linux::FIBEntry")
136 ec.set(entry2, "host", hostname1)
137 ec.register_connection(entry2, ccnd2)
138
139 ##### STARTING THE EXPERIMENT
140
141 ## Deploy all resources
142 ec.deploy()
143
144 # Wait until the ccncat is finished
145 ec.wait_finished([ccncat])
146
147 stdout = ec.trace(ccncat, "stdout")
148 f = open("video.ts", "w")
149 f.write(stdout)
150 f.close()
151
152 ec.shutdown()
153
154 print "Transfered FILE stored localy at video.ts"