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