ns3学习笔记(二):third.cc代码分析

一、场景

官方教程中把这个例子当作建立无线网络拓扑的教程,是在second.cc上面进行的拓展,增加了wifi网络

Building a Wireless Network Topology
if you change nCsma, it will give you a number of “extra” CSMA nodes.
Similarly, you can set nWifi to control how many STA (station) nodes are created in the simulation.
There will always be one AP (access point) node on the wireless network.
By default there are three “extra” CSMA nodes and three wireless STA nodes.

# // Default Network Topology
# //
# //   Wifi 10.1.3.0
# //                 AP
# //  *    *    *    *
# //  |    |    |    |    10.1.1.0
# // n5   n6   n7   n0 -------------- n1   n2   n3   n4
# //                   point-to-point  |    |    |    |
# //                                   ================
# //                                     LAN 10.1.2.0

AP是静止的接入点,STA nodes是为了补充wifi网络,CSMA nodes同理,应该是为了模拟更真实,网络中不可能只有一个节点
 

二、代码

总的逻辑是:nodes-devices-channels-protocol stacks

主要参考:
7. Building Topologies — Tutorial

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#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"

#include "ns3/netanim-module.h"  

// Default Network Topology
//
//   Wifi 10.1.3.0
//                 AP
//  *    *    *    *
//  |    |    |    |    10.1.1.0
// n5   n6   n7   n0 -------------- n1   n2   n3   n4
//                   point-to-point  |    |    |    |
//                                   ================
//                                     LAN 10.1.2.0

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");

int 
main (int argc, char *argv[])
{

  bool verbose = true;
  uint32_t nCsma = 3;
  uint32_t nWifi = 3;
  bool tracing = false;

  //启用或禁用日志记录组件以及更改创建的设备数量
  CommandLine cmd (__FILE__);
  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);

  cmd.Parse (argc,argv);

  // The underlying restriction of 18 is due to the grid position
  // allocator's configuration; the grid layout will exceed the
  // bounding box if more than 18 nodes are provided.
  if (nWifi > 18)
    {
      std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl;
      return 1;
    }

  if (verbose)
    {
      LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
      LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
    }

  //1. 创建p2p信道
  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网络
  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网络
  // 创建一些由命令行参数指定的"站"节点,并使用p2p链路的"最左"节点作为接入点的节点。
  NodeContainer wifiStaNodes;
  wifiStaNodes.Create (nWifi);
  NodeContainer wifiApNode = p2pNodes.Get (0);

  // PHY layer
  //构建了wifi设备以及这些wifi节点之间的互连通道,设备底层都一样
  YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
  YansWifiPhyHelper phy;
  phy.SetChannel (channel.Create ());

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

  // MAC layer
  WifiMacHelper mac;
  Ssid ssid = Ssid ("ns-3-ssid");
  mac.SetType ("ns3::StaWifiMac",
               "Ssid", SsidValue (ssid),
               "ActiveProbing", BooleanValue (false));

  // 在MAC层和PHY层对所有站点特定的参数进行了完全配置后,
  // 用Install方法来创建这些站点的Wi-Fi设备
  // STA nodes
  NetDeviceContainer staDevices;
  staDevices = wifi.Install (phy, mac, wifiStaNodes);

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

  NetDeviceContainer apDevices;
  apDevices = wifi.Install (phy, mac, wifiApNode);

  // STA nodes: 移动
  MobilityHelper mobility;
  // 规定2维空间的初始布局
  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                                 "MinX", DoubleValue (0.0), //网格起始点所在的x坐标
                                 "MinY", DoubleValue (0.0), //网格起始点所在的y坐标
                                 "DeltaX", DoubleValue (5.0), //对象之间的x距离
                                 "DeltaY", DoubleValue (10.0),//对象之间的y距离
                                 "GridWidth", UintegerValue (3),//在一条线上布置的物体的数量
                                 "LayoutType", StringValue ("RowFirst")); // 布局类型,行先
  //具体的移动模型
  //在规定范围内随机移动
  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                             "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
  mobility.Install (wifiStaNodes);

  // AP nodes:静止
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (wifiApNode);

  // 4.安装协议栈
  InternetStackHelper stack;
  stack.Install (csmaNodes);
  stack.Install (wifiApNode);
  stack.Install (wifiStaNodes);

  // 5.分配ipv4地址
  Ipv4AddressHelper address;
  //10.1.1.0:p2p地址。
  address.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer p2pInterfaces;
  p2pInterfaces = address.Assign (p2pDevices);

  //10.1.2.0:CSMA地址
  address.SetBase ("10.1.2.0", "255.255.255.0");
  Ipv4InterfaceContainer csmaInterfaces;
  csmaInterfaces = address.Assign (csmaDevices);

  //10.1.3.0:STA设备和AP
  address.SetBase ("10.1.3.0", "255.255.255.0");
  address.Assign (staDevices);
  address.Assign (apDevices);

  // 6.安装udp
  UdpEchoServerHelper echoServer (9);

  // server服务端
  ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));
  // client客户端
  UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (10)); //总发包数
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); //发包间隔
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));//每次发包大小

  ApplicationContainer clientApps = 
    echoClient.Install (wifiStaNodes.Get (nWifi - 1));
  clientApps.Start (Seconds (1.0));
  clientApps.Stop (Seconds (10.0));

  // 7.路由协议
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  Simulator::Stop (Seconds (10.0));

  //8.启动pcap,将获得4个文件
  //1.文件" third- 0- 0.pcap "对应节点零上的点对点设备- - "主干"左侧
  //2.文件' third-1-0.pcap '对应于节点一上的点对点设备- - '主干'右侧
  //3.文件third - 0 - 1 . pcap "将是Wi - Fi网络中的混杂(监听模式)痕迹
  //4.文件third - 1 - 1 . pcap "将是CSMA网络中的混杂痕迹。
  if (tracing)
    {
      phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
      pointToPoint.EnablePcapAll ("third");
      phy.EnablePcap ("third", apDevices.Get (0));
      csma.EnablePcap ("third", csmaDevices.Get (0), true);
    }

  AnimationInterface anim("third.xml"); 
  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

采用NetAnim可视化,画面如下:

三、改动

参考知乎的帖子,卫星也采用wifi模型,third.cc里面的两个点由卫星生成,主要修改相对距离、运动范围参数

STK与NS3交互demo(python) - 知乎

    # 利用两个卫星之间的相对距离生成相对静止的两个点
    # DeltaX和Y为发射机和接收机的相对距离
    mobility = ns.mobility.MobilityHelper()
    mobility.SetPositionAllocator("ns3::GridPositionAllocator", "MinX", ns.core.DoubleValue(0),
                                  "MinY", ns.core.DoubleValue(0.0), "DeltaX", ns.core.DoubleValue(abs(X_1[i] - X_0[i])),
                                  "DeltaY",
                                  ns.core.DoubleValue(abs(Y_1[i] - Y_0[i])),
                                  "GridWidth", ns.core.UintegerValue(3000), "LayoutType",
                                  ns.core.StringValue("RowFirst"))

    mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel", "Bounds",
                              ns.mobility.RectangleValue(ns.mobility.Rectangle(-50, 5000, -50, 50)))
    mobility.Install(wifiStaNodes)


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值