Removing +x from examples
[nepi.git] / examples / linux / file_transfer.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
20
21 from nepi.execution.ec import ExperimentController
22 from nepi.execution.resource import ResourceAction, ResourceState
23
24 def add_node(ec, host, user):
25     node = ec.register_resource("LinuxNode")
26     ec.set(node, "hostname", host)
27     ec.set(node, "username", user)
28     ec.set(node, "cleanHome", True)
29     ec.set(node, "cleanProcesses", True)
30     return node
31
32 def add_app(ec, command, node, sudo=None, video=None, depends=None, forward_x11=None, \
33         env=None):
34     app = ec.register_resource("LinuxApplication")
35     if sudo is not None:
36         ec.set(app, "sudo", sudo)
37     if video is not None:
38         ec.set(app, "sources", video)
39     if depends is not None:
40         ec.set(app, "depends", depends)
41     if forward_x11 is not None:
42         ec.set(app, "forwardX11", forward_x11)
43     if env is not None:
44         ec.set(app, "env", env)
45     ec.set(app, "command", command)
46     ec.register_connection(app, node)
47     return app
48
49 exp_id = "transfer_file"
50
51 # Create the EC
52 ec = ExperimentController(exp_id)
53
54 # PlanetLab choosen nodes for the experiment, change for PlanetLab nodes in your slice or
55 # other linux nodes
56 server_name = "planetlab2.ionio.gr"
57 client_name = "planetlab2.fri.uni-lj.si"
58
59 slicename = "inria_sfatest"
60
61 # Location of the video in local machine
62 video= "../big_buck_bunny_240p_mpeg4_lq.ts"
63
64 # Packets needed for running the experiment
65 depends_server = "pv nc tcpdump"
66 depends_client = "nc"
67
68 # Add resource managers for the linux nodes
69 server = add_node(ec, server_name, slicename)
70 client = add_node(ec, client_name, slicename)
71
72 # Add resource managers for the linux applications
73 app_server =  add_app(ec, "cat ${SRC}/big_buck_bunny_240p_mpeg4_lq.ts | pv -fbt 2> \
74      bw.txt | nc %s 1234" % client_name, server, video=video, depends=depends_server)
75
76 # Note: is important to add the -d option in nc command to not attempt to read from the 
77 # stdin
78 # if not nc in the client side close the socket suddently if runned in background
79 app_client =  add_app(ec, "nc -dl 1234 > big_buck_copied_movie.ts", client, \
80     depends=depends_client)
81
82 capture = add_app(ec, "tcpdump -ni eth0 -w video_transfer.pcap -s0 port 1234 2>&1", \
83     server, sudo=True)
84
85 # Register conditions 1. nodes ; 2. start tcpdump capture ; 3. client listen port 1234 ;
86 # 4. server start sending video
87 ec.register_condition(app_server, ResourceAction.START, app_client, ResourceState.STARTED) 
88 ec.register_condition(app_client, ResourceAction.START, capture, ResourceState.STARTED)
89
90 # Deploy
91 ec.deploy()
92
93 # Wait until the applications are finish to retrive the traces
94 ec.wait_finished([app_server, app_client])
95
96 bw = ec.trace(app_server, "bw.txt")
97 pcap = ec.trace(capture, "video_transfer.pcap")
98
99 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
100 f = open("examples/linux/transfer/bw.txt", "w")
101 f.write(bw)
102 f.close()
103 f = open("examples/linux/transfer/video_transfer.pcap", "w")
104 f.write(pcap)
105 f.close()
106
107 ec.shutdown()
108