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