OMF examples
[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 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 : Nitos
30 #
31 #     Node
32 #     node0XX
33 #     VLC client
34 #     0
35 #     |
36 #     |
37 #     0
38 #     Node
39 #     node0ZZ 
40 #     VLC server
41 #     
42 #   
43 #      - Experiment:
44 #        - t0 : Deployment
45 #        - t1 : Ping Start
46 #        - t2 (t1 + 10s) : Ping stop
47 #        - t3 (t2 + 2s) : Kill the application
48 #
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="Nitos first reserved node "
61             "(e.g. hostname must be of form: node0XX)", 
62         type="str")
63 parser.add_option("-z", "--nodez", dest="nodez", 
64         help="Nitos second reserved node "
65             "(e.g. hostname must be of form: node0ZZ)", 
66         type="str")
67 parser.add_option("-c", "--channel", dest="channel", 
68         help="Nitos reserved channel",
69         type="str")
70 parser.add_option("-s", "--slice-name", dest="slicename", 
71         help="Nitos slice name", type="str")
72 (options, args) = parser.parse_args()
73
74 nodex = options.nodex
75 nodez = options.nodez
76 slicename = options.slicename
77 chan = options.channel
78
79 # Create the EC
80 ec = ExperimentController(exp_id="nitos_omf6_vlc")
81
82 # Create and Configure the Nodes
83 node1 = ec.register_resource("OMFNode")
84 ec.set(node1, "hostname", nodex)
85 ec.set(node1, "xmppUser", slicename)
86 ec.set(node1, "xmppServer", "nitlab.inf.uth.gr")
87 ec.set(node1, "xmppPort", "5222")
88 ec.set(node1, "xmppPassword", "1234")
89
90 # Create and Configure the Interfaces
91 iface1 = ec.register_resource("OMFWifiInterface")
92 ec.set(iface1, "name", "wlan0")
93 ec.set(iface1, "mode", "adhoc")
94 ec.set(iface1, "hw_mode", "g")
95 ec.set(iface1, "essid", "ping")
96 ec.set(iface1, "ip", "192.168.0.%s/24" % nodex[-2:]) 
97 ec.register_connection(node1, iface1)
98
99 # Create and Configure the Nodes
100 node2 = ec.register_resource("OMFNode")
101 ec.set(node2, "hostname", nodez)
102 ec.set(node2, "xmppUser", slicename)
103 ec.set(node2, "xmppServer", "nitlab.inf.uth.gr")
104 ec.set(node2, "xmppPort", "5222")
105 ec.set(node2, "xmppPassword", "1234")
106
107 # Create and Configure the Interfaces
108 iface2 = ec.register_resource("OMFWifiInterface")
109 ec.set(iface2, "name", "wlan0")
110 ec.set(iface2, "mode", "adhoc")
111 ec.set(iface2, "hw_mode", "g")
112 ec.set(iface2, "essid", "ping")
113 ec.set(iface2, "ip", "192.168.0.%s/24" % nodez[-2:]) 
114 ec.register_connection(node2, iface2)
115
116 # Create and Configure the Channel
117 channel = ec.register_resource("OMFChannel")
118 ec.set(channel, "channel", chan)
119 ec.set(channel, "xmppUser", slicename)
120 ec.set(channel, "xmppServer", "nitlab.inf.uth.gr")
121 ec.set(channel, "xmppPort", "5222")
122 ec.set(channel, "xmppPassword", "1234")
123 ec.register_connection(iface1, channel)
124 ec.register_connection(iface2, channel)
125
126 client_ip = "192.168.0.%s" % nodez[-2:]
127
128 # Create and Configure the Application
129 app1 = ec.register_resource("OMFApplication")
130 ec.set(app1, "command", 
131     "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) 
132 ec.register_connection(app1, node1)
133
134 ## Add a OMFApplication to run the client VLC and count the numer of bytes 
135 ## transmitted,  using wc.
136 app2 = ec.register_resource("OMFApplication")
137 ec.set(app2, "command", 
138     "DISPLAY=localhost:10.0 XAUTHORITY=/root/.Xauthority /root/vlc/vlc-1.1.13/cvlc rtp://%s:5004 | wc -c "% client_ip)
139
140 ## Alternativelly, you can try to send the video to standard output and 
141 ## recover it using the stdout trace. However, it seems that sending 
142 ## binary messages back to the client is not well supported by the OMF 6 RC
143 #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)
144 ec.register_connection(app2, node2)
145
146 ## stop app1 65s after it started
147 ec.register_condition(app1, ResourceAction.STOP, app1, ResourceState.STARTED , "65s")
148 ## start app2 5s after app1
149 ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "5s")
150 ## stop app2 5 seconds after app2
151 ec.register_condition(app2, ResourceAction.STOP, app1, ResourceState.STOPPED , "5s")
152
153 # Deploy
154 ec.deploy()
155
156 ec.wait_finished([app2])
157
158 # Retrieve the bytes transmitted count and print it
159 byte_count = ec.trace(app2, "stdout")
160 print "BYTES transmitted", byte_count
161
162 ## If you redirected the video to standard output, you can try to 
163 ## retrieve the stdout of the VLC client
164 ## video = ec.trace(app2, "stdout")
165 #f = open("video.ts", "w")
166 #f.write(video)
167 #f.close()
168
169 # Stop Experiment
170 ec.shutdown()
171