ns3 入门案例2:third.cc
代碼分析
1 頭文件
#include "ns3/core-module.h" #include "ns3/point-to-point-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/mobility-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/yans-wifi-helper.h" #include "ns3/ssid.h"2 名字空間
3 打印內容
NS_LOG_COMPONENT_DEFINE (“ThirdScriptExample”);
4 主函數(變量聲明)
與案例1不同,此處定義并使用一些命令行參數。
bool verbose = true;uint32_t nCsma = 3;uint32_t nWifi = 3;bool tracing = false;CommandLine cmd;cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);cmd.AddValue ("tracing", "Enable pcap tracing", tracing); `以nWifi為例,意義為默認無線節點數目,設置大小為3,但可以通過命令行傳入參數
若運行時采用以下方式,則把參數傳進里面。
5 創建網絡拓撲
(1)案例1涉及PPP網絡,【設置鏈路速率、時延】代碼如下
NodeContainer p2pNodes;p2pNodes.Create (2);PointToPointHelper pointToPoint;pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer p2pDevices;p2pDevices = pointToPoint.Install (p2pNodes);(2)CSMA網絡
CSMA可以連接多個節點,節點之間競爭使用信道
(PPP連接兩個節點,專屬信道)
(3)Wifi網絡
由一個接入點(AP)與nWiFi個移動節點組成,AP為雙模節點,安裝WiFi與PPP兩個設備。WiFi協議包括鏈路層(WiFIMac)與物理層(WifiPhy)。WifiNetDevice只起到連接上下層協議的作用。
1)
設置Channel和WifiPhy
2)
服務集標志符(SSID)
其中,設置主要用WifiHelper設置,助手類
3)設置移動模型
設置笛卡爾坐標系,其中AP節點坐標設為原點
主要應用助手類MobilityHelper
6 安裝TCP/IP協議
InternetStackHelper stack;stack.Install (csmaNodes);stack.Install (wifiApNode);stack.Install (wifiStaNodes);Ipv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer p2pInterfaces;p2pInterfaces = address.Assign (p2pDevices);address.SetBase ("10.1.2.0", "255.255.255.0");Ipv4InterfaceContainer csmaInterfaces;csmaInterfaces = address.Assign (csmaDevices);address.SetBase ("10.1.3.0", "255.255.255.0");address.Assign (staDevices);address.Assign (apDevices);UdpEchoServerHelper echoServer (9);7 安裝應用程序
ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);echoClient.SetAttribute ("MaxPackets", UintegerValue (1));echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));echoClient.SetAttribute ("PacketSize", UintegerValue (1024));ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));具體解釋可以看案例1
8 設置路由
之前一個節點設置兩個網絡設備(PPP CSMA),而網絡屬于兩個不同的子網,需要連接兩個字網的節點具有路由功能。才可以進行數據包的轉發(PPP與CSMA之間)
此處設置路由協議為全局路由,采用最短路徑優先(OSPF算法),為每個節點生成路由表。對于IPV4,此處調用Ipv4GlobalRoutingHelper的PopulateRoutingTable()函數。
9 數據追蹤
分析網絡性能的重要前提是獲取實驗數據,ns3以pcap或ASCII文本形式保存在指定文件中,這部分代碼往往最后編輯。
pointToPoint.EnablePcapAll ("third");//EnablePcapAll()用于收集信道上所有鏈路層分組收發記錄,格式Pcap,文件名前綴為third,命名規則為“前綴-節點號-網絡設備”phy.EnablePcap ("third", apDevices.Get (0));csma.EnablePcap ("third", csmaDevices.Get (0), true);10 啟動與結束
Simulator::Run ();Simulator::Destroy ();return 0;運行程序
在ns3目錄下運行
./waf --run third總結
以上是生活随笔為你收集整理的ns3 入门案例2:third.cc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 京东物流上半年实现盈利 二季度盈利8.3
- 下一篇: 比亚迪方程豹“豹8&rdqu