NS3基本核心
·
节点 Node
- NS-3将基本的计算设备抽象为Node 具体表现为C++的Node类
- Node类提供了很多方法来模拟计算设备在计算机网络中的行为
- Node的概念对应于host, end system
- 把Node看成一台计算机,需要向计算机内添加软硬件功能部件:应用程序,协议栈,计算机外设等
网络设备 NetDevice
-
net device 被安装到Node中,使当前Node能够和其他Node构建网络(借助Channel)
-
网络设备抽象用类 NetDevice 表示。
-
NetDevice 类提供连接 节点和信道的方法,是协议栈和信道之间的接口,节点只有绑定网络设备后才 具有通信功能,一个节点可以像真实的计算机一样绑定多个网络设备(multiple NetDevices)同时连接到多条信道上。
-
NS-3 中典型的网络设备,如 CsmaNetDevice、PointToPointNetDevice 和 WifiNetDevice 。
- CsmaNetDevice 被设计为在 CSMA 信道中工作,而 PointToPointNetDevice 在 PointToPoint 信道中工作,WifiNetNevice 在 wifi 信道 中工作。
信道 Channel
-
模拟世界:Node <----> Channel
-
Channel类提供相应的方法来管理通信子网对象,将节点连接到通信子网中
-
Channel 类提供管理通信子网对象和与节点相连的方法,用来模拟信号在传输过程中的变化特性。如传播时延、能量损耗、 噪声干扰、误码率等。
-
常见的Channel
- CsmaChannel:可以建模实现csms的通信子网
- PointToPointChannel
- WifiChannel
应用程序 Application
- 应用程序Application是运行在Node内部的用户软件
- 应用程序是用户程序产生模拟活动的最基本的抽象,来产生和消费网络流量,驱动网络模拟 器
NS-3中有很多Application类的子类:
-
应用程序包含了一个 client/server 应用来产生和回应(发 ACK)模拟过程的网络 数据包
- UdbEchoClientApplication
- UdbEchoServerApplication
拓扑辅助类 Topology
NS-3 中提供了一类拓扑生成器模块,对应每种拓扑连接有不同的 Helper(例如 CsmaNetHelper 等),使用这些类来模拟现实中的安 装网络设备、连接、配置链路等过程,拓扑生成器可以使这些工作变得简单, 它将这些分开的操作整合成一个使用起来简单的模型。
为什么需要Helpers类?
核心:简化网络拓扑配置,将那些机械的重复性的配置操作用Helper实现
例子
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#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"
using namespace ns3;
//日志组件
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
// 能够通过命令行参数往脚本中传递数据
CommandLine cmd;
cmd.Parse (argc, argv);//读取命令行参数
// 最小时间单元:ns
Time::SetResolution (Time::NS);
// 激活日志
// LOG_LEVEL_INFO 控制日志输出多少
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
// 创建节点
NodeContainer nodes;
nodes.Create (2); // 两个空节点
// 点到点的信道
PointToPointHelper pointToPoint;
//设置参数
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
// 将节点安装到设备上
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
// 协议
InternetStackHelper stack;
stack.Install (nodes);
// IP地址
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// 端口 9
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// 客户端
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));
// 运行
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
更多推荐

所有评论(0)