9except ModuleNotFoundError:
11 "Error: ns3 Python module not found;"
12 " Python bindings may not be enabled"
13 " or your PYTHONPATH might not be properly configured"
16from ctypes
import c_bool, c_int
33tracing = c_bool(
False)
35cmd = ns.CommandLine(__file__)
36cmd.AddValue(
"nCsma",
"Number of extra CSMA nodes/devices", nCsma)
37cmd.AddValue(
"nWifi",
"Number of wifi STA devices", nWifi)
38cmd.AddValue(
"verbose",
"Tell echo applications to log if true", verbose)
39cmd.AddValue(
"tracing",
"Enable pcap tracing", tracing)
47 print(
"nWifi should be 18 or less; otherwise grid layout exceeds the bounding box")
51 ns.LogComponentEnable(
"UdpEchoClientApplication", ns.LOG_LEVEL_INFO)
52 ns.LogComponentEnable(
"UdpEchoServerApplication", ns.LOG_LEVEL_INFO)
54p2pNodes = ns.NodeContainer()
57pointToPoint = ns.PointToPointHelper()
58pointToPoint.SetDeviceAttribute(
"DataRate", ns.StringValue(
"5Mbps"))
59pointToPoint.SetChannelAttribute(
"Delay", ns.StringValue(
"2ms"))
61p2pDevices = pointToPoint.Install(p2pNodes)
63csmaNodes = ns.NodeContainer()
64csmaNodes.Add(p2pNodes.Get(1))
65csmaNodes.Create(nCsma.value)
68csma.SetChannelAttribute(
"DataRate", ns.StringValue(
"100Mbps"))
69csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.NanoSeconds(6560)))
71csmaDevices = csma.Install(csmaNodes)
73wifiStaNodes = ns.NodeContainer()
74wifiStaNodes.Create(nWifi.value)
75wifiApNode = p2pNodes.Get(0)
77channel = ns.YansWifiChannelHelper.Default()
78phy = ns.YansWifiPhyHelper()
79phy.SetChannel(channel.Create())
81mac = ns.WifiMacHelper()
82ssid = ns.Ssid(
"ns-3-ssid")
86mac.SetType(
"ns3::StaWifiMac",
"Ssid", ns.SsidValue(ssid),
"ActiveProbing", ns.BooleanValue(
False))
87staDevices = wifi.Install(phy, mac, wifiStaNodes)
89mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.SsidValue(ssid))
90apDevices = wifi.Install(phy, mac, wifiApNode)
92mobility = ns.MobilityHelper()
93mobility.SetPositionAllocator(
94 "ns3::GridPositionAllocator",
102 ns.DoubleValue(10.0),
106 ns.StringValue(
"RowFirst"),
109mobility.SetMobilityModel(
110 "ns3::RandomWalk2dMobilityModel",
112 ns.RectangleValue(ns.Rectangle(-50, 50, -50, 50)),
114mobility.Install(wifiStaNodes)
116mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
117mobility.Install(wifiApNode)
119stack = ns.InternetStackHelper()
120stack.Install(csmaNodes)
121stack.Install(wifiApNode)
122stack.Install(wifiStaNodes)
124address = ns.Ipv4AddressHelper()
125address.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
126p2pInterfaces = address.Assign(p2pDevices)
128address.SetBase(ns.Ipv4Address(
"10.1.2.0"), ns.Ipv4Mask(
"255.255.255.0"))
129csmaInterfaces = address.Assign(csmaDevices)
131address.SetBase(ns.Ipv4Address(
"10.1.3.0"), ns.Ipv4Mask(
"255.255.255.0"))
132address.Assign(staDevices)
133address.Assign(apDevices)
135echoServer = ns.UdpEchoServerHelper(9)
137serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
138serverApps.Start(ns.Seconds(1.0))
139serverApps.Stop(ns.Seconds(10.0))
141echoClient = ns.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
142echoClient.SetAttribute(
"MaxPackets", ns.UintegerValue(1))
143echoClient.SetAttribute(
"Interval", ns.TimeValue(ns.Seconds(1.0)))
144echoClient.SetAttribute(
"PacketSize", ns.UintegerValue(1024))
146clientApps = echoClient.Install(wifiStaNodes.Get(nWifi.value - 1))
147clientApps.Start(ns.Seconds(2.0))
148clientApps.Stop(ns.Seconds(10.0))
150ns.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
152ns.Simulator.Stop(ns.Seconds(10.0))
155 phy.SetPcapDataLinkType(phy.DLT_IEEE802_11_RADIO)
156 pointToPoint.EnablePcapAll(
"third")
157 phy.EnablePcap(
"third", apDevices.Get(0))
158 csma.EnablePcap(
"third", csmaDevices.Get(0),
True)
161ns.Simulator.Destroy()