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
    评论
02f,18aug03,agi added #include 02e,02jun03,agi removed #include "rwproto.h" 02d,02jun03,agi changed #include "rwos.h" to include "ospf_rwos.h" 02c,29may03,agi removed unused includes, added new includes 02c,08may03,asr Changes to make OSPF virtual stack compatible 02b,09may03,agi added #include , removed #include 02a,17feb02,ram SPR 81808 Added OSPF memory partition support 21,13october01,kc Dynamic configuration changes. 20,21september01,kc Removed unused raw socket specific declarations. 19,26september00,reshma Added WindRiver CopyRight 18,25september00,reshma RFC-1587 implementation for OSPF NSSA Option, also tested against ANVL. 17,20july00,reshma Unix compatibility related changes. 16,06july00,reshma Removed unnecessary header files and defines. 15,23february00,reshma Changes for ospf mib 14,23december99,reshma Compatibility with VxWorks-IP and VxWorks RTM-interface 13,13august99,jack compilation fixes no IP case 12,05august99,nishit Replaced including IP header files by the new ospf_ip_structures.h 11,17may99,jack Added new include file ospf_patricia_32_bits_key_prototypes.h 10,28december98,jack Compiled and added some comments 09,25november98,rajive Deleted socket include file 08,11november98,jack Config changes, linted and big endian changes 07,30october98,jack Incorporate changes for compilation on Vxworks 06,12february98,release engineer code style changes, feature enhancements, complete CISCO and BAY compaltibility. OSPF v4.2.0 05,10july97,cindy Pre-release v1.52b 04,10february97,cindy Release Version 1.52 03,22october97,cindy Release Version 1.50 02,05june96,cindy Including visnpstr.h as a kludge for the first beta release. 01,05june96,cindy First Beta Release
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值