1、目录结构
example:1、根文件下自带示例结构,作为良好的参考资源
2、src环境下中各模块中example作为资源
build: 编译后文件以及可执行文件
src:各模块源代码
2、新代码运行
将新脚本放在scratch文件夹中,该目录默认在waf编译环境内。可以通过直接编译./waf运行
first代码解析
拓扑:点对点网络---最简单
1 头文件
脚本通过各个模块提供的API进行网络模拟,每个模块的API放在“模块名-module.h”下。其中core与network为必须模块。另外,脚本中使用非ns3库中函数,也需要在这一步添加
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
2 名字空间
将ns3项目与非ns3项目分离。使用标准库函数需要添加std名字空间,如
using namespace ns3;
3 NS_LOG_COMPONENT_DEFINE
脚本使用宏定义打印辅助信息
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
4 int main()函数
读取命令行参数、设置模拟单元、开启log组件
Time::SetResolution (Time::NS);LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
5、网络拓扑创建
基本设置节点:Node
ns3中节点、信道、与节点中连接信道的网络设备分别对应于Node、Channel、NetDevice三个类(英文名字可以看出)。其中,信道与网络设备有着与之对应的多个子类。该网络中使用助手类构建网络(--Helper)。代码设置节点。设置PPP【点对点】的属性,并通过Install()在节点中安装设备。Install()返回NetDevice对象。
NodeContainer nodes;nodes.Create (2);PointToPointHelper pointToPoint;pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));NetDeviceContainer devices;devices = pointToPoint.Install (nodes);
6、安装TCP/IP协议族
安装协议,其中包括:TCP和UDP。网络层的IP与路由协议
InternetStackHelper stack;stack.Install (nodes);Ipv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");Ipv4InterfaceContainer interfaces = address.Assign (devices);
通过InternetStackHelper助手安装协议栈stack,并通过Intall(nodes)安装在节点中
IP地址通过AddressHelper设置并通过Assign()函数安装在节点中设备中。
7 安装应用程序
可以有不同的应用程序协议分发模块,first采用UdpEcho应用程序。
利用服务器助手,初始化监听9号端口。利用服务器助手将其安装在其中的一个节点中,编号为1,同样使用Install()函数,并设置在1s后启动,10s后结束。
UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));
客户端助手接收来自1编号的IP地址,并从9号端口接收。MaxPackets、Interval、PacketSize分别对应echoClient的三个属性。为最大发送分组数、发送间隔与发送包裹大小(负载)。客户端安装在结点0中,在2s时开始,10s时结束。
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);echoClient.SetAttribute ("MaxPackets", UintegerValue (1));echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));echoClient.SetAttribute ("PacketSize", UintegerValue (1024));ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));
8、数据生成
first未涉及
9、启动与结束
Simulator::Run ();Simulator::Destroy ();return 0;
执行之前定义的操作。Run()按顺序执行;Destroy()执行清除操作。
以上为first中所有操作,对于整体ns3把握还不完善。需要理解过程后进一步通过案例分析。