一。程序
sample.cc (略)
/* -*- 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 * * Authors: Rohan Patidar*/// This script outputs the throughput at 6Mbps rate with respect to distace // between the nodes for UDP, // The simulation assumes a single station in an infrastructure network://
// STA AP// * *// | |// n1 n2
#include "ns3/core-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/gnuplot.h"
using namespace ns3;
int main (int argc, char *argv[])
{
std::vector <std::string> modes;
modes.push_back ("OfdmRate6Mbps");
modes.push_back ("OfdmRate12Mbps");
modes.push_back ("OfdmRate18Mbps");
modes.push_back ("OfdmRate24Mbps");
modes.push_back ("OfdmRate36Mbps");
modes.push_back ("OfdmRate48Mbps");
modes.push_back ("OfdmRate54Mbps");
double simulationTime = 5; //seconds
bool shortGuardInterval = false;
for (uint32_t i = 0; i < modes.size (); i++) //MCS
{
std::cout << modes[i] << std::endl;
uint32_t payloadSize; //1500 byte IP packet
payloadSize = 1472; //bytes
NodeContainer wifiStaNode;
wifiStaNode.Create (1);
NodeContainer wifiApNode;
wifiApNode.Create (1);
// Set channel type
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
// Set guard interval
phy.Set ("ShortGuardEnabled", BooleanValue (shortGuardInterval));
//
WifiMacHelper mac;
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue (modes[i]));
Ssid ssid = Ssid ("ns3-80211a");
mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid));
NetDeviceContainer staDevice;
staDevice = wifi.Install (phy, mac, wifiStaNode);
mac.SetType ("ns3::ApWifiMac","Ssid", SsidValue (ssid));
NetDeviceContainer apDevice;
apDevice = wifi.Install (phy, mac, wifiApNode);
// mobility.
MobilityHelper mobility;
PtrpositionAlloc = CreateObject();
positionAlloc->Add (Vector (0.0, 0.0, 0.0));
positionAlloc->Add (Vector (10, 0.0, 0.0));
mobility.SetPositionAllocator (positionAlloc);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiApNode);
mobility.Install (wifiStaNode);
/* Internet stack*/
InternetStackHelper stack;
stack.Install (wifiApNode);
stack.Install (wifiStaNode);
Ipv4AddressHelper address;
address.SetBase ("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer staNodeInterface;
Ipv4InterfaceContainer apNodeInterface;
staNodeInterface = address.Assign (staDevice);
apNodeInterface = address.Assign (apDevice);
/* Setting applications */
ApplicationContainer serverApp;
//UDP flow
uint16_t port = 9;
UdpServerHelper server (port);
serverApp = server.Install (wifiStaNode.Get (0));
serverApp.Start (Seconds (0.0));
serverApp.Stop (Seconds (simulationTime + 1));
UdpClientHelper client (staNodeInterface.GetAddress (0), port);
client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
clientApp.Start (Seconds (1.0));
clientApp.Stop (Seconds (simulationTime + 1));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Stop (Seconds (simulationTime + 1));
Simulator::Run ();
Simulator::Destroy ();
double throughput = 0;
uint64_t totalPacketsThrough = DynamicCast(serverApp.Get (0))->GetReceived ();
throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); //Mbit/s
std::cout << "Throughput: " << throughput << " Mbit/s" << std::endl;
}
return 0;
}
二. 运行结果