21 import ns.point_to_point
22 import ns.applications
40 cmd = ns.core.CommandLine()
46 cmd.AddValue(
"nCsma",
"Number of \"extra\" CSMA nodes/devices")
47 cmd.AddValue(
"nWifi",
"Number of wifi STA devices")
48 cmd.AddValue(
"verbose",
"Tell echo applications to log if true")
49 cmd.AddValue(
"tracing",
"Enable pcap tracing")
53 nCsma = int(cmd.nCsma)
55 nWifi = int(cmd.nWifi)
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)
69 p2pNodes = ns.network.NodeContainer()
72 pointToPoint = ns.point_to_point.PointToPointHelper()
73 pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
74 pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
76 p2pDevices = pointToPoint.Install(p2pNodes)
78 csmaNodes = ns.network.NodeContainer()
79 csmaNodes.Add(p2pNodes.Get(1))
80 csmaNodes.Create(nCsma)
82 csma = ns.csma.CsmaHelper()
83 csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
84 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
86 csmaDevices = csma.Install(csmaNodes)
88 wifiStaNodes = ns.network.NodeContainer()
89 wifiStaNodes.Create(nWifi)
90 wifiApNode = p2pNodes.Get(0)
92 channel = ns.wifi.YansWifiChannelHelper.Default()
93 phy = ns.wifi.YansWifiPhyHelper.Default()
94 phy.SetChannel(channel.Create())
96 wifi = ns.wifi.WifiHelper()
97 wifi.SetRemoteStationManager(
"ns3::AarfWifiManager")
99 mac = ns.wifi.WifiMacHelper()
100 ssid = ns.wifi.Ssid (
"ns-3-ssid")
102 mac.SetType (
"ns3::StaWifiMac",
"Ssid", ns.wifi.SsidValue(ssid),
"ActiveProbing", ns.core.BooleanValue(
False))
103 staDevices = wifi.Install(phy, mac, wifiStaNodes)
105 mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.wifi.SsidValue (ssid))
106 apDevices = wifi.Install(phy, mac, wifiApNode)
108 mobility = ns.mobility.MobilityHelper()
109 mobility.SetPositionAllocator (
"ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue(0.0),
110 "MinY", ns.core.DoubleValue (0.0),
"DeltaX", ns.core.DoubleValue(5.0),
"DeltaY", ns.core.DoubleValue(10.0),
111 "GridWidth", ns.core.UintegerValue(3),
"LayoutType", ns.core.StringValue(
"RowFirst"))
113 mobility.SetMobilityModel (
"ns3::RandomWalk2dMobilityModel",
"Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle (-50, 50, -50, 50)))
114 mobility.Install(wifiStaNodes)
116 mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
117 mobility.Install(wifiApNode)
119 stack = ns.internet.InternetStackHelper()
120 stack.Install(csmaNodes)
121 stack.Install(wifiApNode)
122 stack.Install(wifiStaNodes)
124 address = ns.internet.Ipv4AddressHelper()
125 address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
126 p2pInterfaces = address.Assign(p2pDevices)
128 address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
129 csmaInterfaces = address.Assign(csmaDevices)
131 address.SetBase(ns.network.Ipv4Address(
"10.1.3.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
132 address.Assign(staDevices)
133 address.Assign(apDevices)
135 echoServer = ns.applications.UdpEchoServerHelper(9)
137 serverApps = echoServer.Install(csmaNodes.Get(nCsma))
138 serverApps.Start(ns.core.Seconds(1.0))
139 serverApps.Stop(ns.core.Seconds(10.0))
141 echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)
142 echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
143 echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
144 echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
146 clientApps = echoClient.Install(wifiStaNodes.Get (nWifi - 1))
147 clientApps.Start(ns.core.Seconds(2.0))
148 clientApps.Stop(ns.core.Seconds(10.0))
150 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
152 ns.core.Simulator.Stop(ns.core.Seconds(10.0))
154 if tracing ==
"True":
155 pointToPoint.EnablePcapAll (
"third")
156 phy.EnablePcap (
"third", apDevices.Get (0))
157 csma.EnablePcap (
"third", csmaDevices.Get (0),
True)
159 ns.core.Simulator.Run()
160 ns.core.Simulator.Destroy()