编译、运行脚本和Command Line命令行参数设置

来源:互联网 发布:雷蛇键盘 mac 编辑:程序博客网 时间:2024/04/26 06:38

出处:http://blog.csdn.net/finded/article/details/45337725




7. 编译和运行脚本主要步骤

1) 将编写的脚本复制到ns-3.22/scratch目录下(可以在ubuntu窗口界面直接复制)

进入ns3目录: /ns-3.22

cp examples/tutorial/first.cc  scratch/myfirst.cc将脚本复制到scratch目录下

2) 构建(编译)

./waf

3) 运行

./waf --run scratch/myfirst

(可能会有运行权限问题,可在root下运行)

7.1  ns3中first.cc例子注释

[cpp] view plain copy
  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. }  

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

参考文献:

[1] http://blog.sina.com.cn/s/articlelist_1923709774_8_1.html

[2] ns-3project. ns-3 Software Tutorial. ns-3 project. 2015年2月26日

[3] ns-3project. ns-3 Reference Manual. ns-3 project. 2015年2月26日

[4]《ns-3网络模拟器基础及应用》,人民邮电出版社,马春光 姚建盛,2014年1月

部分参考网络资料,未一一列举,敬请原谅!

阅读全文
0 0
原创粉丝点击