10814ae7a9368954cba65d4822635fccdfc56066
[nepi.git] / examples / linux / ccn / two_nodes_file_retrieval.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 # Instructions to run this example:
21 #
22 # 1. First edit the script file where required (See ASSING messages)
23 #
24 # 2. Then, run the script:
25 #
26 # $ cd <path-to-nepi>
27 # $ PYTHONPATH=$PYTHONPATHS:src python examples/linux/ccn/two_nodes_file_retrieval.py
28 #
29
30 from nepi.execution.ec import ExperimentController
31
32 import os
33
34 ssh_key = ####### <<< ASSING the absolute path to the private SSH key to login into the remote host >>>
35 ssh_user = ####### <<< ASSING the SSH username >>>
36
37 results_dir = "/tmp/demo_CCN_results"
38
39 ## Create the experiment controller
40 ec = ExperimentController(exp_id = "demo_CCN", local_dir = results_dir)
41
42 ## Register node 1
43 node1 = ec.register_resource("LinuxNode")
44 # Set the hostname of the first node to use for the experiment
45 hostname1 = "peeramidion.irisa.fr" ##### <<< ASSIGN the hostname of a host you have SSSH access to >>>
46 ec.set(node1, "hostname", hostname1)
47 # username should be your SSH user 
48 ec.set(node1, "username", ssh_user)
49 # Absolute path to the SSH private key
50 ec.set(node1, "identity", ssh_key)
51 # Clean all files, results, etc, from previous experiments wit the same exp_id
52 ec.set(node1, "cleanExperiment", True)
53 # Kill all running processes in the node before running the experiment
54 ec.set(node1, "cleanProcesses", True)
55
56 ## Register node 2 
57 node2 = ec.register_resource("LinuxNode")
58 # Set the hostname of the first node to use for the experiment
59 hostname2 = "planetlab2.upc.es" ##### <<< ASSIGN the hostname of a host you have SSSH access to >>>
60 ec.set(node2, "hostname", hostname2)
61 # username should be your SSH user 
62 ec.set(node2, "username", ssh_user)
63 # Absolute path to the SSH private key
64 ec.set(node2, "identity", ssh_key)
65 # Clean all files, results, etc, from previous experiments wit the same exp_id
66 ec.set(node2, "cleanExperiment", True)
67 # Kill all running processes in the node before running the experiment
68 ec.set(node2, "cleanProcesses", True)
69
70 ## Register a CCN daemon in node 1
71 ccnd1 = ec.register_resource("LinuxCCND")
72 # Set ccnd log level to 7
73 ec.set(ccnd1, "debug", 7)
74 ec.register_connection(ccnd1, node1)
75
76 ## Register a CCN daemon in node 2
77 ccnd2 = ec.register_resource("LinuxCCND")
78 # Set ccnd log level to 7
79 ec.set(ccnd2, "debug", 7)
80 ec.register_connection(ccnd2, node2)
81
82 ## Register a repository in node 1
83 ccnr1 = ec.register_resource("LinuxCCNR")
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             "..", "..",
90             "big_buck_bunny_240p_mpeg4_lq.ts")
91
92 # Register a FIB entry from node 1 to node 2
93 co = ec.register_resource("LinuxCCNContent")
94 ec.set(co, "contentName", "ccnx:/test/FILE1")
95 # NEPI will upload the specified file to the remote node and write it
96 # into the CCN repository
97 ec.set(co, "content", local_path_to_content)
98 ec.register_connection(co, ccnr1)
99
100 # Register a FIB entry from node 1 to node 2
101 entry1 = ec.register_resource("LinuxFIBEntry")
102 ec.set(entry1, "host", hostname2)
103 ec.register_connection(entry1, ccnd1)
104
105 # Register a FIB entry from node 2 to node 1
106 entry2 = ec.register_resource("LinuxFIBEntry")
107 ec.set(entry2, "host", hostname1)
108 ec.register_connection(entry2, ccnd2)
109
110 ## Retrieve the file stored in node 1 from node 2
111 command = "ccncat ccnx:/test/FILE1"
112 app = ec.register_resource("LinuxCCNApplication")
113 ec.set(app, "command", command)
114 ec.register_connection(app, ccnd2)
115
116 # Register a collector to automatically collect the ccnd logs
117 # to a local directory
118 col1 = ec.register_resource("Collector")
119 ec.set(col1, "traceName", "stderr")
120 ec.set(col1, "subDir", hostname1)
121 ec.register_connection(col1, ccnd1)
122
123 col2 = ec.register_resource("Collector")
124 ec.set(col2, "traceName", "stderr")
125 ec.set(col2, "subDir", hostname2)
126 ec.register_connection(col2, ccnd2)
127
128 ## Deploy all resources
129 ec.deploy()
130
131 # Wait until the ccncat is finished
132 ec.wait_finished([app])
133
134 ## CCND logs will be collected to the results_dir upon shutdown.
135 ## We can aldo get the content of the logs now:
136 #print "LOG2", ec.trace(ccnd1, "stderr")
137 #print "LOG 1", ec.trace(ccnd2, "stderr")
138
139 ec.shutdown()