use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / dce / custom_local_wifi_ping.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 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: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from __future__ import print_function
21
22 from nepi.execution.ec import ExperimentController 
23
24 def add_ns3_node(ec, simu):
25     node = ec.register_resource("ns3::Node")
26     ec.register_connection(node, simu)
27
28     ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
29     ec.register_connection(node, ipv4)
30
31     arp = ec.register_resource("ns3::ArpL3Protocol")
32     ec.register_connection(node, arp)
33     
34     icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
35     ec.register_connection(node, icmp)
36
37     udp = ec.register_resource("ns3::UdpL4Protocol")
38     ec.register_connection(node, udp)
39
40     tcp = ec.register_resource("ns3::TcpL4Protocol")
41     ec.register_connection(node, tcp)
42
43     return node
44
45 def add_device(ec, node, ip, prefix, access_point = False):
46     dev = ec.register_resource("ns3::WifiNetDevice")
47     ec.set(dev, "ip", ip)
48     ec.set(dev, "prefix", prefix)
49     ec.register_connection(node, dev)
50
51     phy = ec.register_resource("ns3::YansWifiPhy")
52     ec.set(phy, "Standard", "WIFI_PHY_STANDARD_80211a")
53     ec.register_connection(dev, phy)
54
55     error = ec.register_resource("ns3::NistErrorRateModel")
56     ec.register_connection(phy, error)
57
58     manager = ec.register_resource("ns3::ArfWifiManager")
59     ec.register_connection(dev, manager)
60
61     if access_point:
62         mac = ec.register_resource("ns3::ApWifiMac")
63     else:
64         mac = ec.register_resource("ns3::StaWifiMac")
65
66     ec.set(mac, "Standard", "WIFI_PHY_STANDARD_80211a")
67     ec.register_connection(dev, mac)
68
69     return dev, phy
70
71 def add_constant_mobility(ec, node, x, y, z):
72     mobility = ec.register_resource("ns3::ConstantPositionMobilityModel") 
73     position = "%d:%d:%d" % (x, y, z)
74     ec.set(mobility, "Position", position)
75     ec.register_connection(node, mobility)
76     return mobility
77
78 def add_wifi_channel(ec):
79     channel = ec.register_resource("ns3::YansWifiChannel")
80     delay = ec.register_resource("ns3::ConstantSpeedPropagationDelayModel")
81     ec.register_connection(channel, delay)
82
83     loss  = ec.register_resource("ns3::LogDistancePropagationLossModel")
84     ec.register_connection(channel, loss)
85
86     return channel
87
88 ec = ExperimentController(exp_id = "dce-custom-wifi-ping")
89
90 node = ec.register_resource("linux::Node")
91 ec.set(node, "hostname", "localhost")
92 ec.set(node, "cleanProcesses", True)
93
94 simu = ec.register_resource("linux::ns3::Simulation")
95 ec.set(simu, "verbose", True)
96 ec.register_connection(simu, node)
97
98 nsnode1 = add_ns3_node(ec, simu)
99 add_constant_mobility(ec, nsnode1, 0, 0, 0)
100 dev1, phy1 = add_device(ec, nsnode1, "10.0.0.1", "30")
101
102 nsnode2 = add_ns3_node(ec, simu)
103 add_constant_mobility(ec, nsnode2, 50, 50, 0)
104 dev2, phy2 = add_device(ec, nsnode2, "10.0.0.2", "30", access_point = True)
105
106 # Create channel
107 chan = add_wifi_channel(ec)
108 ec.register_connection(chan, phy1)
109 ec.register_connection(chan, phy2)
110
111 ### create applications
112 ping = ec.register_resource("linux::ns3::dce::Application")
113 ec.set (ping, "sources", "http://www.skbuff.net/iputils/iputils-s20101006.tar.bz2")
114 ec.set (ping, "build", "tar xvjf ${SRC}/iputils-s20101006.tar.bz2 && "
115         "cd iputils-s20101006/ && "
116         "sed -i 's/CFLAGS=/CFLAGS+=/g' Makefile && "
117         "make CFLAGS=-fPIC LDFLAGS='-pie -rdynamic' ping && "
118         "cp ping ${BIN_DCE} && cd - ")
119 ec.set (ping, "binary", "ping")
120 ec.set (ping, "stackSize", 1<<20)
121 ec.set (ping, "arguments", "-c 10;-s 1000;10.0.0.2")
122 ec.set (ping, "StartTime", "1s")
123 ec.set (ping, "StopTime", "20s")
124 ec.register_connection(ping, nsnode1)
125
126 ec.deploy()
127
128 ec.wait_finished([ping])
129
130 stdout = ec.trace(ping, "stdout") 
131
132 ec.shutdown()
133
134 print("PING OUTPUT", stdout)