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