我自己寫了一個mywireless.cc,主要內容為:這是一個wifi模型,一個AP節點,若干個sta節點,其中的信道channel為共享信道,也就是說可以競爭和相互影響通信。
代碼如下:
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
// Default Network Topology
//
// Number of wifi nodes can be increased up to 250
// |
// Rank 0
// ------------------
// Wifi 10.1.1.0
// AP
// * * * *
// | | | |
// n3 n2 n1 n0
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("mywireless");
int
main (int argc, char *argv[])
{
bool verbose = true; // 如果為true,則在終端輸出日志內容
uint32_t nWifi = 3; // sta節點個數
bool tracing = false; // 如果為true,則生成跟蹤檔案(.pcap)
CommandLine cmd;
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);
// Check for valid number of csma or wifi nodes
// 250 should be enough, otherwise IP addresses
// soon become an issue
if (nWifi > 250)
{
std::cout << "Too many wifi nodes, no more than 250 each." << std::endl;
return 1;
}
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
/* 創建wifi的AP節點和Sta節點 */
NodeContainer wifiApNode;
wifiApNode.Create (1); // 創建一個AP節點
NodeContainer wifiStaNodes;
wifiStaNodes.Create (nWifi); // 創建nWifi個sta節點
/* 一旦創建了這些物件,我們創建一個通道物件并將其關聯到我們的PHY層物件管理器,以確保由YansWifiPhyHelper創建的所有PHY層物件 共享相同的底層通道,即它們共享相同的無線介質,并且可以溝通與干涉 */
YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); //配置Channel助手
YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); // 配置phy助手
phy.SetChannel (channel.Create ()); // 使每一個phy與Channel相關聯
WifiHelper wifi; // 創建wifi助手,有助于創建WifiNetDevice物件
wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); // 設定wifi助手物件的速率控制演算法型別:AARF演算法
// MAC層設定。
WifiMacHelper mac; // 創建mac層
Ssid ssid = Ssid ("ns-3-ssid"); // 創建IEEE 802.11 SSID資訊元素
mac.SetType ("ns3::StaWifiMac", // 設定mac型別為"ns3::StaWifiMac"
"Ssid", SsidValue (ssid), // 設定mac的"Ssid"屬性的值為SsidValue (ssid)
"ActiveProbing", BooleanValue (false)); // 設定mac的"ActiveProbing"屬性的值為BooleanValue (false)。如果為true,我們發送探測請求。
NetDeviceContainer staDevices; // 創建WifiStaNetDevice物件
staDevices = wifi.Install (phy, mac, wifiStaNodes); // 根據phy、mac和節點集合回傳一個NetDeviceContainer物件
mac.SetType ("ns3::ApWifiMac", // 設定mac型別為"ns3::ApWifiMac"
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices; // 創建WifiApNetDevice物件
apDevices = wifi.Install (phy, mac, wifiApNode);
/* 添加移動模型,使sta節點處于移動狀態,使ap節點處于靜止狀態 */
MobilityHelper mobility; // 創建MobilityHelper物件,將位置和移動模型分配給節點
// 設定位置分配器,用于分配MobilityModel :: Install中初始化的每個節點的初始位置。
mobility.SetPositionAllocator ("ns3::GridPositionAllocator", // 設定使用的移動模型的型別(在矩形2d網格上分配位置)
"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")); // 布局型別
// 設定移動模型型別
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", // 該型別:其中節點在隨機方向上以隨機速度圍繞邊界框移動
"Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); // 界限屬性(矩形的范圍)
mobility.Install (wifiStaNodes); // 把該移動模型安裝到wifiStaNodes節點上
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); // 固定位置的模型
mobility.Install (wifiApNode);
/* 安裝協議堆疊 */
InternetStackHelper stack; // 用于將IP / TCP / UDP功能聚合到現有節點
stack.Install (wifiApNode);
stack.Install (wifiStaNodes);
/* 給設備介面分配IP地址 */
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer apInterfaces; // apInterfaces用于獲取ap節點的ip地址
apInterfaces = address.Assign (apDevices);
Ipv4InterfaceContainer staInterfaces; // staInterfaces用于獲取sta節點的ip地址
staInterfaces = address.Assign (staDevices);
/* 創建clien和server應用 */
UdpEchoServerHelper echoServer (9); // 創建一個埠號為9的server
ApplicationContainer serverApps = echoServer.Install (wifiApNode.Get (0)); // 把該server安裝到ap節點上
serverApps.Start (Seconds (1.0)); // server開始于程式運行的第1秒中
serverApps.Stop (Seconds (10.0)); // server結束于程式運行的第10秒中
UdpEchoClientHelper echoClient (apInterfaces.GetAddress (0), 9); // 創建client,并為其指向ap節點的ip地址和埠號
echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); // 應用程式發送的最大資料包數
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); // 資料包之間等待的時間
echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); // 出站資料包中回波資料的大小
ApplicationContainer clientApps = echoClient.Install (wifiStaNodes);
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
/* 啟用互聯網路路由 */
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Stop (Seconds (10.0));
if (tracing == true)
{
phy.EnablePcap ("mywireless", apDevices.Get (0));
phy.EnablePcap ("mywireless", staDevices);
}
AnimationInterface anim("mywireless.xml");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
運行結果:我在終端輸入$ ./waf --run "scratch/mywireless.cc --nWifi=2"之后,意思就是在這個wifi模型中有2個sta節點,一個AP節點。這個AP安裝有一個UdpEchoServer用于接受資料和反饋資訊,另外兩個sta節點分別裝有一個UdpEchoClient客戶端用于發送資料包。兩個sta節點發送資料包的時間均為2s之后,運行的結果如下圖:

問題在于:按理來講,因為channel是共享的,具有競爭和沖突的特點,所以運行結果應該是sta1節點發送資料到AP節點,AP節點接受資料然后反饋資料給sta1,sta1節點收到反饋資料之后,再讓sta2節點發送資料,然后AP節點再次接受資料反饋資料給sta2。但是,實際的運行結果似乎是sta1和sta2節點同時發送資料,然后AP節點依次接受sta1和sta2的節點的資料并回傳,最后sta1和sta2接受反饋回來的資料。
uj5u.com熱心網友回復:
請問如果是多個AP跟多個STA應該如何寫腳本uj5u.com熱心網友回復:
因為你的2個STA節點下面的start seconds都是2s,應該用回圈把時間分開轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71273.html
標籤:C++ 語言
上一篇:在線等待,c++問題
下一篇:編程實作訪問控制矩陣
