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