8e40c3bdd9c0bea53c3311fa72c8d0f374d55f23
[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 ## Create the experiment controller
38 ec = ExperimentController(exp_id = "demo_CCN")
39
40 ## Register node 1
41 node1 = ec.register_resource("LinuxNode")
42 # Set the hostname of the first node to use for the experiment
43 hostname1 = "peeramidion.irisa.fr" ##### <<< ASSIGN the hostname of a host you have SSSH access to >>>
44 ec.set(node1, "hostname", hostname1)
45 # username should be your SSH user 
46 ec.set(node1, "username", ssh_user)
47 # Absolute path to the SSH private key
48 ec.set(node1, "identity", ssh_key)
49 # Clean all files, results, etc, from previous experiments wit the same exp_id
50 ec.set(node1, "cleanExperiment", True)
51 # Kill all running processes in the node before running the experiment
52 ec.set(node1, "cleanProcesses", True)
53
54 ## Register node 2 
55 node2 = ec.register_resource("LinuxNode")
56 # Set the hostname of the first node to use for the experiment
57 hostname2 = "planetlab2.upc.es" ##### <<< ASSIGN the hostname of a host you have SSSH access to >>>
58 ec.set(node2, "hostname", hostname2)
59 # username should be your SSH user 
60 ec.set(node2, "username", ssh_user)
61 # Absolute path to the SSH private key
62 ec.set(node2, "identity", ssh_key)
63 # Clean all files, results, etc, from previous experiments wit the same exp_id
64 ec.set(node2, "cleanExperiment", True)
65 # Kill all running processes in the node before running the experiment
66 ec.set(node2, "cleanProcesses", True)
67
68 ## Register a CCN daemon in node 1
69 ccnd1 = ec.register_resource("LinuxCCND")
70 # Set ccnd log level to 7
71 ec.set(ccnd1, "debug", 7)
72 ec.register_connection(ccnd1, node1)
73
74 ## Register a CCN daemon in node 2
75 ccnd2 = ec.register_resource("LinuxCCND")
76 # Set ccnd log level to 7
77 ec.set(ccnd2, "debug", 7)
78 ec.register_connection(ccnd2, node2)
79
80 ## Register a repository in node 1
81 ccnr1 = ec.register_resource("LinuxCCNR")
82 ec.register_connection(ccnr1, ccnd1)
83
84 ## Push the file into the repository
85 local_path_to_content = os.path.join(
86         os.path.dirname(os.path.realpath(__file__)),
87             "..", "..",
88             "big_buck_bunny_240p_mpeg4_lq.ts")
89
90 # Register a FIB entry from node 1 to node 2
91 co = ec.register_resource("LinuxCCNContent")
92 ec.set(co, "contentName", "ccnx:/test/FILE1")
93 # NEPI will upload the specified file to the remote node and write it
94 # into the CCN repository
95 ec.set(co, "content", local_path_to_content)
96 ec.register_connection(co, ccnr1)
97
98 # Register a FIB entry from node 1 to node 2
99 entry1 = ec.register_resource("LinuxFIBEntry")
100 ec.set(entry1, "host", hostname2)
101 ec.register_connection(entry1, ccnd1)
102
103 # Register a FIB entry from node 2 to node 1
104 entry2 = ec.register_resource("LinuxFIBEntry")
105 ec.set(entry2, "host", hostname1)
106 ec.register_connection(entry2, ccnd2)
107
108 ## Retrieve the file stored in node 1 from node 2
109 command = "ccncat ccnx:/test/FILE1"
110 app = ec.register_resource("LinuxCCNApplication")
111 ec.set(app, "command", command)
112 ec.register_connection(app, ccnd2)
113
114 # Register a collector to automatically collect the ccnd logs
115 # to a local directory
116 results_dir = "/tmp/demo_CCN_results"
117 col1 = ec.register_resource("Collector")
118 ec.set(col1, "traceName", "stderr")
119 ec.set(col1, "storeDir", results_dir)
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, "storeDir", results_dir)
126 ec.set(col2, "subDir", hostname2)
127 ec.register_connection(col2, ccnd2)
128
129 ## Deploy all resources
130 ec.deploy()
131
132 # Wait until the ccncat is finished
133 ec.wait_finished([app])
134
135 ## CCND logs will be collected to the results_dir upon shutdown.
136 ## We can aldo get the content of the logs now:
137 #print "LOG2", ec.trace(ccnd1, "stderr")
138 #print "LOG 1", ec.trace(ccnd2, "stderr")
139
140 ec.shutdown()