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,但可以通过命令行传入参数
若运行时采用以下方式,则把参数传进里面。

./waf --run "third --nWifi=18"

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连接两个节点,专属信道)

  NodeContainer csmaNodes;
  csmaNodes.Add (p2pNodes.Get (1));//此处为双接口节点
  csmaNodes.Create (nCsma);

  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

  NetDeviceContainer csmaDevices;
  csmaDevices = csma.Install (csmaNodes);

(3)Wifi网络
由一个接入点(AP)与nWiFi个移动节点组成,AP为双模节点,安装WiFi与PPP两个设备。WiFi协议包括链路层(WiFIMac)与物理层(WifiPhy)。WifiNetDevice只起到连接上下层协议的作用。
1)

    NodeContainer wifiStaNodes;
  wifiStaNodes.Create (nWifi);
  NodeContainer wifiApNode = p2pNodes.Get (0);//双模节点
//默认模型
  YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();//配置Channel类
  YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();//配置wifiPhy类
  phy.SetChannel (channel.Create ());

设置Channel和WifiPhy
2)

  WifiHelper wifi;
  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");

  WifiMacHelper mac;
  Ssid ssid = Ssid ("ns-3-ssid");
  mac.SetType ("ns3::StaWifiMac",//移动节点
               "Ssid", SsidValue (ssid),
               "ActiveProbing", BooleanValue (false));

  NetDeviceContainer staDevices;
  staDevices = wifi.Install (phy, mac, wifiStaNodes);//安装移动节点

  mac.SetType ("ns3::ApWifiMac",
               "Ssid", SsidValue (ssid));

  NetDeviceContainer apDevices;//AP结点
  apDevices = wifi.Install (phy, mac, wifiApNode);//安装AP节点

服务集标志符(SSID)
其中,设置主要用WifiHelper设置,助手类
3)设置移动模型
设置笛卡尔坐标系,其中AP节点坐标设为原点
主要应用助手类MobilityHelper

    MobilityHelper mobility;
 //使用分布器GridPositionAllocator形成初始分布
  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                                 "MinX", DoubleValue (0.0),
                                 "MinY", DoubleValue (0.0),
                                 "DeltaX", DoubleValue (5.0),
                                 "DeltaY", DoubleValue (10.0),
                                 "GridWidth", UintegerValue (3),
                                 "LayoutType", StringValue ("RowFirst"));
//利用RandomWalk2MobilityModel设置移动轨迹
  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                             "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
  mobility.Install (wifiStaNodes);
// 为AP节点设置移动模型,设置原点
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (wifiApNode);

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()函数。

 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

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

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值