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