NS3網路模擬(7): Wifi節點
阿新 • • 發佈:2019-02-10
在上一節中,我們模擬了一個匯流排型網路,這一節嘗試將上一節中的n0變成一個無線的AP,再連上幾個節點。這也是NS3中的示例third.cc乾的事情,只是我們用Python實現。
// Default Network Topology // // Wifi 10.1.3.0 // AP // * * * * // | | | | 10.1.1.0 // n5 n6 n7 n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0
與上一節一樣,先構造p2p網路,再構建匯流排型網線:
# 構建點對點連線 p2pNodes = ns.network.NodeContainer() p2pNodes.Create (2) pointToPoint = ns.point_to_point.PointToPointHelper() pointToPoint.SetDeviceAttribute ("DataRate", ns.core.StringValue ("5Mbps")) pointToPoint.SetChannelAttribute ("Delay", ns.core.StringValue ("2ms")) p2pDevices = pointToPoint.Install (p2pNodes) # 構建匯流排連線 nCsma = 3 csmaNodes = ns.network.NodeContainer() csmaNodes.Add (p2pNodes.Get (1)) csmaNodes.Create (nCsma) csma = ns.csma.CsmaHelper() csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("100Mbps")) csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.NanoSeconds (6560))) csmaDevices = csma.Install (csmaNodes)
接著構建無線網路:
# 構建Wifi連線
nWifi = 3
wifiStaNodes = ns.network.NodeContainer()
wifiStaNodes.Create (nWifi)
wifiApNode = p2pNodes.Get (0)
channel = ns.wifi.YansWifiChannelHelper.Default ()
phy = ns.wifi.YansWifiPhyHelper.Default ()
phy.SetChannel (channel.Create ())
接著配置AP:
# 配置AP wifi = ns.wifi.WifiHelper.Default () wifi.SetRemoteStationManager ("ns3::AarfWifiManager") mac = ns.wifi.NqosWifiMacHelper.Default () ssid = ns.wifi.Ssid ("ns-3-ssid") mac.SetType ("ns3::StaWifiMac", "Ssid", ns.wifi.SsidValue (ssid), "ActiveProbing", ns.core.BooleanValue (False)) staDevices = wifi.Install (phy, mac, wifiStaNodes) mac.SetType ("ns3::ApWifiMac", "Ssid", ns.wifi.SsidValue (ssid)) apDevices = wifi.Install (phy, mac, wifiApNode);
接著配置無線節點的位置引數:
# 配置無線節點的位置
mobility = ns.mobility.MobilityHelper()
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue (0.0),
"MinY", ns.core.DoubleValue (0.0),
"DeltaX", ns.core.DoubleValue (5.0),
"DeltaY", ns.core.DoubleValue (10.0),
"GridWidth", ns.core.UintegerValue (3),
"LayoutType", ns.core.StringValue ("RowFirst"))
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", ns.mobility.RectangleValue (ns.mobility.Rectangle (-50, 50, -50, 50)))
mobility.Install (wifiStaNodes)
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel")
mobility.Install (wifiApNode)
接著安裝協議棧:
# 安裝協議棧
stack = ns.internet.InternetStackHelper()
stack.Install (csmaNodes)
stack.Install (wifiApNode)
stack.Install (wifiStaNodes)
配置IP,這個和上一節一樣,只是加上10.1.3.0網段而已:
# 配置IP
address = ns.internet.Ipv4AddressHelper()
address.SetBase (
ns.network.Ipv4Address("10.1.1.0"),
ns.network.Ipv4Mask("255.255.255.0"))
p2pInterfaces = address.Assign (p2pDevices)
address.SetBase (
ns.network.Ipv4Address("10.1.2.0"),
ns.network.Ipv4Mask("255.255.255.0"))
csmaInterfaces = address.Assign (csmaDevices)
address.SetBase (
ns.network.Ipv4Address("10.1.3.0"),
ns.network.Ipv4Mask("255.255.255.0"))
address.Assign (staDevices)
address.Assign (apDevices)
接下來模擬一個Echo服務,這個與上一節相同,只是Client安裝在了Wifi節點上。
# 配置應用程式
echoServer = ns.applications.UdpEchoServerHelper (9)
serverApps = echoServer.Install (csmaNodes.Get (nCsma))
serverApps.Start (ns.core.Seconds (1.0))
serverApps.Stop (ns.core.Seconds (20.0))
echoClient = ns.applications.UdpEchoClientHelper (csmaInterfaces.GetAddress (nCsma), 9)
echoClient.SetAttribute ("MaxPackets", ns.core.UintegerValue (5))
echoClient.SetAttribute ("Interval", ns.core.TimeValue (ns.core.Seconds (1.0)))
echoClient.SetAttribute ("PacketSize", ns.core.UintegerValue (1024))
clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1))
clientApps.Start (ns.core.Seconds (2.0))
clientApps.Stop (ns.core.Seconds (20.0))
接下來的部分與上一節幾乎完全相同,只是加上了Simulator.Stop,因為如果沒有這個函式呼叫,那麼將導致Simulator.Run永遠不會停止:
# 全域性路由管理器根據節點產生 的鏈路通告為每個節點建立路由表
ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
ns.core.Simulator.Stop (ns.core.Seconds (10.0));
pointToPoint.EnablePcapAll ("third");
csma.EnablePcap ("third", csmaDevices.Get (1), True)
phy.EnablePcap ("third", apDevices.Get (0))
anim = ns.netanim.AnimationInterface('third.xml')
anim.SetConstantPosition(p2pNodes.Get(0), 10, 10)
anim.SetConstantPosition(csmaNodes.Get(0), 30, 10)
anim.SetConstantPosition(csmaNodes.Get(1), 40, 10)
anim.SetConstantPosition(csmaNodes.Get(2), 50, 10)
anim.SetConstantPosition(csmaNodes.Get(3), 60, 10)
anim.EnablePacketMetadata(True)
# 開始模擬
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
看看NetAnim顯示的模擬結果:
再看看third-0-1.pcap的內容:
如我們所願,802.11協議,呵呵~~~~~