use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / ns3 / local_mobile_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 """ 
23 network topology:
24  
25 Initial positions. AP is fixed.
26
27     +
28 100m| (30,100)  (60,100)  (90,100)
29     |    n1       n2       n3
30  90m|
31     |
32     |
33  60m|  (30,60)  (60,60)   (90,60)
34     |    n4       n5       n6
35     |
36  30m|             
37     |            (60,0)
38     |             n7 (AP)
39     +----------------------------+
40     0     30       60       90  100m
41
42 """
43
44 from nepi.execution.ec import ExperimentController 
45
46 def add_node(ec, simu):
47     ## Add a ns-3 node with its protocol stack
48     nsnode = ec.register_resource("ns3::Node")
49     ec.register_connection(nsnode, simu)
50
51     ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
52     ec.register_connection(nsnode, ipv4)
53     arp = ec.register_resource("ns3::ArpL3Protocol")
54     ec.register_connection(nsnode, arp)
55     icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
56     ec.register_connection(nsnode, icmp)
57     return nsnode
58
59 def add_wifi_device(ec, node, ip, prefix, access_point = False):
60     dev = ec.register_resource("ns3::WifiNetDevice")
61     ec.set(dev, "ip", ip)
62     ec.set(dev, "prefix", prefix)
63     ec.register_connection(node, dev)
64
65     phy = ec.register_resource("ns3::YansWifiPhy")
66     ec.set(phy, "Standard", "WIFI_PHY_STANDARD_80211a")
67     ec.register_connection(dev, phy)
68
69     error = ec.register_resource("ns3::NistErrorRateModel")
70     ec.register_connection(phy, error)
71
72     manager = ec.register_resource("ns3::ArfWifiManager")
73     ec.register_connection(dev, manager)
74
75     if access_point:
76         mac = ec.register_resource("ns3::ApWifiMac")
77     else:
78         mac = ec.register_resource("ns3::StaWifiMac")
79
80     ec.set(mac, "Standard", "WIFI_PHY_STANDARD_80211a")
81     ec.register_connection(dev, mac)
82
83     return dev, phy
84
85 def add_random_mobility(ec, node, x, y, z, speed, bounds_width, 
86         bounds_height):
87     position = "%d:%d:%d" % (x, y, z)
88     bounds = "0|%d|0|%d" % (bounds_width, bounds_height) 
89     speed = "ns3::UniformRandomVariable[Min=%d|Max=%s]" % (speed, speed)
90     pause = "ns3::ConstantRandomVariable[Constant=1.0]"
91     
92     mobility = ec.register_resource("ns3::RandomDirection2dMobilityModel")
93     ec.set(mobility, "Position", position)
94     ec.set(mobility, "Bounds", bounds)
95     ec.set(mobility, "Speed", speed)
96     ec.set(mobility, "Pause",  pause)
97     ec.register_connection(node, mobility)
98     return mobility
99
100 def add_constant_mobility(ec, node, x, y, z):
101     mobility = ec.register_resource("ns3::ConstantPositionMobilityModel") 
102     position = "%d:%d:%d" % (x, y, z)
103     ec.set(mobility, "Position", position)
104     ec.register_connection(node, mobility)
105     return mobility
106
107 def add_wifi_channel(ec):
108     channel = ec.register_resource("ns3::YansWifiChannel")
109     delay = ec.register_resource("ns3::ConstantSpeedPropagationDelayModel")
110     ec.register_connection(channel, delay)
111
112     loss  = ec.register_resource("ns3::LogDistancePropagationLossModel")
113     ec.register_connection(channel, loss)
114
115     return channel
116
117 bounds_width = bounds_height = 100
118 speed = 1
119
120 ec = ExperimentController(exp_id = "ns3-wifi-ping")
121
122 # Simulation will run in a remote machine
123 node = ec.register_resource("linux::Node")
124 ec.set(node, "hostname", "localhost")
125
126 # Add a simulation resource
127 simu = ec.register_resource("linux::ns3::Simulation")
128 ec.set(simu, "verbose", True)
129 ec.set(simu, "enableDump", True)
130 ec.set (simu, "StopTime", "22s")
131 ec.register_connection(simu, node)
132
133 x = 30
134 y = 100
135 nsnode1 = add_node(ec, simu)
136 dev1, phy1 = add_wifi_device(ec, nsnode1, "10.0.0.1", "24", access_point = False)
137 mobility1 = add_random_mobility(ec, nsnode1, x, y, 0, speed, bounds_width, bounds_height)
138
139 x = 60
140 y = 100
141 nsnode2 = add_node(ec, simu)
142 dev2, phy2 = add_wifi_device(ec, nsnode2, "10.0.0.2", "24", access_point = False)
143 mobility2 = add_random_mobility(ec, nsnode2, x, y, 0, speed, bounds_width, bounds_height)
144
145 x = 90
146 y = 100
147 nsnode3 = add_node(ec, simu)
148 dev3, phy3 = add_wifi_device(ec, nsnode3, "10.0.0.3", "24", access_point = False)
149 mobility3 = add_random_mobility(ec, nsnode3, x, y, 0, speed, bounds_width, bounds_height)
150
151 x = 30
152 y = 60
153 nsnode4 = add_node(ec, simu)
154 dev4, phy4 = add_wifi_device(ec, nsnode4, "10.0.0.4", "24", access_point = False)
155 mobility4 = add_random_mobility(ec, nsnode4, x, y, 0, speed, bounds_width, bounds_height)
156
157 x = 60
158 y = 60
159 nsnode5 = add_node(ec, simu)
160 dev5, phy5 = add_wifi_device(ec, nsnode5, "10.0.0.5", "24", access_point = False)
161 mobility5 = add_random_mobility(ec, nsnode5, x, y, 0, speed, bounds_width, bounds_height)
162
163 x = 90
164 y = 60
165 nsnode6 = add_node(ec, simu)
166 dev6, phy6 = add_wifi_device(ec, nsnode6, "10.0.0.6", "24", access_point = False)
167 mobility6 = add_random_mobility(ec, nsnode6, x, y, 0, speed, bounds_width, bounds_height)
168
169 x = 60
170 y = 0
171 nsnode7 = add_node(ec, simu)
172 dev7, phy7 = add_wifi_device(ec, nsnode7, "10.0.0.7", "24", access_point = True)
173 mobility7 = add_constant_mobility(ec, nsnode7, x, y, 0)
174
175 # Create channel
176 chan = add_wifi_channel(ec)
177 ec.register_connection(chan, phy1)
178 ec.register_connection(chan, phy2)
179 ec.register_connection(chan, phy3)
180 ec.register_connection(chan, phy4)
181 ec.register_connection(chan, phy5)
182 ec.register_connection(chan, phy6)
183 ec.register_connection(chan, phy7)
184
185 ### create pinger
186 ping = ec.register_resource("ns3::V4Ping")
187 ec.set (ping, "Remote", "10.0.0.6")
188 ec.set (ping, "Interval", "1s")
189 ec.set (ping, "Verbose", True)
190 ec.set (ping, "StartTime", "1s")
191 ec.set (ping, "StopTime", "21s")
192 ec.register_connection(ping, nsnode1)
193
194 ec.deploy()
195
196 ec.wait_finished([ping])
197
198 stdout = ec.trace(simu, "stdout") 
199
200 ec.shutdown()
201
202 print("PING OUTPUT", stdout)
203