NS3實例分析(3)-- second.cc


這一小節我們來看看第三個例子,這個例子應用了一個P2P信道和一個實現CSMA的以太信道。

網絡拓撲如下:

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1   n2   n3   n4
//    point-to-point  |    |    |    |
//                    ================
//                      LAN 10.1.2.0


接下來,分析一下 second.cc  的源碼實現:

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

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"

using namespace ns3;

//聲明了一個叫SecondScriptExample的日志構件,通過引用SecondScriptExample這個名字的操作,
//可以實現打開或者關閉控制台日志的輸出。
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

int main (int argc, char *argv[])
{
  //定義變量,用於決定是否開啟兩個UdpApplication的Logging組件;默認true開啟
  bool verbose = true;
  uint32_t nCsma = 3;        

  CommandLine cmd;
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
  //命令行參數設置是否開啟logging
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);

  cmd.Parse (argc,argv);

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

  nCsma = nCsma == 0 ? 1 : nCsma;

  /********************網絡拓撲部分************************/
  //創建使用P2P鏈路鏈接的2個node
  NodeContainer p2pNodes;
  p2pNodes.Create (2);

  //創建另一個NodeContainer類對象,用於總線(CSMA)網絡
  NodeContainer csmaNodes;
  //將之前P2P的NodeContianer的第二個節點添加到CSMA的NodeContainer,
  //以獲得CSMA device;這個node將會有兩個device
  csmaNodes.Add (p2pNodes.Get (1));
  //再創建Bus network上另外3個node
  csmaNodes.Create (nCsma);

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

  //安裝P2P網卡設備到P2P網絡節點
  NetDeviceContainer p2pDevices;
  p2pDevices = pointToPoint.Install (p2pNodes);

  //創建和連接CSMA設備及信道
  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

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

  //安裝網絡協議
  InternetStackHelper stack;
  //P2P鏈路中的第一個節點
  stack.Install (p2pNodes.Get (0));
  //P2P鏈路中的第二個節點
  stack.Install (csmaNodes);

  //兩個網段的IP地址類對象
  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);
  /********************網絡拓撲部分結束*********************/

  /**********************應用程序部分*********************/
  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服務安裝在P2P網段的第一個節點上
  ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));
  /**********************應用程序部分結束*********************/

  /****************調用全局路由Helper幫助建立網絡路由*******************/
  //全局路由管理器根據節點產生的鏈路通告為每個節點建立路由表
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  /****************開啟pcap跟蹤*******************/
  //開啟P2PHelper類對象的pcap
  pointToPoint.EnablePcapAll ("second");
  //開啟csmaHelper類對象的pcap
  //使用csma網段第二個節點進行sniff,True開啟混雜模式
  csma.EnablePcap ("second", csmaDevices.Get (1), true);

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

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

編譯並運行,結果如下:

 

 


需要注意的有幾點:

1、CsmaChannel信道模擬了用於一個可以實現載波偵聽多路訪問通信子網中的媒介。這個信道具有和以太網相似的功能。

2、NetDevice類提供了管理連接其他節點和信道對象的各種方法,並且允許開發者以面向對象的方法來自定義。本例中用到了CsmaNetDevice和PointToPointNetDevice。CsmaNetDevice被設計成在csma信道中工作,而PointToPointNetDevice 在PointToPoint信道中工作。以后還會遇見WifiNetNevice,這是在wifi信道中工作。

3、使用了全局路由管理器,根據節點產生的鏈路通告為每個節點建立路由表

4、開啟了網絡嗅探器
————————————————
版權聲明:本文為CSDN博主「左手碼農」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/liuruiqun/article/details/45226683


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM