b246c9bf96a9af5a74a4aba3aa3f88a6d706011a
[nepi.git] / examples / linux / ccn / vlc_extended_ring_topo.py
1 #!/usr/bin/env python
2
3 #
4 #    NEPI, a framework to manage network experiments
5 #    Copyright (C) 2013 INRIA
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21
22 #
23 # CCN topology:
24 #
25 #                h2
26 #                0 
27 #  content   l1 / \ l2         ccncat
28 #  b1          /l5 \           b2
29 #  0 ----- h1 0 --- 0 h3 ------ 0
30 #              \   / 
31 #            l4 \ / l3
32 #                0
33 #                h4
34 # Experiment:
35 # - t0 : b2 retrives video published in b1
36 # - t1 : l1 goes down
37 # - t2 : l2 goes down
38 # - t3 : l5 goes up
39 #
40
41 from nepi.execution.ec import ExperimentController, ECState 
42 from nepi.execution.resource import ResourceState, ResourceAction, \
43         populate_factory
44 from nepi.resources.linux.node import OSType
45
46 from optparse import OptionParser, SUPPRESS_HELP
47
48 import os
49 import time
50 import tempfile
51
52 def add_node(ec, host, user, ssh_key = None):
53     node = ec.register_resource("LinuxNode")
54     ec.set(node, "hostname", host)
55     ec.set(node, "username", user)
56     ec.set(node, "identity", ssh_key)
57     ec.set(node, "cleanHome", True)
58     ec.set(node, "cleanProcesses", True)
59     return node
60
61 def add_ccnd(ec, node):
62     ccnd = ec.register_resource("LinuxCCND")
63     ec.set(ccnd, "debug", 7)
64     ec.register_connection(ccnd, node)
65     return ccnd
66
67 def add_ccnr(ec, ccnd):
68     ccnr = ec.register_resource("LinuxCCNR")
69     ec.register_connection(ccnr, ccnd)
70     return ccnr
71
72 def add_fib_entry(ec, ccnd, peer_host):
73     entry = ec.register_resource("LinuxFIBEntry")
74     ec.set(entry, "host", peer_host)
75     ec.register_connection(entry, ccnd)
76     return entry
77
78 def add_content(ec, ccnr, content_name, content):
79     co = ec.register_resource("LinuxCCNContent")
80     ec.set(co, "contentName", content_name)
81     ec.set(co, "content", content)
82     ec.register_connection(co, ccnr)
83     return co
84
85 def add_stream(ec, ccnd, content_name):
86     command = "sudo -S dbus-uuidgen --ensure ; ccncat %s | vlc - " % \
87             content_name
88
89     app = ec.register_resource("LinuxCCNApplication")
90     ec.set(app, "depends", "vlc")
91     ec.set(app, "forwardX11", True)
92     ec.set(app, "command", command)
93     ec.register_connection(app, ccnd)
94
95     return app
96
97 if __name__ == '__main__':
98     # Search for available RMs
99     populate_factory()
100     
101     ec = ExperimentController(exp_id = "olahh")
102     
103     # hosts
104     host1 = "planetlab2.u-strasbg.fr"
105     host2 = "planet1.servers.ua.pt"
106     host3 = "planetlab1.cs.uoi.gr"
107     host4 = "planetlab1.aston.ac.uk"
108     host5 = "itchy.comlab.bth.se"
109     host6 = "roseval.pl.sophia.inria.fr"
110
111     # users
112     pluser = "inria_alina"
113     user = "alina"
114
115     content_name = "ccnx:/VIDEO"
116     video = "/home/alina/repos/nepi/examples/big_buck_bunny_240p_mpeg4_lq.ts"
117     """
118     # describe nodes in the central ring
119     ring_hosts = [host1, host2, host3, host4]
120     ccnds = dict()
121
122     for i in xrange(len(ring_hosts)):
123         host = ring_hosts[i]
124         node = add_node(ec, host, pluser)
125         ccnd = add_ccnd(ec, node)
126         ccnr = add_ccnr(ec, ccnd)
127         ccnds[host] = ccnd
128     
129     ## Add ccn ring links
130     # l1 : h1 - h2 , h2 - h1
131     l1u = add_fib_entry(ec, ccnds[host1], host2)
132     l1d = add_fib_entry(ec, ccnds[host2], host1)
133
134     # l2 : h2 - h3 , h3 - h2
135     l2u = add_fib_entry(ec, ccnds[host2], host3)
136     l2d = add_fib_entry(ec, ccnds[host3], host2)
137
138     # l3 : h3 - h4 , h4 - h3
139     l3u = add_fib_entry(ec, ccnds[host3], host4)
140     l3d = add_fib_entry(ec, ccnds[host4], host3)
141
142     # l4 : h4 - h1 , h1 - h4
143     l4u = add_fib_entry(ec, ccnds[host4], host1)
144     l4d = add_fib_entry(ec, ccnds[host1], host4)
145
146     # l5 : h1 - h3 , h3 - h1
147     l5u = add_fib_entry(ec, ccnds[host1], host3)
148     l5d = add_fib_entry(ec, ccnds[host3], host1)
149     """  
150     # border node 1
151     bnode1 = add_node(ec, host5, pluser)
152     ccndb1 = add_ccnd(ec, bnode1)
153     ccnrb1 = add_ccnr(ec, ccndb1)
154     co = add_content(ec, ccnrb1, content_name, video)
155
156     # border node 2
157     bnode2 = add_node(ec, host6, user)
158     ccndb2 = add_ccnd(ec, bnode2)
159     ccnrb2 = add_ccnr(ec, ccndb2)
160     app = add_stream(ec, ccndb2, content_name)
161  
162     # connect border nodes
163     #add_fib_entry(ec, ccndb1, host1)
164     #add_fib_entry(ec, ccnds[host1], host5)
165
166     #add_fib_entry(ec, ccndb2, host3)
167     #add_fib_entry(ec, ccnds[host3], host6)
168  
169     add_fib_entry(ec, ccndb2, host5)
170     add_fib_entry(ec, ccndb1, host6)
171  
172     # deploy all ResourceManagers
173     ec.deploy()
174
175     ec.wait_finished([app])
176     
177     """
178     proc2 = subprocess.Popen(['vlc', 
179         '--ffmpeg-threads=1',
180         '--sub-filter', 'marq', 
181         '--marq-marquee', 
182         '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org', 
183         '--marq-position=8', 
184         '--no-video-title-show', '-'], 
185         stdin=proc1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
186
187     dirpath = tempfile.mkdtemp()
188     """
189
190     # shutdown the experiment controller
191     ec.shutdown()
192