20except ModuleNotFoundError:
22 "Error: ns3 Python module not found;"
23 " Python bindings may not be enabled"
24 " or your PYTHONPATH might not be properly configured"
27from ctypes
import c_bool, c_int
44tracing = c_bool(
False)
46cmd = ns.CommandLine(__file__)
47cmd.AddValue(
"nCsma",
"Number of extra CSMA nodes/devices", nCsma)
48cmd.AddValue(
"nWifi",
"Number of wifi STA devices", nWifi)
49cmd.AddValue(
"verbose",
"Tell echo applications to log if true", verbose)
50cmd.AddValue(
"tracing",
"Enable pcap tracing", tracing)
58 print(
"nWifi should be 18 or less; otherwise grid layout exceeds the bounding box")
62 ns.core.LogComponentEnable(
"UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
63 ns.core.LogComponentEnable(
"UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
65p2pNodes = ns.network.NodeContainer()
68pointToPoint = ns.point_to_point.PointToPointHelper()
69pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
70pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
72p2pDevices = pointToPoint.Install(p2pNodes)
74csmaNodes = ns.network.NodeContainer()
75csmaNodes.Add(p2pNodes.Get(1))
76csmaNodes.Create(nCsma.value)
78csma = ns.csma.CsmaHelper()
79csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
80csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
82csmaDevices = csma.Install(csmaNodes)
84wifiStaNodes = ns.network.NodeContainer()
85wifiStaNodes.Create(nWifi.value)
86wifiApNode = p2pNodes.Get(0)
88channel = ns.wifi.YansWifiChannelHelper.Default()
89phy = ns.wifi.YansWifiPhyHelper()
90phy.SetChannel(channel.Create())
92mac = ns.wifi.WifiMacHelper()
93ssid = ns.wifi.Ssid(
"ns-3-ssid")
95wifi = ns.wifi.WifiHelper()
98 "ns3::StaWifiMac",
"Ssid", ns.wifi.SsidValue(ssid),
"ActiveProbing", ns.core.BooleanValue(
False)
100staDevices = wifi.Install(phy, mac, wifiStaNodes)
102mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.wifi.SsidValue(ssid))
103apDevices = wifi.Install(phy, mac, wifiApNode)
105mobility = ns.mobility.MobilityHelper()
106mobility.SetPositionAllocator(
107 "ns3::GridPositionAllocator",
109 ns.core.DoubleValue(0.0),
111 ns.core.DoubleValue(0.0),
113 ns.core.DoubleValue(5.0),
115 ns.core.DoubleValue(10.0),
117 ns.core.UintegerValue(3),
119 ns.core.StringValue(
"RowFirst"),
122mobility.SetMobilityModel(
123 "ns3::RandomWalk2dMobilityModel",
125 ns.mobility.RectangleValue(ns.mobility.Rectangle(-50, 50, -50, 50)),
127mobility.Install(wifiStaNodes)
129mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
130mobility.Install(wifiApNode)
132stack = ns.internet.InternetStackHelper()
133stack.Install(csmaNodes)
134stack.Install(wifiApNode)
135stack.Install(wifiStaNodes)
137address = ns.internet.Ipv4AddressHelper()
138address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
139p2pInterfaces = address.Assign(p2pDevices)
141address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
142csmaInterfaces = address.Assign(csmaDevices)
144address.SetBase(ns.network.Ipv4Address(
"10.1.3.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
145address.Assign(staDevices)
146address.Assign(apDevices)
148echoServer = ns.applications.UdpEchoServerHelper(9)
150serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
151serverApps.Start(ns.core.Seconds(1.0))
152serverApps.Stop(ns.core.Seconds(10.0))
154echoClient = ns.applications.UdpEchoClientHelper(
155 csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9
157echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
158echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
159echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
161clientApps = echoClient.Install(wifiStaNodes.Get(nWifi.value - 1))
162clientApps.Start(ns.core.Seconds(2.0))
163clientApps.Stop(ns.core.Seconds(10.0))
165ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
167ns.core.Simulator.Stop(ns.core.Seconds(10.0))
170 phy.SetPcapDataLinkType(phy.DLT_IEEE802_11_RADIO)
171 pointToPoint.EnablePcapAll(
"third")
172 phy.EnablePcap(
"third", apDevices.Get(0))
173 csma.EnablePcap(
"third", csmaDevices.Get(0),
True)
175ns.core.Simulator.Run()
176ns.core.Simulator.Destroy()