use print() - import print_function - should be fine for both py2 and py3
[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 __future__ import print_function
49
50 from nepi.execution.resource import ResourceAction, ResourceState
51 from nepi.execution.ec import ExperimentController
52
53 from optparse import OptionParser
54 import os
55
56 usage = ("usage: %prog -x <nodex> -z <nodez> -s <slice-name> -c <channel>")
57
58 parser = OptionParser(usage = usage)
59 parser.add_option("-x", "--nodex", dest="nodex", 
60         help="w-iLab.t first reserved node "
61             "(must be of form:  "
62             " nodex.<experiment_id>.<project_id>.wilab2.ilabt.iminds.be"
63             " all letters in lowercase )", 
64         type="str")
65 parser.add_option("-z", "--nodez", dest="nodez", 
66         help="w-iLab.t first reserved node "
67              "(must be of form:  "
68             " nodex.<experiment_id>.<project_id>.wilab2.ilabt.iminds.be"
69             " all letters in lowercase )", 
70         type="str")
71 parser.add_option("-c", "--channel", dest="channel", 
72         help="Nitos reserved channel",
73         type="str")
74 parser.add_option("-s", "--slice-name", dest="slicename", 
75         help="Nitos slice name", type="str")
76 (options, args) = parser.parse_args()
77
78 nodex = options.nodex
79 nodez = options.nodez
80 slicename = options.slicename
81 chan = options.channel
82
83 # Create the EC
84 ec = ExperimentController(exp_id="iminds_omf6_ping")
85
86 # Create and Configure the Nodes
87
88 node1 = ec.register_resource("omf::Node")
89 ec.set(node1, "hostname", nodex)
90 ec.set(node1, "xmppUser", slicename)
91 ec.set(node1, "xmppServer", "xmpp.ilabt.iminds.be")
92 ec.set(node1, "xmppPort", "5222")
93 ec.set(node1, "xmppPassword", "1234")
94
95 iface1 = ec.register_resource("omf::WifiInterface")
96 ec.set(iface1, "name", "wlan0")
97 ec.set(iface1, "mode", "adhoc")
98 ec.set(iface1, "hw_mode", "g")
99 ec.set(iface1, "essid", "vlc")
100 ec.set(iface1, "ip", "192.168.0.1/24")
101 ec.register_connection(iface1, node1)
102
103 node2 = ec.register_resource("omf::Node")
104 ec.set(node2, "hostname", nodez)
105 ec.set(node2, "xmppUser", slicename)
106 ec.set(node2, "xmppServer", "xmpp.ilabt.iminds.be")
107 ec.set(node2, "xmppPort", "5222")
108 ec.set(node2, "xmppPassword", "1234")
109
110 iface2 = ec.register_resource("omf::WifiInterface")
111 ec.set(iface2, "name", "wlan0")
112 ec.set(iface2, "mode", "adhoc")
113 ec.set(iface2, "hw_mode", "g")
114 ec.set(iface2, "essid", "vlc")
115 ec.set(iface2, "ip", "192.168.0.2/24")
116 ec.register_connection(iface2, node2)
117
118 channel = ec.register_resource("omf::Channel")
119 ec.set(channel, "channel", "6")
120 ec.register_connection(iface1, channel)
121 ec.register_connection(iface2, channel)
122
123 client_ip = "192.168.0.2" 
124
125 # Create and Configure the Application
126 app1 = ec.register_resource("omf::Application")
127 ec.set(app1, "command", 
128     "/root/vlc/vlc-1.1.13/cvlc /root/10-by-p0d.avi --sout '#rtp{dst=%s,port=5004,mux=ts}'" % client_ip) 
129 ec.register_connection(app1, node1)
130
131 ## Add a OMFApplication to run the client VLC
132 app2 = ec.register_resource("omf::Application")
133 ## Send the transmitted video to a file.
134 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)
135 ec.register_connection(app2, node2)
136
137 ## Add a OMFApplication to count the number of bytes in the transmitted video
138 app3 = ec.register_resource("omf::Application")
139 ## Send the transmitted video to a file.
140 ec.set(app3, "command", "ls -lah /root/video.ts")
141 ec.register_connection(app3, node2)
142
143 app4 = ec.register_resource("omf::Application")
144 ec.set(app4, "command", "/usr/bin/killall vlc_app")
145 ec.register_connection(app4, node1)
146
147 app5 = ec.register_resource("omf::Application")
148 ec.set(app5, "command", "/usr/bin/killall vlc_app")
149 ec.register_connection(app5, node2)
150
151 ## start app2 5s after app1
152 ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "5s")
153 # start app3 after app2 stopped
154 ec.register_condition(app3, ResourceAction.START, app2, ResourceState.STOPPED , "5s")
155 # start the kill of vlc processes after they stopped
156 ec.register_condition(app4, ResourceAction.START, app1, ResourceState.STOPPED , "5s")
157 ec.register_condition(app5, ResourceAction.START, app2, ResourceState.STOPPED , "5s")
158
159 ## We need to explicitly STOP all applications
160 ## stop app1 65s after it started
161 ec.register_condition(app1, ResourceAction.STOP, app1, ResourceState.STARTED , "65s")
162 ## stop app2 5 seconds after app2
163 ec.register_condition(app2, ResourceAction.STOP, app1, ResourceState.STOPPED , "5s")
164 # stop app3 after 5s
165 ec.register_condition(app3, ResourceAction.STOP, app3, ResourceState.STOPPED , "5s")
166 # stop app4 
167 ec.register_condition(app4, ResourceAction.STOP, app4, ResourceState.STARTED , "5s")
168 # stop app5 
169 ec.register_condition(app5, ResourceAction.STOP, app5, ResourceState.STARTED , "5s")
170
171 # Deploy
172 ec.deploy()
173
174 # DO NOT WAIT FOR THE VLC applications or it will never stop
175 ec.wait_finished([app4, app5])
176
177 # Retrieve the bytes transmitted output and print it
178 byte_count = ec.trace(app3, "stdout")
179 print("BYTES transmitted", byte_count)
180
181 # Stop Experiment
182 ec.shutdown()
183