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协议,呵呵~~~~~



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值