44except ModuleNotFoundError:
46 "Error: ns3 Python module not found;"
47 " Python bindings may not be enabled"
48 " or your PYTHONPATH might not be properly configured"
67 from ctypes
import c_double, c_int
69 backboneNodes = c_int(10)
72 stopTime = c_double(20)
73 cmd = ns.CommandLine(__file__)
79 ns.Config.SetDefault(
"ns3::OnOffApplication::PacketSize", ns.StringValue(
"1472"))
80 ns.Config.SetDefault(
"ns3::OnOffApplication::DataRate", ns.StringValue(
"100kb/s"))
88 cmd.AddValue(
"backboneNodes",
"number of backbone nodes", backboneNodes)
89 cmd.AddValue(
"infraNodes",
"number of leaf nodes", infraNodes)
90 cmd.AddValue(
"lanNodes",
"number of LAN nodes", lanNodes)
91 cmd.AddValue[
"double"](
"stopTime",
"simulation stop time(seconds)", stopTime)
99 if stopTime.value < 10:
100 print(
"Use a simulation stop time >= 10 seconds")
112 backbone = ns.NodeContainer()
113 backbone.Create(backboneNodes.value)
118 wifi = ns.WifiHelper()
119 mac = ns.WifiMacHelper()
120 mac.SetType(
"ns3::AdhocWifiMac")
121 wifi.SetRemoteStationManager(
122 "ns3::ConstantRateWifiManager",
"DataMode", ns.StringValue(
"OfdmRate54Mbps")
124 wifiPhy = ns.YansWifiPhyHelper()
125 wifiPhy.SetPcapDataLinkType(wifiPhy.DLT_IEEE802_11_RADIO)
126 wifiChannel = ns.YansWifiChannelHelper.Default()
127 wifiPhy.SetChannel(wifiChannel.Create())
128 backboneDevices = wifi.Install(wifiPhy, mac, backbone)
132 print(
"Enabling OLSR routing on all backbone nodes")
133 internet = ns.InternetStackHelper()
134 olsr = ns.OlsrHelper()
135 internet.SetRoutingHelper(olsr)
137 internet.Install(backbone)
144 ipAddrs = ns.Ipv4AddressHelper()
145 ipAddrs.SetBase(ns.Ipv4Address(
"192.168.0.0"), ns.Ipv4Mask(
"255.255.255.0"))
146 ipAddrs.Assign(backboneDevices)
152 mobility = ns.MobilityHelper()
153 mobility.SetPositionAllocator(
154 "ns3::GridPositionAllocator",
156 ns.DoubleValue(20.0),
158 ns.DoubleValue(20.0),
160 ns.DoubleValue(20.0),
162 ns.DoubleValue(20.0),
166 ns.StringValue(
"RowFirst"),
168 mobility.SetMobilityModel(
169 "ns3::RandomDirection2dMobilityModel",
171 ns.RectangleValue(ns.Rectangle(-500, 500, -500, 500)),
173 ns.StringValue(
"ns3::ConstantRandomVariable[Constant=2]"),
175 ns.StringValue(
"ns3::ConstantRandomVariable[Constant=0.2]"),
177 mobility.Install(backbone)
187 ipAddrs.SetBase(ns.Ipv4Address(
"172.16.0.0"), ns.Ipv4Mask(
"255.255.255.0"))
189 for i
in range(backboneNodes.value):
190 print(
"Configuring local area network for backbone node ", i)
196 newLanNodes = ns.NodeContainer()
197 newLanNodes.Create(lanNodes.value - 1)
199 lan = ns.NodeContainer(ns.NodeContainer(backbone.Get(i)), newLanNodes)
204 csma = ns.CsmaHelper()
205 csma.SetChannelAttribute(
"DataRate", ns.DataRateValue(ns.DataRate(5000000)))
206 csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.MilliSeconds(2)))
207 lanDevices = csma.Install(lan)
211 internet.Install(newLanNodes)
216 ipAddrs.Assign(lanDevices)
226 mobilityLan = ns.MobilityHelper()
227 positionAlloc = ns.ListPositionAllocator()
228 for j
in range(newLanNodes.GetN()):
229 positionAlloc.Add(ns.Vector(0.0, (j * 10 + 10), 0.0))
231 mobilityLan.SetPositionAllocator(positionAlloc)
232 mobilityLan.PushReferenceMobilityModel(backbone.Get(i))
233 mobilityLan.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
234 mobilityLan.Install(newLanNodes)
244 ipAddrs.SetBase(ns.Ipv4Address(
"10.0.0.0"), ns.Ipv4Mask(
"255.255.255.0"))
246 for i
in range(backboneNodes.value):
247 print(
"Configuring wireless network for backbone node ", i)
253 stas = ns.NodeContainer()
254 stas.Create(infraNodes.value - 1)
256 infra = ns.NodeContainer(ns.NodeContainer(backbone.Get(i)), stas)
260 ssid = ns.Ssid(
"wifi-infra" + str(i))
261 wifiInfra = ns.WifiHelper()
262 wifiPhy.SetChannel(wifiChannel.Create())
263 macInfra = ns.WifiMacHelper()
264 macInfra.SetType(
"ns3::StaWifiMac",
"Ssid", ns.SsidValue(ssid))
267 staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
269 macInfra.SetType(
"ns3::ApWifiMac",
"Ssid", ns.SsidValue(ssid))
270 apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
272 infraDevices = ns.NetDeviceContainer(apDevices, staDevices)
276 internet.Install(stas)
281 ipAddrs.Assign(infraDevices)
290 subnetAlloc = ns.ListPositionAllocator()
293 tempRef.append(subnetAlloc)
299 for j
in range(infra.GetN()):
300 subnetAlloc.Add(ns.Vector(0.0, j, 0.0))
302 mobility.PushReferenceMobilityModel(backbone.Get(i))
303 mobility.SetPositionAllocator(subnetAlloc)
304 mobility.SetMobilityModel(
305 "ns3::RandomDirection2dMobilityModel",
307 ns.RectangleValue(ns.Rectangle(-10, 10, -10, 10)),
309 ns.StringValue(
"ns3::ConstantRandomVariable[Constant=3]"),
311 ns.StringValue(
"ns3::ConstantRandomVariable[Constant=0.4]"),
313 mobility.Install(stas)
323 print(
"Create Applications.")
326 appSource = ns.NodeList.GetNode(backboneNodes.value)
329 + backboneNodes.value * (lanNodes.value - 1)
330 + backboneNodes.value * (infraNodes.value - 1)
333 appSink = ns.NodeList.GetNode(lastNodeIndex)
337 Ipv4Address getIpv4AddressFromNode(Ptr<Node> node){
338 return node->GetObject<Ipv4>()->GetAddress(1,0).GetLocal();
343 remoteAddr = ns.cppyy.gbl.getIpv4AddressFromNode(appSink)
344 socketAddr = ns.InetSocketAddress(remoteAddr, port)
345 onoff = ns.OnOffHelper(
"ns3::UdpSocketFactory", socketAddr.ConvertTo())
346 apps = onoff.Install(ns.NodeContainer(appSource))
347 apps.Start(ns.Seconds(3))
348 apps.Stop(ns.Seconds(stopTime.value - 1))
351 sink = ns.PacketSinkHelper(
352 "ns3::UdpSocketFactory",
353 ns.InetSocketAddress(ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port)).ConvertTo(),
355 sinkContainer = ns.NodeContainer(appSink)
356 apps = sink.Install(sinkContainer)
357 apps.Start(ns.Seconds(3))
365 print(
"Configure Tracing.")
366 csma = ns.CsmaHelper()
370 ascii = ns.AsciiTraceHelper()
371 stream = ascii.CreateFileStream(
"mixed-wireless.tr")
372 wifiPhy.EnableAsciiAll(stream)
373 csma.EnableAsciiAll(stream)
374 internet.EnableAsciiIpv4All(stream)
377 csma.EnablePcapAll(
"mixed-wireless",
False)
379 wifiPhy.EnablePcap(
"mixed-wireless", backboneDevices)
380 wifiPhy.EnablePcap(
"mixed-wireless", appSink.GetId(), 0)
393 print(
"Run Simulation.")
394 ns.Simulator.Stop(ns.Seconds(stopTime.value))
396 ns.Simulator.Destroy()
399if __name__ ==
"__main__":