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.LogComponentEnable(
"UdpEchoClientApplication", ns.LOG_LEVEL_INFO)
63 ns.LogComponentEnable(
"UdpEchoServerApplication", ns.LOG_LEVEL_INFO)
65p2pNodes = ns.NodeContainer()
68pointToPoint = ns.PointToPointHelper()
69pointToPoint.SetDeviceAttribute(
"DataRate", ns.StringValue(
"5Mbps"))
70pointToPoint.SetChannelAttribute(
"Delay", ns.StringValue(
"2ms"))
72p2pDevices = pointToPoint.Install(p2pNodes)
74csmaNodes = ns.NodeContainer()
75csmaNodes.Add(p2pNodes.Get(1))
76csmaNodes.Create(nCsma.value)
79csma.SetChannelAttribute(
"DataRate", ns.StringValue(
"100Mbps"))
80csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.NanoSeconds(6560)))
82csmaDevices = csma.Install(csmaNodes)
84wifiStaNodes = ns.NodeContainer()
85wifiStaNodes.Create(nWifi.value)
86wifiApNode = p2pNodes.Get(0)
88channel = ns.YansWifiChannelHelper.Default()
89phy = ns.YansWifiPhyHelper()
90phy.SetChannel(channel.Create())
92mac = ns.WifiMacHelper()
93ssid = ns.Ssid(
"ns-3-ssid")
97mac.SetType(
"ns3::StaWifiMac",
"Ssid", ns.SsidValue(ssid),
"ActiveProbing", ns.BooleanValue(
False))
98staDevices = wifi.Install(phy, mac, wifiStaNodes)
100mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.SsidValue(ssid))
101apDevices = wifi.Install(phy, mac, wifiApNode)
103mobility = ns.MobilityHelper()
104mobility.SetPositionAllocator(
105 "ns3::GridPositionAllocator",
113 ns.DoubleValue(10.0),
117 ns.StringValue(
"RowFirst"),
120mobility.SetMobilityModel(
121 "ns3::RandomWalk2dMobilityModel",
123 ns.RectangleValue(ns.Rectangle(-50, 50, -50, 50)),
125mobility.Install(wifiStaNodes)
127mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
128mobility.Install(wifiApNode)
130stack = ns.InternetStackHelper()
131stack.Install(csmaNodes)
132stack.Install(wifiApNode)
133stack.Install(wifiStaNodes)
135address = ns.Ipv4AddressHelper()
136address.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
137p2pInterfaces = address.Assign(p2pDevices)
139address.SetBase(ns.Ipv4Address(
"10.1.2.0"), ns.Ipv4Mask(
"255.255.255.0"))
140csmaInterfaces = address.Assign(csmaDevices)
142address.SetBase(ns.Ipv4Address(
"10.1.3.0"), ns.Ipv4Mask(
"255.255.255.0"))
143address.Assign(staDevices)
144address.Assign(apDevices)
146echoServer = ns.UdpEchoServerHelper(9)
148serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
149serverApps.Start(ns.Seconds(1.0))
150serverApps.Stop(ns.Seconds(10.0))
152echoClient = ns.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
153echoClient.SetAttribute(
"MaxPackets", ns.UintegerValue(1))
154echoClient.SetAttribute(
"Interval", ns.TimeValue(ns.Seconds(1.0)))
155echoClient.SetAttribute(
"PacketSize", ns.UintegerValue(1024))
157clientApps = echoClient.Install(wifiStaNodes.Get(nWifi.value - 1))
158clientApps.Start(ns.Seconds(2.0))
159clientApps.Stop(ns.Seconds(10.0))
161ns.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
163ns.Simulator.Stop(ns.Seconds(10.0))
166 phy.SetPcapDataLinkType(phy.DLT_IEEE802_11_RADIO)
167 pointToPoint.EnablePcapAll(
"third")
168 phy.EnablePcap(
"third", apDevices.Get(0))
169 csma.EnablePcap(
"third", csmaDevices.Get(0),
True)
172ns.Simulator.Destroy()