Removing repeated command declaration in examples/linux/file_transfer.py
[nepi.git] / examples / linux / file_transfer.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
20 #         Alina Quereilhac <alina.quereilhac@inria.fr>
21 #
22 #
23 # Example of how to run this experiment (replace with your credentials):
24 #
25 # $ cd <path-to-nepi>
26 # $ PYTHONPATH=$PYTHONPATH:~/repos/nepi/src python examples/linux/file_transfer.py -u inria_nepi -i ~/.ssh/id_rsa_planetlab -a planetlab1.u-strasbg.fr -b planetlab1.utt.fr
27
28
29 from nepi.execution.ec import ExperimentController
30 from nepi.execution.resource import ResourceAction, ResourceState
31
32 from optparse import OptionParser, SUPPRESS_HELP
33 import os
34
35
36 # The pluser and plpassword are the ones used to login in the PlanetLab web 
37 # site. Replace with your own user and password account information.
38 pl_password =  os.environ.get("PL_PASS")
39 pl_user =  os.environ.get("PL_USER")
40 username =  os.environ.get("PL_SLICE")
41 ssh_key =  "/home/alina/.ssh/id_rsa_planetlab"
42
43 ## Create the experiment controller
44 ec = ExperimentController(exp_id = "file_transfer")
45
46 ## Register node 1
47 node1 = ec.register_resource("PlanetlabNode")
48 # Set the hostname of the first node to use for the experiment
49 ec.set(node1, "pluser", pl_user)
50 ec.set(node1, "plpassword", pl_password)
51 # username should be your SSH user 
52 ec.set(node1, "username", username)
53 # Absolute path to the SSH private key
54 ec.set(node1, "identity", ssh_key)
55 # Clean all files, results, etc, from previous experiments wit the same exp_id
56 ec.set(node1, "cleanExperiment", True)
57 # Kill all running processes in the node before running the experiment
58 ec.set(node1, "cleanProcesses", True)
59
60 ## Register node 2 
61 node2 = ec.register_resource("PlanetlabNode")
62 # Set the hostname of the first node to use for the experiment
63 ec.set(node2, "pluser", pl_user)
64 ec.set(node2, "plpassword", pl_password)
65 # Set the hostname of the first node to use for the experiment
66 ec.set(node2, "hostname", hostname2)
67 # username should be your SSH user 
68 ec.set(node2, "username", username)
69 # Absolute path to the SSH private key
70 ec.set(node2, "identity", ssh_key)
71 # Clean all files, results, etc, from previous experiments wit the same exp_id
72 ec.set(node2, "cleanExperiment", True)
73 # Kill all running processes in the node before running the experiment
74 ec.set(node2, "cleanProcesses", True)
75
76 # Register server
77 video = "big_buck_bunny_240p_mpeg4_lq.ts"
78 local_path_to_video = os.path.join(
79         os.path.dirname(os.path.realpath(__file__)),
80             "..", video)
81
82 command = "cat ${SHARE}/%s | pv -fbt 2> bw.txt | nc %s 1234" % ( 
83         video, hostname2 )
84
85 server = ec.register_resource("LinuxApplication")
86 ec.set(server, "depends", "pv nc tcpdump")
87 ec.set(server, "files", local_path_to_video)
88 ec.set(server, "command", command)
89 ec.register_connection(server, node1)
90
91 # Register client
92 command = ("sudo -S dbus-uuidgen --ensure; sleep 3; "
93         "vlc -I dummy rtp://%s:5004/%s "
94         "--sout '#std{access=file,mux=ts,dst=VIDEO}'") % \
95                 (hostname2, video)
96
97 # Note: is important to add the -d option in nc command to not attempt to read from the 
98 # stdin
99 # if not nc in the client side close the socket suddently if runned in background
100 command =  "nc -dl 1234 > %s" % video
101
102 client = ec.register_resource("LinuxApplication")
103 ec.set(client, "depends", "nc")
104 ec.set(client, "command", command)
105 ec.register_connection(client, node2)
106
107 # Register a tcpdump in the server node to monitor the file transfer 
108 command = "tcpdump -ni eth0 -w file_transfer.pcap -s0 port 1234 2>&1"
109
110 capture = ec.register_resource("LinuxApplication")
111 ec.set(capture, "depends", "tcpdump")
112 ec.set(capture, "command", command)
113 ec.set(capture, "sudo", True)
114 ec.register_connection(capture, node1)
115
116 # Register conditions 1. nodes ; 2. start tcpdump capture ; 3. client listen port 1234 ;
117 # 4. server start sending video
118 ec.register_condition(server, ResourceAction.START, client, ResourceState.STARTED) 
119 ec.register_condition(client, ResourceAction.START, capture, ResourceState.STARTED)
120
121 # Deploy
122 ec.deploy()
123
124 # Wait until the applications are finish to retrive the traces
125 ec.wait_finished([server, client])
126
127 # Retrieve traces from nc and tcpdump
128 bw = ec.trace(server, "bw.txt")
129 pcap = ec.trace(capture, "file_transfer.pcap")
130
131 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
132 f = open("bw.txt", "w")
133 f.write(bw)
134 f.close()
135 f = open("video_transfer.pcap", "w")
136 f.write(pcap)
137 f.close()
138
139 ec.shutdown()
140