33 from ctypes
import c_bool, c_int
35 verbose = c_bool(
True)
37 tracing = c_bool(
False)
39 cmd = ns.CommandLine(__file__)
40 cmd.AddValue(
"nCsma",
"Number of extra CSMA nodes/devices", nCsma)
41 cmd.AddValue(
"nWifi",
"Number of wifi STA devices", nWifi)
42 cmd.AddValue(
"verbose",
"Tell echo applications to log if true", verbose)
43 cmd.AddValue(
"tracing",
"Enable pcap tracing", tracing)
51 print(
"nWifi should be 18 or less; otherwise grid layout exceeds the bounding box")
55 ns.core.LogComponentEnable(
"UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
56 ns.core.LogComponentEnable(
"UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
58 p2pNodes = ns.network.NodeContainer()
61 pointToPoint = ns.point_to_point.PointToPointHelper()
62 pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
63 pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
65 p2pDevices = pointToPoint.Install(p2pNodes)
67 csmaNodes = ns.network.NodeContainer()
68 csmaNodes.Add(p2pNodes.Get(1))
69 csmaNodes.Create(nCsma.value)
71 csma = ns.csma.CsmaHelper()
72 csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
73 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
75 csmaDevices = csma.Install(csmaNodes)
77 wifiStaNodes = ns.network.NodeContainer()
78 wifiStaNodes.Create(nWifi.value)
79 wifiApNode = p2pNodes.Get(0)
81 channel = ns.wifi.YansWifiChannelHelper.Default()
82 phy = ns.wifi.YansWifiPhyHelper()
83 phy.SetChannel(channel.Create())
85 mac = ns.wifi.WifiMacHelper()
86 ssid = ns.wifi.Ssid (
"ns-3-ssid")
88 wifi = ns.wifi.WifiHelper()
90 mac.SetType (
"ns3::StaWifiMac",
"Ssid", ns.wifi.SsidValue(ssid),
"ActiveProbing", ns.core.BooleanValue(
False))
91 staDevices = wifi.Install(phy, mac, wifiStaNodes)
93 mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.wifi.SsidValue (ssid))
94 apDevices = wifi.Install(phy, mac, wifiApNode)
96 mobility = ns.mobility.MobilityHelper()
97 mobility.SetPositionAllocator(
"ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue(0.0),
98 "MinY", ns.core.DoubleValue (0.0),
"DeltaX", ns.core.DoubleValue(5.0),
"DeltaY", ns.core.DoubleValue(10.0),
99 "GridWidth", ns.core.UintegerValue(3),
"LayoutType", ns.core.StringValue(
"RowFirst"))
101 mobility.SetMobilityModel (
"ns3::RandomWalk2dMobilityModel",
"Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle (-50, 50, -50, 50)))
102 mobility.Install(wifiStaNodes)
104 mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
105 mobility.Install(wifiApNode)
107 stack = ns.internet.InternetStackHelper()
108 stack.Install(csmaNodes)
109 stack.Install(wifiApNode)
110 stack.Install(wifiStaNodes)
112 address = ns.internet.Ipv4AddressHelper()
113 address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
114 p2pInterfaces = address.Assign(p2pDevices)
116 address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
117 csmaInterfaces = address.Assign(csmaDevices)
119 address.SetBase(ns.network.Ipv4Address(
"10.1.3.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
120 address.Assign(staDevices)
121 address.Assign(apDevices)
123 echoServer = ns.applications.UdpEchoServerHelper(9)
125 serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
126 serverApps.Start(ns.core.Seconds(1.0))
127 serverApps.Stop(ns.core.Seconds(10.0))
129 echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
130 echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
131 echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
132 echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
134 clientApps = echoClient.Install(wifiStaNodes.Get (nWifi.value - 1))
135 clientApps.Start(ns.core.Seconds(2.0))
136 clientApps.Stop(ns.core.Seconds(10.0))
138 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
140 ns.core.Simulator.Stop(ns.core.Seconds(10.0))
143 phy.SetPcapDataLinkType(phy.DLT_IEEE802_11_RADIO)
144 pointToPoint.EnablePcapAll (
"third")
145 phy.EnablePcap (
"third", apDevices.Get (0))
146 csma.EnablePcap (
"third", csmaDevices.Get (0),
True)
148 ns.core.Simulator.Run()
149 ns.core.Simulator.Destroy()