21 import ns.point_to_point
 
   22 import ns.applications
 
   40 cmd = ns.core.CommandLine()
 
   44 cmd.AddValue(
"nCsma", 
"Number of \"extra\" CSMA nodes/devices")
 
   45 cmd.AddValue(
"nWifi", 
"Number of wifi STA devices")
 
   46 cmd.AddValue(
"verbose", 
"Tell echo applications to log if true")
 
   50 nCsma = int(cmd.nCsma)
 
   52 nWifi = int(cmd.nWifi)
 
   55     print "Number of wifi nodes "+ str(nWifi)+ 
" specified exceeds the mobility bounding box" 
   59     ns.core.LogComponentEnable(
"UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
 
   60     ns.core.LogComponentEnable(
"UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
 
   62 p2pNodes = ns.network.NodeContainer()
 
   65 pointToPoint = ns.point_to_point.PointToPointHelper()
 
   66 pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
 
   67 pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
 
   69 p2pDevices = pointToPoint.Install(p2pNodes)
 
   71 csmaNodes = ns.network.NodeContainer()
 
   72 csmaNodes.Add(p2pNodes.Get(1))
 
   73 csmaNodes.Create(nCsma)
 
   75 csma = ns.csma.CsmaHelper()
 
   76 csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
 
   77 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
 
   79 csmaDevices = csma.Install(csmaNodes)
 
   81 wifiStaNodes = ns.network.NodeContainer()
 
   82 wifiStaNodes.Create(nWifi)
 
   83 wifiApNode = p2pNodes.Get(0)
 
   85 channel = ns.wifi.YansWifiChannelHelper.Default()
 
   86 phy = ns.wifi.YansWifiPhyHelper.Default()
 
   87 phy.SetChannel(channel.Create())
 
   89 wifi = ns.wifi.WifiHelper.Default()
 
   90 wifi.SetRemoteStationManager(
"ns3::AarfWifiManager")
 
   92 mac = ns.wifi.NqosWifiMacHelper.Default()
 
   93 ssid = ns.wifi.Ssid (
"ns-3-ssid")
 
   95 mac.SetType (
"ns3::StaWifiMac", 
"Ssid", ns.wifi.SsidValue(ssid), 
"ActiveProbing", ns.core.BooleanValue(
False))
 
   96 staDevices = wifi.Install(phy, mac, wifiStaNodes)
 
   98 mac.SetType(
"ns3::ApWifiMac",
"Ssid", ns.wifi.SsidValue (ssid))
 
   99 apDevices = wifi.Install(phy, mac, wifiApNode)
 
  101 mobility = ns.mobility.MobilityHelper()
 
  102 mobility.SetPositionAllocator (
"ns3::GridPositionAllocator", 
"MinX", ns.core.DoubleValue(0.0), 
 
  103                                 "MinY", ns.core.DoubleValue (0.0), 
"DeltaX", ns.core.DoubleValue(5.0), 
"DeltaY", ns.core.DoubleValue(10.0), 
 
  104                                  "GridWidth", ns.core.UintegerValue(3), 
"LayoutType", ns.core.StringValue(
"RowFirst"))
 
  106 mobility.SetMobilityModel (
"ns3::RandomWalk2dMobilityModel", 
"Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle (-50, 50, -50, 50)))
 
  107 mobility.Install(wifiStaNodes)
 
  109 mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
 
  110 mobility.Install(wifiApNode)
 
  112 stack = ns.internet.InternetStackHelper()
 
  113 stack.Install(csmaNodes)
 
  114 stack.Install(wifiApNode)
 
  115 stack.Install(wifiStaNodes)
 
  117 address = ns.internet.Ipv4AddressHelper()
 
  118 address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
 
  119 p2pInterfaces = address.Assign(p2pDevices)
 
  121 address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
 
  122 csmaInterfaces = address.Assign(csmaDevices)
 
  124 address.SetBase(ns.network.Ipv4Address(
"10.1.3.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
 
  125 address.Assign(staDevices)
 
  126 address.Assign(apDevices)
 
  128 echoServer = ns.applications.UdpEchoServerHelper(9)
 
  130 serverApps = echoServer.Install(csmaNodes.Get(nCsma))
 
  131 serverApps.Start(ns.core.Seconds(1.0))
 
  132 serverApps.Stop(ns.core.Seconds(10.0))
 
  134 echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)
 
  135 echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
 
  136 echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
 
  137 echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
 
  139 clientApps = echoClient.Install(wifiStaNodes.Get (nWifi - 1))
 
  140 clientApps.Start(ns.core.Seconds(2.0))
 
  141 clientApps.Stop(ns.core.Seconds(10.0))
 
  143 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
 
  145 ns.core.Simulator.Stop(ns.core.Seconds(10.0))
 
  147 pointToPoint.EnablePcapAll (
"third")
 
  148 phy.EnablePcap (
"third", apDevices.Get (0))
 
  149 csma.EnablePcap (
"third", csmaDevices.Get (0), 
True)
 
  151 ns.core.Simulator.Run()
 
  152 ns.core.Simulator.Destroy()