Improving linux examples
[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 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
36
37 parser = OptionParser(usage = usage)
38 parser.add_option("-a", "--hostname1", dest="hostname1", 
39         help="Remote host 1", type="str")
40 parser.add_option("-b", "--hostname2", dest="hostname2", 
41         help="Remote host 2", type="str")
42 parser.add_option("-u", "--username", dest="username", 
43         help="Username to SSH to remote host", type="str")
44 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
45         help="Path to private SSH key to be used for connection", 
46         type="str")
47 (options, args) = parser.parse_args()
48
49 hostname1 = options.hostname1
50 hostname2 = options.hostname2
51 username = options.username
52 ssh_key = options.ssh_key
53
54 ## Create the experiment controller
55 ec = ExperimentController(exp_id = "file_transfer")
56
57 ## Register node 1
58 node1 = ec.register_resource("LinuxNode")
59 # Set the hostname of the first node to use for the experiment
60 ec.set(node1, "hostname", hostname1)
61 # username should be your SSH user 
62 ec.set(node1, "username", username)
63 # Absolute path to the SSH private key
64 ec.set(node1, "identity", ssh_key)
65 # Clean all files, results, etc, from previous experiments wit the same exp_id
66 ec.set(node1, "cleanExperiment", True)
67 # Kill all running processes in the node before running the experiment
68 ec.set(node1, "cleanProcesses", True)
69
70 ## Register node 2 
71 node2 = ec.register_resource("LinuxNode")
72 # Set the hostname of the first node to use for the experiment
73 ec.set(node2, "hostname", hostname2)
74 # username should be your SSH user 
75 ec.set(node2, "username", username)
76 # Absolute path to the SSH private key
77 ec.set(node2, "identity", ssh_key)
78 # Clean all files, results, etc, from previous experiments wit the same exp_id
79 ec.set(node2, "cleanExperiment", True)
80 # Kill all running processes in the node before running the experiment
81 ec.set(node2, "cleanProcesses", True)
82
83 # Register server
84 video = "big_buck_bunny_240p_mpeg4_lq.ts"
85 local_path_to_video = os.path.join(
86         os.path.dirname(os.path.realpath(__file__)),
87             "..", video)
88
89 command = "cat ${SHARE}/%s | pv -fbt 2> bw.txt | nc %s 1234" % ( 
90         video, hostname2 )
91
92 server = ec.register_resource("LinuxApplication")
93 ec.set(server, "depends", "pv nc tcpdump")
94 ec.set(server, "files", local_path_to_video)
95 ec.set(server, "command", command)
96 ec.register_connection(server, node1)
97
98 # Register client
99 command = ("sudo -S dbus-uuidgen --ensure; sleep 3; "
100         "vlc -I dummy rtp://%s:5004/%s "
101         "--sout '#std{access=file,mux=ts,dst=VIDEO}'") % \
102                 (hostname2, video)
103
104 # Note: is important to add the -d option in nc command to not attempt to read from the 
105 # stdin
106 # if not nc in the client side close the socket suddently if runned in background
107 command =  "nc -dl 1234 > %s" % video
108
109 client = ec.register_resource("LinuxApplication")
110 ec.set(client, "depends", "nc")
111 ec.set(client, "command", command)
112 ec.register_connection(client, node2)
113
114 # Register a tcpdump in the server node to monitor the file transfer 
115 command = "tcpdump -ni eth0 -w file_transfer.pcap -s0 port 1234 2>&1"
116
117 capture = ec.register_resource("LinuxApplication")
118 ec.set(capture, "depends", "tcpdump")
119 ec.set(capture, "command", command)
120 ec.set(capture, "sudo", True)
121 ec.register_connection(capture, node1)
122
123 # Register conditions 1. nodes ; 2. start tcpdump capture ; 3. client listen port 1234 ;
124 # 4. server start sending video
125 ec.register_condition(server, ResourceAction.START, client, ResourceState.STARTED) 
126 ec.register_condition(client, ResourceAction.START, capture, ResourceState.STARTED)
127
128 # Deploy
129 ec.deploy()
130
131 # Wait until the applications are finish to retrive the traces
132 ec.wait_finished([server, client])
133
134 # Retrieve traces from nc and tcpdump
135 bw = ec.trace(server, "bw.txt")
136 pcap = ec.trace(capture, "file_transfer.pcap")
137
138 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
139 f = open("bw.txt", "w")
140 f.write(bw)
141 f.close()
142 f = open("video_transfer.pcap", "w")
143 f.write(pcap)
144 f.close()
145
146 ec.shutdown()
147