1.网络结构
这一个网络结构相比与second,多了一个wifi子网。
这里把second的n0变成了一个无线AP
2.程序
/* -*- 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/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.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;
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);
// The underlying restriction of 18 is due to the grid position
// allocator's configuration; the grid layout will exceed the
// bounding box if more than 18 nodes are provided.
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);
}
//上面的是一些初始设置,这里进入正式的网络结构
//创建2个P2P节点,并设置信道,安装网卡
NodeContainer p2pNodes;
p2pNodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
//创建4个csma节点,其中一个兼有p2p和csma,并设置信道,安装网卡
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);
//创建4个wifi节点,其中一个兼有p2p和wifi,并设置信道,安装网卡
NodeContainer wifiStaNodes;
wifiStaNodes.Create (nWifi);
//将p2p第一个节点n0设置为wifi的Ap结点
NodeContainer wifiApNode = p2pNodes.Get (0);
//配置PHY(物理层)和Channel通道助手
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
//把channel和phy关联
phy.SetChannel (channel.Create ());
//远程基站管理,使用AARF速率控制算法,调节速率,以增加网络速率。
WifiHelper wifi;
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
//配置wifi网络STA网络设备的MAC类型和基础设施网络的SSID
WifiMacHelper mac;
Ssid ssid = Ssid ("ns-3-ssid"); //IEEE 802.1服务集标识符对象
mac.SetType ("ns3::StaWifiMac", //设置为特定种类MAC层,non-AP
"Ssid", SsidValue (ssid), //设置SSID
"ActiveProbing", BooleanValue (false)); //设置ActiveProbing,false意味着助手创建的mac将不会发送探测请求。
//为wifi节点安装网路设备
NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes);
//设置mac层,并为Ap节点安装网络设备
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiApNode);
//创建移动模型
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),//起始x坐标
"MinY", DoubleValue (0.0),//起始y坐标
"DeltaX", DoubleValue (5.0),//x轴上节点距离
"DeltaY", DoubleValue (10.0),//y轴上节点距离
"GridWidth", UintegerValue (3),//一行节点最大数量
"LayoutType", StringValue ("RowFirst")//布局类型
);//设置位置分配器
//为wifi移动节点设置移动特性
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
//移动的一些参数具体解释:“Bounds”,“运动的范围”;“Time”,“走多久换一次方向”,TimeValue (Seconds (1.0));Distance”,"走多远换一次方向"DoubleValue (1.0);“Mode”,“Time or Distance”,EnumValue (RandomWalk2dMobilityModel::MODE_DISTANCE) ;“Direction”,"随机方向 (radians)."StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=6.283184]”);“Speed”“随机速度 (m/s).”,StringValue (“ns3::UniformRandomVariable[Min=2.0|Max=4.0]”)
mobility.Install (wifiStaNodes);
//为Ap节点设置移动特性
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);
ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
//客户端设置
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));//将客户端安装在WiFi的倒数第2个节点。
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
//创建路由
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Stop (Seconds (10.0));
if (tracing == true)
{
pointToPoint.EnablePcapAll ("third");//追踪所有p2p节点的pcap
phy.EnablePcap ("third", apDevices.Get (0));//追踪ap节点的pcap
csma.EnablePcap ("third", csmaDevices.Get (0), true);追踪csma的第一个节点的pcap
}
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
wifi网络结构
里面有几个大家非常陌生的配置,这里详细说一下
1)YansWifiPhy
这个代表天线的物理参数,比如 TxGain, RxGain, RxRoiseFigure 等。
从文中也可以看到,我们在使用它时,也用到了channel,这是因为该应用依赖于 YansWifiChannel 中的两个成员变量 PropagationDelayModel (无线电的传播模型,光速c)和 PropagationLossModel(无线电的传播衰减模型)。