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