首页 > 其他分享 >ns-3_ Day 5

ns-3_ Day 5

时间:2022-12-20 12:55:44浏览次数:34  
标签:ns3 point cmd ns module AddValue include Day

无线网络仿真

examples/tutorial/third.cc

前两个例子中涉及到了P2P网络结构和CSMA网络结构,这个例子在second.cc的基础上加入3台WIFI节点(n5、n6、n7):

#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/ssid.h"

// Default Network Topology
//
//   Wifi 10.1.3.0
//                 AP
//  *    *    *    *
//  |    |    |    |    10.1.1.0
// n5   n6   n7   n0 -------------- n1   n2   n3   n4
//                   point-to-point  |    |    |    |
//                                   ================
//                                     LAN 10.1.2.0

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");

int main (int argc, char *argv[])
{
	bool verbose = true;
	uint32_t nCsma = 3;
	uint32_t nWifi = 3;
	bool tracing = false;

	CommandLine cmd (__FILE__);
	cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
	cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
	cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
	cmd.AddValue ("tracing", "Enable pcap tracing", tracing);

	cmd.Parse (argc, argv);

	if (nWifi > 18)
    {
		std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box"
					<< std::endl;
		return 1;
    }

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

	NodeContainer p2pNodes;
	p2pNodes.Create (2);

	PointToPointHelper pointToPoint;
	// 设置设备的发射速率和信道延迟
	pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
	pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

	NetDeviceContainer p2pDevices;
	p2pDevices = pointToPoint.Install (p2pNodes);

	NodeContainer csmaNodes;
	csmaNodes.Add (p2pNodes.Get (1));
	csmaNodes.Create (nCsma);

	CsmaHelper csma;
	csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
	csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

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

	NodeContainer wifiStaNodes;
	wifiStaNodes.Create (nWifi);
	NodeContainer wifiApNode = p2pNodes.Get (0);

	// 构建无线设备与无线节点之间的互连通道
	YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
	YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
	// 把Channel关联到物理层对象,确保所有物理层共享底层信道
	phy.SetChannel (channel.Create ());

	WifiHelper wifi;
	// AARF Adaptive Auto Rate Fallback 算法(是一种wifi速率控制算法)
	wifi.SetRemoteStationManager ("ns3::AarfWifiManager");

	// 配置MAC类型和基础设施网络的SSID(用于区分不同的WIFI网络)
	WifiMacHelper mac;
	Ssid ssid = Ssid ("ns-3-ssid");
	mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid), "ActiveProbing", BooleanValue (false));

	// WIFI场景下的STA节点和AP节点:
	// 1. STA即station,指接入无线网络的设备
	// 2. AccessPoint即无线网络的创建者和中心节点,常常指的是路由器
	NetDeviceContainer staDevices;
	staDevices = wifi.Install (phy, mac, wifiStaNodes);

	mac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid));

	NetDeviceContainer apDevices;
	apDevices = wifi.Install (phy, mac, wifiApNode);

	// 加入移动模型
	MobilityHelper mobility;

	// 使用二维网络放置最初的STA节点
	mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (0.0), "MinY",
									DoubleValue (0.0), "DeltaX", DoubleValue (5.0), "DeltaY",
									DoubleValue (10.0), "GridWidth", UintegerValue (3), "LayoutType",
									StringValue ("RowFirst"));

	// 节点以随机游走的方式移动,
	mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
	mobility.Install (wifiStaNodes);

	// 接入点在模拟过程中保持在固定位置
	mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
	mobility.Install (wifiApNode);

	// 安装网络协议栈
	InternetStackHelper stack;
	stack.Install (csmaNodes);
	stack.Install (wifiApNode);
	stack.Install (wifiStaNodes);

	Ipv4AddressHelper address;

	address.SetBase ("10.1.1.0", "255.255.255.0");
	Ipv4InterfaceContainer p2pInterfaces;
	p2pInterfaces = address.Assign (p2pDevices);

	address.SetBase ("10.1.2.0", "255.255.255.0");
	Ipv4InterfaceContainer csmaInterfaces;
	csmaInterfaces = address.Assign (csmaDevices);

	address.SetBase ("10.1.3.0", "255.255.255.0");
	address.Assign (staDevices);
	address.Assign (apDevices);

	UdpEchoServerHelper echoServer (9);

	// 在CSMA最右端放置echo服务端程序
	ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
	serverApps.Start (Seconds (1.0));
	serverApps.Stop (Seconds (10.0));

	// echo客户端程序装在STA节点
	UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
	echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
	echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
	echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

	ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1));
	clientApps.Start (Seconds (2.0));
	clientApps.Stop (Seconds (10.0));

	// 启用互联网的全局路由
	Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

	Simulator::Stop (Seconds (10.0));

	if (tracing == true)
	{
		// 在P2P网络(骨干网络)上进行PCAP Tracing
		// 在WIFI网络和CSMA网络上进行混杂模式Tracing
		pointToPoint.EnablePcapAll ("third");
		phy.EnablePcap ("third", apDevices.Get (0));
		csma.EnablePcap ("third", csmaDevices.Get (0), true);
	}

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

观察WIFI网络的构建过程,在基本的 Node-Topology-Device框架下,还增加了很多细节:

  • 物理通道和信道的绑定
  • 配置MAC类型和SSID
  • 节点需分为STA和AP两种
  • 加入移动模型

在完成网络设备的创建后,就可以像P2P和CSMA一样安装网络协议栈、进入IP配置阶段并安装应用程序。
image.png

标签:ns3,point,cmd,ns,module,AddValue,include,Day
From: https://www.cnblogs.com/leewaytang/p/16993952.html

相关文章