21import ns.point_to_point
40cmd = ns.core.CommandLine()
46cmd.AddValue(
"nCsma",
"Number of \"extra\" CSMA nodes/devices")
47cmd.AddValue(
"nWifi",
"Number of wifi STA devices")
48cmd.AddValue(
"verbose",
"Tell echo applications to log if true")
49cmd.AddValue(
"tracing",
"Enable pcap tracing")
62 print (
"nWifi should be 18 or less; otherwise grid layout exceeds the bounding box")
66 ns.core.LogComponentEnable(
"UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
67 ns.core.LogComponentEnable(
"UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
69p2pNodes = ns.network.NodeContainer()
72pointToPoint = ns.point_to_point.PointToPointHelper()
73pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
74pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
76p2pDevices = pointToPoint.Install(p2pNodes)
78csmaNodes = ns.network.NodeContainer()
79csmaNodes.Add(p2pNodes.Get(1))
80csmaNodes.Create(nCsma)
82csma = ns.csma.CsmaHelper()
83csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
84csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
86csmaDevices = csma.Install(csmaNodes)
88wifiStaNodes = ns.network.NodeContainer()
89wifiStaNodes.Create(nWifi)
90wifiApNode = p2pNodes.Get(0)
92channel = ns.wifi.YansWifiChannelHelper.Default()
93phy = ns.wifi.YansWifiPhyHelper()
94phy.SetChannel(channel.Create())
96mac = ns.wifi.WifiMacHelper()
97ssid = ns.wifi.Ssid (
"ns-3-ssid")
99wifi = ns.wifi.WifiHelper()
101mac.SetType (
"ns3::StaWifiMac",
"Ssid", ns.wifi.SsidValue(ssid),
"ActiveProbing", ns.core.BooleanValue(
False))
102staDevices = wifi.Install(phy, mac, wifiStaNodes)
104mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.wifi.SsidValue (ssid))
105apDevices = wifi.Install(phy, mac, wifiApNode)
107mobility = ns.mobility.MobilityHelper()
108mobility.SetPositionAllocator (
"ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue(0.0),
109 "MinY", ns.core.DoubleValue (0.0),
"DeltaX", ns.core.DoubleValue(5.0),
"DeltaY", ns.core.DoubleValue(10.0),
110 "GridWidth", ns.core.UintegerValue(3),
"LayoutType", ns.core.StringValue(
"RowFirst"))
112mobility.SetMobilityModel (
"ns3::RandomWalk2dMobilityModel",
"Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle (-50, 50, -50, 50)))
113mobility.Install(wifiStaNodes)
115mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
116mobility.Install(wifiApNode)
118stack = ns.internet.InternetStackHelper()
119stack.Install(csmaNodes)
120stack.Install(wifiApNode)
121stack.Install(wifiStaNodes)
123address = ns.internet.Ipv4AddressHelper()
124address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
125p2pInterfaces = address.Assign(p2pDevices)
127address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
128csmaInterfaces = address.Assign(csmaDevices)
130address.SetBase(ns.network.Ipv4Address(
"10.1.3.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
131address.Assign(staDevices)
132address.Assign(apDevices)
134echoServer = ns.applications.UdpEchoServerHelper(9)
136serverApps = echoServer.Install(csmaNodes.Get(nCsma))
137serverApps.Start(ns.core.Seconds(1.0))
138serverApps.Stop(ns.core.Seconds(10.0))
140echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)
141echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
142echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
143echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
145clientApps = echoClient.Install(wifiStaNodes.Get (nWifi - 1))
146clientApps.Start(ns.core.Seconds(2.0))
147clientApps.Stop(ns.core.Seconds(10.0))
149ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
151ns.core.Simulator.Stop(ns.core.Seconds(10.0))
154 phy.SetPcapDataLinkType(phy.DLT_IEEE802_11_RADIO)
155 pointToPoint.EnablePcapAll (
"third")
156 phy.EnablePcap (
"third", apDevices.Get (0))
157 csma.EnablePcap (
"third", csmaDevices.Get (0),
True)
159ns.core.Simulator.Run()
160ns.core.Simulator.Destroy()