use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / omf / nitos_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 : Nitos
29 #
30 #     Node
31 #     node0XX
32 #     VLC client
33 #     0
34 #     |
35 #     |
36 #     0
37 #     Node
38 #     node0ZZ 
39 #     VLC server
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 __future__ import print_function
50
51 from nepi.execution.resource import ResourceAction, ResourceState
52 from nepi.execution.ec import ExperimentController
53
54 from optparse import OptionParser
55 import os
56
57 usage = ("usage: %prog -x <nodex> -z <nodez> -s <slice-name> -c <channel>")
58
59 parser = OptionParser(usage = usage)
60 parser.add_option("-x", "--nodex", dest="nodex", 
61         help="Nitos first reserved node "
62             "(e.g. hostname must be of form: node0XX)", 
63         type="str")
64 parser.add_option("-z", "--nodez", dest="nodez", 
65         help="Nitos second reserved node "
66             "(e.g. hostname must be of form: node0ZZ)", 
67         type="str")
68 parser.add_option("-c", "--channel", dest="channel", 
69         help="Nitos reserved channel",
70         type="str")
71 parser.add_option("-s", "--slice-name", dest="slicename", 
72         help="Nitos slice name", type="str")
73 (options, args) = parser.parse_args()
74
75 nodex = options.nodex
76 nodez = options.nodez
77 slicename = options.slicename
78 chan = options.channel
79
80 # Create the EC
81 ec = ExperimentController(exp_id="nitos_omf6_vlc")
82
83 # Create and Configure the Nodes
84 node1 = ec.register_resource("omf::Node")
85 ec.set(node1, "hostname", nodex)
86 ec.set(node1, "xmppUser", slicename)
87 ec.set(node1, "xmppServer", "nitlab.inf.uth.gr")
88 ec.set(node1, "xmppPort", "5222")
89 ec.set(node1, "xmppPassword", "1234")
90
91 # Create and Configure the Interfaces
92 iface1 = ec.register_resource("omf::WifiInterface")
93 ec.set(iface1, "name", "wlan0")
94 ec.set(iface1, "mode", "adhoc")
95 ec.set(iface1, "hw_mode", "g")
96 ec.set(iface1, "essid", "vlc")
97 ec.set(iface1, "ip", "192.168.0.%s/24" % nodex[-2:]) 
98 ec.register_connection(node1, iface1)
99
100 # Create and Configure the Nodes
101 node2 = ec.register_resource("omf::Node")
102 ec.set(node2, "hostname", nodez)
103 ec.set(node2, "xmppUser", slicename)
104 ec.set(node2, "xmppServer", "nitlab.inf.uth.gr")
105 ec.set(node2, "xmppPort", "5222")
106 ec.set(node2, "xmppPassword", "1234")
107
108 # Create and Configure the Interfaces
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.%s/24" % nodez[-2:]) 
115 ec.register_connection(node2, iface2)
116
117 # Create and Configure the Channel
118 channel = ec.register_resource("omf::Channel")
119 ec.set(channel, "channel", chan)
120 ec.set(channel, "xmppUser", slicename)
121 ec.set(channel, "xmppServer", "nitlab.inf.uth.gr")
122 ec.set(channel, "xmppPort", "5222")
123 ec.set(channel, "xmppPassword", "1234")
124 ec.register_connection(iface1, channel)
125 ec.register_connection(iface2, channel)
126
127 client_ip = "192.168.0.%s" % nodez[-2:]
128
129 # Create and Configure the Application
130 app1 = ec.register_resource("omf::Application")
131 ec.set(app1, "command", 
132     "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority /root/vlc/vlc-1.1.13/cvlc /root/10-by-p0d.avi --sout '#rtp{dst=%s,port=5004,mux=ts}'" % client_ip) 
133 ec.register_connection(app1, node1)
134
135 ## Add a OMFApplication to run the client VLC and count the numer of bytes 
136 ## transmitted,  using wc.
137 app2 = ec.register_resource("omf::Application")
138 ec.set(app2, "command", 
139     "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority /root/vlc/vlc-1.1.13/cvlc rtp://%s:5004 | wc -c "% client_ip)
140
141 ## Alternativelly, you can try to send the video to standard output and 
142 ## recover it using the stdout trace. However, it seems that sending 
143 ## binary messages back to the client is not well supported by the OMF 6 RC
144 #ec.set(app2, "command", "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority /root/vlc/vlc-1.1.13/cvlc rtp://%s:5004 --sout '#standard{access=file,mux=ts,dst=-}'" % client_ip)
145 ec.register_connection(app2, node2)
146
147 ## stop app1 65s after it started
148 ec.register_condition(app1, ResourceAction.STOP, app1, ResourceState.STARTED , "65s")
149 ## start app2 5s after app1
150 ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "5s")
151 ## stop app2 5 seconds after app2
152 ec.register_condition(app2, ResourceAction.STOP, app1, ResourceState.STOPPED , "5s")
153
154 # Deploy
155 ec.deploy()
156
157 ec.wait_finished([app2])
158
159 # Retrieve the bytes transmitted count and print it
160 byte_count = ec.trace(app2, "stdout")
161 print("BYTES transmitted", byte_count)
162
163 ## If you redirected the video to standard output, you can try to 
164 ## retrieve the stdout of the VLC client
165 ## video = ec.trace(app2, "stdout")
166 #f = open("video.ts", "w")
167 #f.write(video)
168 #f.close()
169
170 # Stop Experiment
171 ec.shutdown()
172