33from ctypes
import c_bool, c_int
37tracing = c_bool(
False)
39cmd = ns.CommandLine(__file__)
40cmd.AddValue(
"nCsma",
"Number of extra CSMA nodes/devices", nCsma)
41cmd.AddValue(
"nWifi",
"Number of wifi STA devices", nWifi)
42cmd.AddValue(
"verbose",
"Tell echo applications to log if true", verbose)
43cmd.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)
58p2pNodes = ns.network.NodeContainer()
61pointToPoint = ns.point_to_point.PointToPointHelper()
62pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
63pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
65p2pDevices = pointToPoint.Install(p2pNodes)
67csmaNodes = ns.network.NodeContainer()
68csmaNodes.Add(p2pNodes.Get(1))
69csmaNodes.Create(nCsma.value)
71csma = ns.csma.CsmaHelper()
72csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
73csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
75csmaDevices = csma.Install(csmaNodes)
77wifiStaNodes = ns.network.NodeContainer()
78wifiStaNodes.Create(nWifi.value)
79wifiApNode = p2pNodes.Get(0)
81channel = ns.wifi.YansWifiChannelHelper.Default()
82phy = ns.wifi.YansWifiPhyHelper()
83phy.SetChannel(channel.Create())
85mac = ns.wifi.WifiMacHelper()
86ssid = ns.wifi.Ssid (
"ns-3-ssid")
88wifi = ns.wifi.WifiHelper()
90mac.SetType (
"ns3::StaWifiMac",
"Ssid", ns.wifi.SsidValue(ssid),
"ActiveProbing", ns.core.BooleanValue(
False))
91staDevices = wifi.Install(phy, mac, wifiStaNodes)
93mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.wifi.SsidValue (ssid))
94apDevices = wifi.Install(phy, mac, wifiApNode)
96mobility = ns.mobility.MobilityHelper()
97mobility.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"))
101mobility.SetMobilityModel (
"ns3::RandomWalk2dMobilityModel",
"Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle (-50, 50, -50, 50)))
102mobility.Install(wifiStaNodes)
104mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
105mobility.Install(wifiApNode)
107stack = ns.internet.InternetStackHelper()
108stack.Install(csmaNodes)
109stack.Install(wifiApNode)
110stack.Install(wifiStaNodes)
112address = ns.internet.Ipv4AddressHelper()
113address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
114p2pInterfaces = address.Assign(p2pDevices)
116address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
117csmaInterfaces = address.Assign(csmaDevices)
119address.SetBase(ns.network.Ipv4Address(
"10.1.3.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
120address.Assign(staDevices)
121address.Assign(apDevices)
123echoServer = ns.applications.UdpEchoServerHelper(9)
125serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
126serverApps.Start(ns.core.Seconds(1.0))
127serverApps.Stop(ns.core.Seconds(10.0))
129echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
130echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
131echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
132echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
134clientApps = echoClient.Install(wifiStaNodes.Get (nWifi.value - 1))
135clientApps.Start(ns.core.Seconds(2.0))
136clientApps.Stop(ns.core.Seconds(10.0))
138ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
140ns.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)
148ns.core.Simulator.Run()
149ns.core.Simulator.Destroy()