码迷,mamicode.com
首页 > 其他好文 > 详细

NS3实例分析(4)-- third.cc

时间:2015-04-24 09:08:25      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:ns3

这一节主要是分析第三个例子 third.cc。 该例子包含了P2P信道,以太信道和WiFi信道。

网络拓扑如下:

// 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


接下来我们来分析一下 third.cc 源码的实现:

--------------------------------------------------------------------------------------------

#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"

using namespace ns3;

//声明了一个叫SecondScriptExample的日志构件,可以实现打开或者关闭控制台日志的输出。
NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");

int main (int argc, char *argv[])
{
  //定义变量,用于决定是否开启两个UdpApplication的Logging组件;默认true开启
  bool verbose = true;
  uint32_t nCsma = 3;
  uint32_t nWifi = 3;

  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.Parse (argc,argv);

  if (nWifi > 18)
    {
      std::cout << "Number of wifi nodes " << nWifi <<
                   " specified exceeds the mobility bounding box" << std::endl;
      exit (1);
    }

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

  /********************网络拓扑部分************************/
  //创建使用P2P链路链接的2个节点
  NodeContainer p2pNodes;
  p2pNodes.Create (2);

  //设置传送速率和信道延迟
  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  //安装P2P网卡设备到P2P网络节点
  NetDeviceContainer p2pDevices;
  p2pDevices = pointToPoint.Install (p2pNodes);

  //创建NodeContainer类对象,用于总线(CSMA)网络
  NodeContainer csmaNodes;
  //将第二个P2P节点添加到CSMA的NodeContainer
  csmaNodes.Add (p2pNodes.Get (1));
  //创建Bus network上另外3个node
  csmaNodes.Create (nCsma);

  //创建和连接CSMA设备及信道
  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

  //安装网卡设备到CSMA信道的网络节点
  NetDeviceContainer csmaDevices;
  csmaDevices = csma.Install (csmaNodes);

  //创建NodeContainer类对象,用于WiFi网络
  NodeContainer wifiStaNodes;
  wifiStaNodes.Create (nWifi);
  //设置WiFi网络的第一个节点为AP
  NodeContainer wifiApNode = p2pNodes.Get (0);

  //初始化物理信道
  YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
  YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
  phy.SetChannel (channel.Create ());

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

  //Mac层设置
  NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();

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

  //安装网卡设备到WiFi信道的网络节点,并配置参数
  NetDeviceContainer staDevices;
  staDevices = wifi.Install (phy, mac, wifiStaNodes);

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

  ////安装网卡设备到WiFi信道的AP节点,并配置参数
  NetDeviceContainer apDevices;
  apDevices = wifi.Install (phy, mac, wifiApNode);

  //添加移动模型
  MobilityHelper mobility;

  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"));

  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                             "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
  //在STA节点上安装移动模型
  mobility.Install (wifiStaNodes);

  //设置AP:固定在一个位置上
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (wifiApNode);

  //安装网络协议
  InternetStackHelper stack;
  stack.Install (csmaNodes);
  stack.Install (wifiApNode);
  stack.Install (wifiStaNodes);

  Ipv4AddressHelper address;

  //安排P2P网段的地址
  address.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer p2pInterfaces;
  p2pInterfaces = address.Assign (p2pDevices);

  //安排csma网段的地址
  address.SetBase ("10.1.2.0", "255.255.255.0");
  Ipv4InterfaceContainer csmaInterfaces;
  csmaInterfaces = address.Assign (csmaDevices);

  //安排wifi网段的地址
  address.SetBase ("10.1.3.0", "255.255.255.0");
  address.Assign (staDevices);
  address.Assign (apDevices);
  /********************网络拓扑部分结束*********************/

  /**********************应用程序部分*********************/
  UdpEchoServerHelper echoServer (9);

  //将Server服务安装在CSMA网段的最后一个节点上
  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));

  //将Client应用安装在WiFi网段的倒数第二个节点上
  ApplicationContainer clientApps =
    echoClient.Install (wifiStaNodes.Get (nWifi - 1));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

  /****************调用全局路由Helper帮助建立网络路由*******************/
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  Simulator::Stop (Seconds (10.0));

  /****************开启pcap跟踪*******************/
  pointToPoint.EnablePcapAll ("third");
  phy.EnablePcap ("third", apDevices.Get (0));
  csma.EnablePcap ("third", csmaDevices.Get (0), true);

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}


-----------------------------------------------------------------------------------------------


运行结果如下:

技术分享


注意:

1、YansWifiChannelHelper:
YansWifiPhyHelper共享相同的底层信道,也就是说,它们共享相同的无线介质,可以相互通信。

2、NqosWifiMacHelper
使用NqosWifiMacHelper对象设置MAC参数,表示使用没有QoS保障的Mac层机制。

3、RandomWalk2dMobilityModel
表示在一个边界框中,节点以一个随机的速度在一个随机方向上移动

NS3实例分析(4)-- third.cc

标签:ns3

原文地址:http://blog.csdn.net/liuruiqun/article/details/45228081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!