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