1c4d1ce67bae3b3dda79f35adbe7b7b09e9810a9
[nepi.git] / examples / omf / iminds_omf6_vlc.py
1 #!/usr/bin/env python
2
3 ###############################################################################
4 #
5 #    NEPI, a framework to manage network experiments
6 #    Copyright (C) 2013 INRIA
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License version 2 as
10 #    published by the Free Software Foundation;
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Authors: Alina Quereilhac <alina.quereilhac@inria.fr>
21 #         Julien Tribino <julien.tribino@inria.fr>
22 #
23 ###############################################################################
24       
25 # Topology
26 #
27 #
28 #  Testbed : iMinds
29 #
30 #     Node
31 #     node0ZZ 
32 #     0
33 #     |
34 #     |
35 #     0
36 #     Node
37 #     node0ZZ 
38 #     PING
39 #     
40 #   
41 #      - Experiment:
42 #        - t0 : Deployment
43 #        - t1 : Ping Start
44 #        - t2 (t1 + 10s) : Ping stop
45 #        - t3 (t2 + 2s) : Kill the application
46 #
47
48 from nepi.execution.resource import ResourceAction, ResourceState
49 from nepi.execution.ec import ExperimentController
50
51 from optparse import OptionParser
52 import os
53
54 usage = ("usage: %prog -x <nodex> -z <nodez> -s <slice-name> -c <channel>")
55
56 parser = OptionParser(usage = usage)
57 parser.add_option("-x", "--nodex", dest="nodex", 
58         help="w-iLab.t first reserved node "
59             "(must be of form:  "
60             " nodex.<experiment_id>.<project_id>.wilab2.ilabt.iminds.be"
61             " all letters in lowercase )", 
62         type="str")
63 parser.add_option("-z", "--nodez", dest="nodez", 
64         help="w-iLab.t first reserved node "
65              "(must be of form:  "
66             " nodex.<experiment_id>.<project_id>.wilab2.ilabt.iminds.be"
67             " all letters in lowercase )", 
68         type="str")
69 parser.add_option("-c", "--channel", dest="channel", 
70         help="Nitos reserved channel",
71         type="str")
72 parser.add_option("-s", "--slice-name", dest="slicename", 
73         help="Nitos slice name", type="str")
74 (options, args) = parser.parse_args()
75
76 nodex = options.nodex
77 nodez = options.nodez
78 slicename = options.slicename
79 chan = options.channel
80
81 # Create the EC
82 ec = ExperimentController(exp_id="iminds_omf6_ping")
83
84 # Create and Configure the Nodes
85
86 node1 = ec.register_resource("omf::Node")
87 ec.set(node1, "hostname", nodex)
88 ec.set(node1, "xmppUser", slicename)
89 ec.set(node1, "xmppServer", "xmpp.ilabt.iminds.be")
90 ec.set(node1, "xmppPort", "5222")
91 ec.set(node1, "xmppPassword", "1234")
92
93 iface1 = ec.register_resource("omf::WifiInterface")
94 ec.set(iface1, "name", "wlan0")
95 ec.set(iface1, "mode", "adhoc")
96 ec.set(iface1, "hw_mode", "g")
97 ec.set(iface1, "essid", "vlc")
98 ec.set(iface1, "ip", "192.168.0.1/24")
99 ec.register_connection(iface1, node1)
100
101 node2 = ec.register_resource("omf::Node")
102 ec.set(node2, "hostname", nodez)
103 ec.set(node2, "xmppUser", slicename)
104 ec.set(node2, "xmppServer", "xmpp.ilabt.iminds.be")
105 ec.set(node2, "xmppPort", "5222")
106 ec.set(node2, "xmppPassword", "1234")
107
108 iface2 = ec.register_resource("omf::WifiInterface")
109 ec.set(iface2, "name", "wlan0")
110 ec.set(iface2, "mode", "adhoc")
111 ec.set(iface2, "hw_mode", "g")
112 ec.set(iface2, "essid", "vlc")
113 ec.set(iface2, "ip", "192.168.0.2/24")
114 ec.register_connection(iface2, node2)
115
116 channel = ec.register_resource("omf::Channel")
117 ec.set(channel, "channel", "6")
118 ec.register_connection(iface1, channel)
119 ec.register_connection(iface2, channel)
120
121 client_ip = "192.168.0.2" 
122
123 # Create and Configure the Application
124 app1 = ec.register_resource("omf::Application")
125 ec.set(app1, "command", 
126     "/root/vlc/vlc-1.1.13/cvlc /root/10-by-p0d.avi --sout '#rtp{dst=%s,port=5004,mux=ts}'" % client_ip) 
127 ec.register_connection(app1, node1)
128
129 ## Add a OMFApplication to run the client VLC
130 app2 = ec.register_resource("omf::Application")
131 ## Send the transmitted video to a file.
132 ec.set(app2, "command", "/root/vlc/vlc-1.1.13/cvlc rtp://%s:5004 --sout '#standard{access=file,mux=ts,dst=/root/video.ts}'" % client_ip)
133 ec.register_connection(app2, node2)
134
135 ## Add a OMFApplication to count the number of bytes in the transmitted video
136 app3 = ec.register_resource("omf::Application")
137 ## Send the transmitted video to a file.
138 ec.set(app3, "command", "ls -lah /root/video.ts")
139 ec.register_connection(app3, node2)
140
141 app4 = ec.register_resource("omf::Application")
142 ec.set(app4, "command", "/usr/bin/killall vlc_app")
143 ec.register_connection(app4, node1)
144
145 app5 = ec.register_resource("omf::Application")
146 ec.set(app5, "command", "/usr/bin/killall vlc_app")
147 ec.register_connection(app5, node2)
148
149 ## start app2 5s after app1
150 ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "5s")
151 # start app3 after app2 stopped
152 ec.register_condition(app3, ResourceAction.START, app2, ResourceState.STOPPED , "5s")
153 # start the kill of vlc processes after they stopped
154 ec.register_condition(app4, ResourceAction.START, app1, ResourceState.STOPPED , "5s")
155 ec.register_condition(app5, ResourceAction.START, app2, ResourceState.STOPPED , "5s")
156
157 ## We need to explicitly STOP all applications
158 ## stop app1 65s after it started
159 ec.register_condition(app1, ResourceAction.STOP, app1, ResourceState.STARTED , "65s")
160 ## stop app2 5 seconds after app2
161 ec.register_condition(app2, ResourceAction.STOP, app1, ResourceState.STOPPED , "5s")
162 # stop app3 after 5s
163 ec.register_condition(app3, ResourceAction.STOP, app3, ResourceState.STOPPED , "5s")
164 # stop app4 
165 ec.register_condition(app4, ResourceAction.STOP, app4, ResourceState.STARTED , "5s")
166 # stop app5 
167 ec.register_condition(app5, ResourceAction.STOP, app5, ResourceState.STARTED , "5s")
168
169 # Deploy
170 ec.deploy()
171
172 # DO NOT WAIT FOR THE VLC applications or it will never stop
173 ec.wait_finished([app4, app5])
174
175 # Retrieve the bytes transmitted output and print it
176 byte_count = ec.trace(app3, "stdout")
177 print "BYTES transmitted", byte_count
178
179 # Stop Experiment
180 ec.shutdown()
181