zoukankan      html  css  js  c++  java
  • NS3网络仿真(7): Wifi节点


    http://blog.csdn.net/lights_joy/article/details/46868631


    快乐虾

    http://blog.csdn.net/lights_joy/

    欢迎转载,但请保留作者信息


    在上一节中,我们仿真了一个总线型网络,这一节尝试将上一节中的n0变成一个无线的AP,再连上几个节点。这也是NS3中的示例third.cc干的事情,只是我们用Python实现。

    1. // Default Network Topology  
    2. //  
    3. //   Wifi 10.1.3.0  
    4. //                 AP  
    5. //  *    *    *    *  
    6. //  |    |    |    |    10.1.1.0  
    7. // n5   n6   n7   n0 -------------- n1   n2   n3   n4  
    8. //                   point-to-point  |    |    |    |  
    9. //                                   ================  
    10. //                                     LAN 10.1.2.0  


    与上一节一样,先构造p2p网络,再构建总线型网线:

    1. # 构建点对点连接  
    2. p2pNodes = ns.network.NodeContainer()  
    3. p2pNodes.Create (2)  
    4.   
    5. pointToPoint = ns.point_to_point.PointToPointHelper()  
    6. pointToPoint.SetDeviceAttribute ("DataRate", ns.core.StringValue ("5Mbps"))  
    7. pointToPoint.SetChannelAttribute ("Delay", ns.core.StringValue ("2ms"))  
    8. p2pDevices = pointToPoint.Install (p2pNodes)  
    9.   
    10. # 构建总线连接  
    11. nCsma = 3  
    12.   
    13. csmaNodes = ns.network.NodeContainer()  
    14. csmaNodes.Add (p2pNodes.Get (1))  
    15. csmaNodes.Create (nCsma)  
    16.   
    17. csma = ns.csma.CsmaHelper()  
    18. csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("100Mbps"))  
    19. csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.NanoSeconds (6560)))  
    20. csmaDevices = csma.Install (csmaNodes)  

    接着构建无线网络:

    1. # 构建Wifi连接  
    2. nWifi = 3  
    3. wifiStaNodes = ns.network.NodeContainer()  
    4. wifiStaNodes.Create (nWifi)  
    5. wifiApNode = p2pNodes.Get (0)  
    6.   
    7. channel = ns.wifi.YansWifiChannelHelper.Default ()  
    8. phy = ns.wifi.YansWifiPhyHelper.Default ()  
    9. phy.SetChannel (channel.Create ())  

    接着配置AP:

    1. # 配置AP  
    2. wifi = ns.wifi.WifiHelper.Default ()  
    3. wifi.SetRemoteStationManager ("ns3::AarfWifiManager")  
    4.   
    5. mac = ns.wifi.NqosWifiMacHelper.Default ()  
    6.   
    7. ssid = ns.wifi.Ssid ("ns-3-ssid")  
    8. mac.SetType ("ns3::StaWifiMac",  
    9.             "Ssid", ns.wifi.SsidValue (ssid),  
    10.             "ActiveProbing", ns.core.BooleanValue (False))  
    11.   
    12. staDevices = wifi.Install (phy, mac, wifiStaNodes)  
    13.   
    14. mac.SetType ("ns3::ApWifiMac",  
    15.             "Ssid", ns.wifi.SsidValue (ssid))  
    16.   
    17. apDevices = wifi.Install (phy, mac, wifiApNode);  

    接着配置无线节点的位置参数:

    1. # 配置无线节点的位置  
    2. mobility = ns.mobility.MobilityHelper()  
    3.   
    4. mobility.SetPositionAllocator ("ns3::GridPositionAllocator",  
    5.                                 "MinX", ns.core.DoubleValue (0.0),  
    6.                                 "MinY", ns.core.DoubleValue (0.0),  
    7.                                 "DeltaX", ns.core.DoubleValue (5.0),  
    8.                                 "DeltaY", ns.core.DoubleValue (10.0),  
    9.                                 "GridWidth", ns.core.UintegerValue (3),  
    10.                                 "LayoutType", ns.core.StringValue ("RowFirst"))  
    11.   
    12. mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",  
    13.                             "Bounds", ns.mobility.RectangleValue (ns.mobility.Rectangle (-50, 50, -50, 50)))  
    14. mobility.Install (wifiStaNodes)  
    15.   
    16. mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel")  
    17. mobility.Install (wifiApNode)  

    接着安装协议栈:

    1. # 安装协议栈  
    2. stack = ns.internet.InternetStackHelper()  
    3. stack.Install (csmaNodes)  
    4. stack.Install (wifiApNode)  
    5. stack.Install (wifiStaNodes)  

    配置IP,这个和上一节一样,只是加上10.1.3.0网段而已:

    1. # 配置IP  
    2. address = ns.internet.Ipv4AddressHelper()  
    3. address.SetBase (  
    4.     ns.network.Ipv4Address("10.1.1.0"),   
    5.     ns.network.Ipv4Mask("255.255.255.0"))  
    6. p2pInterfaces = address.Assign (p2pDevices)  
    7.   
    8. address.SetBase (  
    9.     ns.network.Ipv4Address("10.1.2.0"),   
    10.     ns.network.Ipv4Mask("255.255.255.0"))  
    11. csmaInterfaces = address.Assign (csmaDevices)  
    12.   
    13. address.SetBase (  
    14.     ns.network.Ipv4Address("10.1.3.0"),   
    15.     ns.network.Ipv4Mask("255.255.255.0"))  
    16. address.Assign (staDevices)  
    17. address.Assign (apDevices)  

    接下来模拟一个Echo服务,这个与上一节相同,只是Client安装在了Wifi节点上。

    1. # 配置应用程序  
    2. echoServer = ns.applications.UdpEchoServerHelper (9)  
    3.   
    4. serverApps = echoServer.Install (csmaNodes.Get (nCsma))  
    5. serverApps.Start (ns.core.Seconds (1.0))  
    6. serverApps.Stop (ns.core.Seconds (20.0))  
    7.   
    8. echoClient = ns.applications.UdpEchoClientHelper (csmaInterfaces.GetAddress (nCsma), 9)  
    9. echoClient.SetAttribute ("MaxPackets", ns.core.UintegerValue (5))  
    10. echoClient.SetAttribute ("Interval", ns.core.TimeValue (ns.core.Seconds (1.0)))  
    11. echoClient.SetAttribute ("PacketSize", ns.core.UintegerValue (1024))  
    12.   
    13. clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1))  
    14. clientApps.Start (ns.core.Seconds (2.0))  
    15. clientApps.Stop (ns.core.Seconds (20.0))  

    接下来的部分与上一节几乎完全相同,只是加上了Simulator.Stop,因为如果没有这个函数调用,那么将导致Simulator.Run永远不会停止:

    1. # 全局路由管理器根据节点产生 的链路通告为每个节点建立路由表  
    2. ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()  
    3.   
    4. ns.core.Simulator.Stop (ns.core.Seconds (10.0));  
    5.   
    6. pointToPoint.EnablePcapAll ("third");  
    7. csma.EnablePcap ("third", csmaDevices.Get (1), True)  
    8. phy.EnablePcap ("third", apDevices.Get (0))  
    9.   
    10. anim = ns.netanim.AnimationInterface('third.xml')  
    11. anim.SetConstantPosition(p2pNodes.Get(0), 10, 10)  
    12. anim.SetConstantPosition(csmaNodes.Get(0), 30, 10)  
    13. anim.SetConstantPosition(csmaNodes.Get(1), 40, 10)  
    14. anim.SetConstantPosition(csmaNodes.Get(2), 50, 10)  
    15. anim.SetConstantPosition(csmaNodes.Get(3), 60, 10)  
    16. anim.EnablePacketMetadata(True)  
    17.   
    18. # 开始仿真  
    19. ns.core.Simulator.Run()  
    20. ns.core.Simulator.Destroy()  

     


    看看NetAnim显示的仿真结果:


    再看看third-0-1.pcap的内容:


    如我们所愿,802.11协议,呵呵~~~~~



  • 相关阅读:
    Chrome使用指南
    Vue2.x-踩坑记
    C# WinForm listView 多行删除 操作
    Winform中DataGridView多行删除
    20211026_阿里云服务器引流限制ssl的问题
    docker commit
    docker build
    docker build与docker commit
    阿里云Docker镜像仓库(Docker Registry)
    Docker Nginx安装(centos7)
  • 原文地址:https://www.cnblogs.com/ztguang/p/12644767.html
Copyright © 2011-2022 走看看