NS3如何編譯、運行腳本(以first.cc為例,其中first.cc有詳細注解)以及調試中用 Command Line命令行改變參數設置


1) 將編寫的腳本復制到ns-3.27/scratch目錄下(可以在ubuntu窗口界面直接復制)

進入ns3目錄: /ns-3.27

$ cp examples/tutorial/first.cc  scratch/myfirst.cc將腳本復制到scratch目錄下

2) 構建(編譯)

$ ./waf

3) 運行

$ ./waf --run scratch/myfirst

(可能會有運行權限問題,可在root下運行)

 

 ns3中first.cc例子注釋

 

  1.  
    //first.cc: 2個節點間的點到點通信
  2.  
     
  3.  
    //頭文件包含
  4.  
    #include "ns3/core-module.h"
  5.  
    #include "ns3/network-module.h"
  6.  
    #include "ns3/internet-module.h"
  7.  
    #include "ns3/point-to-point-module.h"
  8.  
    #include "ns3/applications-module.h"
  9.  
     
  10.  
    //ns3命名空間
  11.  
    using namespace ns3;
  12.  
     
  13.  
    //日志定義
  14.  
    NS_LOG_COMPONENT_DEFINE ( "FirstScriptExample");
  15.  
     
  16.  
    //主函數
  17.  
    int
  18.  
    main (int argc, char *argv[])
  19.  
    {
  20.  
    //時間分辨率
  21.  
    Time::SetResolution (Time::NS);
  22.  
    //使日志組件生效:組件--UdpEchoClientApplication--級別INFO
  23.  
    LogComponentEnable ( "UdpEchoClientApplication", LOG_LEVEL_INFO);
  24.  
    LogComponentEnable ( "UdpEchoServerApplication", LOG_LEVEL_INFO);
  25.  
     
  26.  
    //********生成網絡節點********
  27.  
    // 可以簡單看成部分掏空通信部分內容的計算機,可以加入協議棧、應用及外設的網卡等。
  28.  
    //節點容器類(包含許多方法):是一個helper幫助類,能夠一次操作多個節點。
  29.  
    //如:利用其對象(變量)作為設備helper類對象的參數,可以一次在安裝設備到多個節點。
  30.  
    NodeContainer nodes;
  31.  
    nodes.Create ( 2);//利用該容器類的創建節點方法,創建兩個節點
  32.  
     
  33.  
    //********物理連接計算機********
  34.  
    // 抽象:物理實體=網絡設備+信道,兩者一一對應。
  35.  
    PointToPointHelper pointToPoint; //點到點通信助手類,通過所包含方法能設置網絡設備和信道屬性
  36.  
    pointToPoint.SetDeviceAttribute ( "DataRate", StringValue ("5Mbps"));//調用成員函數
  37.  
    pointToPoint.SetChannelAttribute ( "Delay", StringValue ("2ms"));
  38.  
     
  39.  
    //網絡設備容器,即安裝了網絡設備和信道的節點
  40.  
    NetDeviceContainer devices;
  41.  
    devices = pointToPoint.Install (nodes);
  42.  
     
  43.  
    //********安裝協議棧********//
  44.  
    InternetStackHelper stack;//網絡協議棧幫助類。屬於拓撲幫助類
  45.  
    stack.Install (nodes);//為每個節點安裝協議棧,IP層
  46.  
    //ipv4地址幫助類,屬於拓撲幫助類
  47.  
    Ipv4AddressHelper address;
  48.  
    address.SetBase ( "10.1.1.0", "255.255.255.0");
  49.  
    //ipv4接口容器類,為了以后引用方便,給網絡設備容器類配置地址。結果存在ipv4接口容器類的對象中。
  50.  
    Ipv4InterfaceContainer interfaces = address.Assign (devices);
  51.  
     
  52.  
    //********安裝應用層********//
  53.  
    // UDP服務器設置
  54.  
    UdpEchoServerHelper echoServer (9);
  55.  
     
  56.  
    ApplicationContainer serverApps = echoServer.Install (nodes.Get ( 1));
  57.  
    serverApps.Start (Seconds ( 1.0));
  58.  
    serverApps.Stop (Seconds ( 10.0));
  59.  
    // UDP客戶機
  60.  
    UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);//遠端服務器地址和端口
  61.  
    echoClient.SetAttribute ( "MaxPackets", UintegerValue (1));
  62.  
    echoClient.SetAttribute ( "Interval", TimeValue (Seconds (1.0)));
  63.  
    echoClient.SetAttribute ( "PacketSize", UintegerValue (1024));
  64.  
     
  65.  
    ApplicationContainer clientApps = echoClient.Install (nodes.Get ( 0));
  66.  
    clientApps.Start (Seconds ( 2.0));
  67.  
    clientApps.Stop (Seconds ( 10.0));
  68.  
    //沒有加入Simulation::Stop(Seconds(11.0));仿真結束時間
  69.  
    //因為first.cc例子的事件隊列中事件會自動操作完;
  70.  
    //對於一直有事件產生的仿真(類似操作系統),必須設置仿真結束時間
  71.  
    Simulator::Run ();
  72.  
    Simulator::Destroy ();
  73.  
    return 0;
  74.  
    }

 

二、CommandLine命令行參數

 

仿真一般是為了收集各種不同條件下的數據,常常需要改變一些變量。NS-3提供了Command Line參數接口,可以在運行時對腳本中的變量進行設置,免去了每次更改變量后要重新編譯和構建腳本的麻煩。

1) 修改已有屬性變量

在腳本中添加語句

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

{

...

CommandLine cmd;

cmd.Parse (argc, argv);//將命令行輸入的參數作為類CommandLine的參數進行分析

...

}

這樣可以在shell中使用某些附加參數如PrintHelp:

$~/ns-3.2.1 > ./waf --run "scratch/example --PrintHelp"

這條命令將會列出example當前可用的命令參數:

   Entering directory '/home/craigdo/repos/ns-3-dev/build'

   Compilation finished successfully

   --PrintHelp: Print this help message.

   --PrintGroups: Print the list of groups.

   --PrintTypeIds: Print all TypeIds.

   --PrintGroup=[group]: Print all TypeIds of group.

   --PrintAttributes=[typeid]: Print all attributes of typeid.

   --PrintGlobals: Print the list of globals.

從輸出中(倒數第二行)我們知道可以打印某些類的屬性:

$~/ns-3.2.1 > ./waf --run "scratch/example --PrintAttributes=ns3::PointToPointNetDevice"

這條命令將會列出類型為PointToPointNetDevice的設備的屬性:

   --ns3::PointToPointNetDevice::DataRate=[32768bps]:

The default data rate for point topoint links

知道了屬性名稱,我們也可以使用命令更改這個屬性:前提須把腳本中賦值語句注釋/刪除

$~/ns-3.2.1>./waf--run"scratch/example --ns3::PointToPointNetDevice::DataRate=5Mbps"

2) 添加自己的變量

使用CommandLine::AddValue添加自己的變量,通過鈎掛自己的變量將其與命令行相關聯,使之成為CommandLine可以使用的參數,

在腳本中main函數開始添加

CommandLinecmd;

cmd.AddValue("nPackets","Number of packets to echo", nPackets); //(屬性名稱,屬性說明,變量)

cmd.Parse(argc,argv);

  這樣在shell中我們可以在命令中更改這個屬性:

$~/ns-3.2.1 > ./waf --run "scratch/example --nPackets=2"

 


免責聲明!

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



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