use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / ns3 / multi_host / hybrid.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2015 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 version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Damien Saucez <damien.saucez@inria.fr>
19 #         Alina Quereilhac <alina.quereilhac@inria.fr>
20 #
21
22 #
23 # Note that to run this experiment you need to have a PlanetLab account.
24 #
25 # This experiment consists of a simulated wireless sensor network (ns-3)
26 # with one fixed access point (AP), running an agent application, and several
27 # mobile stations that run a transmitter application to send messages to
28 # the AP.
29 #
30 # One of the transmitter applications runs outside the simulation, on
31 # the host, and sends messages to the AP through the FdNetDevice/TAP
32 # link.
33 #
34
35 #
36 # command line:
37 #
38 # PYTHONPATH=$PYTHONPATH:src python examples/ns3/multi_host/hybrid.py
39 #
40
41 from __future__ import print_function
42
43 import os
44
45 from nepi.execution.ec import ExperimentController
46 from nepi.execution.resource import ResourceState, ResourceManager
47
48 from topology import *
49
50 # tunning
51 os.environ["NEPI_NTHREADS"] = "1"
52 ResourceManager._reschedule_delay = "0s"
53
54 # list of hosts for running the experiment on
55 hostname1 = "onelab4.warsaw.rd.tp.pl"
56 hostname2 = "planet2.servers.ua.pt"
57
58 (username, pl_user, pl_password, ssh_key, node_count) = get_options()
59
60 ec = ExperimentController(exp_id="hybrid")
61
62 host, simu = add_host_simu(ec, hostname1, username, pl_user, pl_password, 
63         ssh_key)
64
65 ap, agent = build_ns3_topology(ec, simu, node_count, network="192.168.3.0", 
66         prefixlen="25", agent_ip="192.168.3.1")
67
68 fddev = add_fdnet_device(ec, ap, "192.168.3.129", "25")
69
70 tap = ec.register_resource("planetlab::Tap")
71 ec.set(tap, "ip", "192.168.3.130")
72 ec.set(tap, "prefix", "25")
73 ec.set(tap, "pointopoint", "192.168.3.129")
74 ec.register_connection(host, tap)  
75
76 connect_with_virtual_link(ec, tap, fddev)
77
78 add_ns3_route(ec, ap, network="192.168.3.128", prefixlen="25", nexthop="192.168.3.1")
79 add_planetlab_route(ec, tap, network="192.168.3.0", prefixlen="25", nexthop="192.168.3.129")
80
81 transmitter = ec.register_resource("linux::Application")
82 ec.set(transmitter, "sources", "code/transmitter.c")
83 ec.set(transmitter, "build", "gcc ${SRC}/transmitter.c -o ${BIN}/transmitter")
84 ec.set(transmitter, "command", "${BIN}/transmitter 192.168.3.1")
85 ec.register_connection(transmitter, host)
86
87 ec.deploy()
88
89 ec.wait_finished([simu, transmitter])
90
91 stdout = ec.trace(agent, "stdout")
92 print(" Agent says: ")
93 print(stdout)
94
95 stdout = ec.trace(transmitter, "stdout")
96 print(" Live transmitter output: ")
97 print(stdout)
98
99 ec.shutdown()
100
101