/* -*- 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"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");
int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
bool verbose = true;
uint32_t nWifi = 3; //設置 無線站點個數
bool tracing = false;
CommandLine cmd;
cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
//用命令行修改參數
cmd.Parse (argc,argv);
if (nWifi > 250)
{
std::cout << "Too many wifi or csma nodes, no more than 250 each." << std::endl;
return 1;
}
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_ALL);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_ALL);
}
NodeContainer wifiStaNodes;
wifiStaNodes.Create (nWifi); //創建無線站點STA的節點
NodeContainer wifiApNode;
wifiApNode.Create(1); //創建一個AP節點
YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); //使用默認的信道模型
YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); //使用默認的PHY模型
phy.SetChannel (channel.Create ()); //創建通道對象並把他關聯到物理層對象管理器
WifiHelper wifi;
wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); //設置wifi助手用AARF速率控制算法
//配置Mac類型以及基礎設置SSID
WifiMacHelper mac;
Ssid ssid = Ssid ("ns-3-aqiao"); //設置SSID的名字為ns-3-aqiao
mac.SetType ("ns3::StaWifiMac", //指定mac層指定為ns3::StaWifiMac
"Ssid", SsidValue (ssid), //設置默認AP是ssid對象
"ActiveProbing", BooleanValue (false)); //設置不會發出主動探測AP的指令,默認AP是ssid
NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes); //在MAC層和PHY層用以前安裝方法來創建這些無線設備
mac.SetType ("ns3::ApWifiMac", //指定為AP
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiApNode); //一起共享PHY層的屬性
// 加入移動模型,讓STA可以移動,AP固定
MobilityHelper mobility;
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", //設置移動模式為"ns3::RandomWalk2dMobilityModel",隨機移動
"Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); //移動范圍
mobility.Install (wifiStaNodes);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); //設置AP的不能夠移動的,"ns3::ConstantPositionMobilityModel"
mobility.Install (wifiApNode);
InternetStackHelper stack;
stack.Install (wifiApNode);
stack.Install (wifiStaNodes); //安裝協議棧
Ipv4AddressHelper address;
address.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer wifiInterfaces;
wifiInterfaces=address.Assign (staDevices);
address.Assign (apDevices); //地址分配
//安裝暗涌程序,使用UDP,與之前的UDP的使用方法一致
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (wifiStaNodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (wifiInterfaces.GetAddress (0), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (5));
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));
phy.EnablePcap ("wifiwifi", apDevices.Get (0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
輸出結果
發現與之前的UDP傳送成功。
利用該命令可以,可以查看代碼中更詳細pcap內容
這個部分主要完成的是各個STA接入AP的控制信息,由於無線網絡中,並不能確定信息是否收發成功,所以一定要接受到一個ACK,表示數據發送成功
接下來是ARP協議通過 IP地址去得到對方的MAC地址,以便於后面可以實現基於mac層的802.11數據傳輸
已經得到了MAC地址后,后面就不需要再進行ARP的詢問了。
通過VIS得到的可視化結果
