25db8fba46cdbcae020d9eb791f526b5f98d2e21
[nepi.git] / examples / linux / vlc_streaming.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=$PYTHONPATH:~/repos/nepi/src python examples/linux/vlc_streaming.py -u inria_nepi -i ~/.ssh/id_rsa_planetlab -a planetlab1.u-strasbg.fr -b planetlab1.utt.fr | vlc -
28
29
30 from nepi.execution.ec import ExperimentController
31 from nepi.execution.resource import ResourceState, ResourceAction 
32
33 from optparse import OptionParser, SUPPRESS_HELP
34 import os
35
36
37 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
38
39 parser = OptionParser(usage = usage)
40 parser.add_option("-a", "--hostname1", dest="hostname1", 
41         help="Remote host 1", type="str")
42 parser.add_option("-b", "--hostname2", dest="hostname2", 
43         help="Remote host 2", type="str")
44 parser.add_option("-u", "--username", dest="username", 
45         help="Username to SSH to remote host", type="str")
46 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
47         help="Path to private SSH key to be used for connection", 
48         type="str")
49 (options, args) = parser.parse_args()
50
51 hostname1 = options.hostname1
52 hostname2 = options.hostname2
53 username = options.username
54 ssh_key = options.ssh_key
55
56 ## Create the experiment controller
57 ec = ExperimentController(exp_id = "demo_CCN")
58
59 ## Register node 1
60 node1 = ec.register_resource("LinuxNode")
61 # Set the hostname of the first node to use for the experiment
62 ec.set(node1, "hostname", hostname1)
63 # username should be your SSH user 
64 ec.set(node1, "username", username)
65 # Absolute path to the SSH private key
66 ec.set(node1, "identity", ssh_key)
67 # Clean all files, results, etc, from previous experiments wit the same exp_id
68 ec.set(node1, "cleanExperiment", True)
69 # Kill all running processes in the node before running the experiment
70 ec.set(node1, "cleanProcesses", True)
71
72 ## Register node 2 
73 node2 = ec.register_resource("LinuxNode")
74 # Set the hostname of the first node to use for the experiment
75 ec.set(node2, "hostname", hostname2)
76 # username should be your SSH user 
77 ec.set(node2, "username", username)
78 # Absolute path to the SSH private key
79 ec.set(node2, "identity", ssh_key)
80 # Clean all files, results, etc, from previous experiments wit the same exp_id
81 ec.set(node2, "cleanExperiment", True)
82 # Kill all running processes in the node before running the experiment
83 ec.set(node2, "cleanProcesses", True)
84
85 # Register VLC server
86 video = "big_buck_bunny_240p_mpeg4_lq.ts"
87 local_path_to_video = os.path.join(
88         os.path.dirname(os.path.realpath(__file__)),
89             "..", video)
90
91 command = ("sudo -S dbus-uuidgen --ensure; sleep 3;"
92           "vlc -I dummy -vvv ${SHARE}/%s " 
93           "--sout '#rtp{dst=%s,port=5004,mux=ts}' vlc://quit") % \
94                   (video, hostname2)
95
96 server = ec.register_resource("LinuxApplication")
97 ec.set(server, "depends", "vlc")
98 ec.set(server, "files", local_path_to_video)
99 ec.set(server, "command", command)
100 ec.register_connection(server, node1)
101
102 command = ("sudo -S dbus-uuidgen --ensure; sleep 3; "
103         "vlc -I dummy rtp://%s:5004/%s "
104         "--sout '#std{access=file,mux=ts,dst=VIDEO}'") % \
105                 (hostname2, video)
106
107 client = ec.register_resource("LinuxApplication")
108 ec.set(client, "depends", "vlc")
109 ec.set(client, "command", command)
110 ec.register_connection(client, node2)
111
112 # The stream can only be retrieved after ccnd is running
113 ec.register_condition(server, ResourceAction.START, 
114         client, ResourceState.STARTED)
115
116 ## Deploy all resources
117 ec.deploy()
118
119 # Wait until the ccncat is finished
120 ec.wait_finished([server])
121
122 print ec.trace(client, "VIDEO")
123
124 ec.shutdown()
125
126