1
我目前正在嘗試使用網絡模擬器NS3來執行一個固定接入點向移動節點(使用802.11a,TCP)傳輸數據的測試。NS3吞吐量計算
該代碼似乎編譯得很好,而且之前輸出的是正確的數據,所以我可以繪製移動節點的吞吐量與其到AP的距離,然後AP的調製方案如何根據移動節點。
我對ThroughputPerSecond方法做了一個小改動,現在仿真停留在一個無限循環(它不會在240秒後停止)並且只輸出全零。我很沮喪地看過過去6個小時試圖修改一些小東西的代碼,我想也許有人比我更有經驗可以看看它,看看我是否缺少明顯的東西?
我附上了所有的代碼,但我認爲問題只在於小的ThroughputPerSecond方法,儘管我無法理解它在哪裏。
任何幫助,非常感謝。
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/athstats-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/internet-module.h"
#include <iostream>
using namespace ns3;
static bool g_verbose = true;
void
PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
{
if (g_verbose)
{
// Output the data rates for each data packet received
std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
}
}
static void
SetPosition (Ptr<Node> node, Vector position)
{
// Set node's initial position
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
mobility->SetPosition (position);
}
static Vector
GetPosition (Ptr<Node> node)
{
// get node's current position
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
return mobility->GetPosition();
}
static void
AdvancePosition (Ptr<Node> node)
{
// advance node's position
Vector pos = GetPosition (node);
pos.x += 1.0;
if (pos.x >= 120.0){
return;
}
SetPosition (node, pos);
if (g_verbose){
//std::cout << "x="<<pos.x << std::endl;
}
// Reschedule AdvancePosition
Simulator::Schedule (Seconds (0.1), &AdvancePosition, node);
}
void
ThroughputPerSecond (Ptr<Application> sink1Apps, int totalPacketsThrough, Ptr<Node> node)
{
double throughput = 0.0;
Ptr<PacketSink> sink1 = DynamicCast<PacketSink> (sink1Apps);
// Calculate and output throughput per second
totalPacketsThrough = sink1->GetTotalRx();
throughput = totalPacketsThrough*8/((237.0)*1000000.0);
std::cout << throughput;
// Reschedule ThroughputPerSecond
//
Simulator::Schedule (Seconds (0.0), &ThroughputPerSecond, sink1Apps, totalPacketsThrough, node);
}
int main (int argc, char *argv[])
{
Packet::EnablePrinting();
CommandLine cmd;
cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
cmd.Parse (argc, argv);
// disable RTS/CTS.
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("99999999"));
// disable fragmentation
Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2222200"));
WifiHelper wifi = WifiHelper::Default();
MobilityHelper mobility;
NodeContainer stas;
NodeContainer ap;
NetDeviceContainer staDevs,apDevs;
Time interPacketInterval;
// Create 1 node, playing the role of the mobile node and 1 node, playing the role of the AP
stas.Create(1);
ap.Create(1);
// Create and setup the wifi Channel, wifi physical and MAC layers for the nodes
wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
interPacketInterval = Seconds (0.00015); //802.11a & 802.11g speeds
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default();
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
wifiPhy.SetChannel (wifiChannel.Create());
Ssid ssid = Ssid ("wifi-default");
// setup the mobile node and the AP (install net devices)
wifiMac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid), "ActiveProbing", BooleanValue (false));
staDevs = wifi.Install(wifiPhy, wifiMac, stas);
wifiMac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid));
apDevs = wifi.Install(wifiPhy, wifiMac, ap);
// Setup the mobility model for the mobile node and the AP
mobility.Install (ap);
mobility.Install (stas);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
// Set initial positions of both nodes
SetPosition (ap.Get(0), Vector (0.0, 0.0, 0.0));
SetPosition (stas.Get(0), Vector (-120.0, -20.0, 0.0));
// Start moving the mobile node at Second 0.5 by calling AdvancePostion function
Simulator::Schedule (Seconds (0.5), &AdvancePosition, stas.Get(0));
// Add the Internet Stack and assign IPs for the mobile node and AP
InternetStackHelper internet;
internet.Install (ap);
internet.Install (stas);
Ipv4AddressHelper address;
address.SetBase("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer apConnection = address.Assign(apDevs);
Ipv4InterfaceContainer staConnection = address.Assign(staDevs);
//Ipv4Address serverAddress = Ipv4Address(apConnection.GetAddress(0,0));
//
// Create application pairs for TCP or UDP using information in the spec
// setting AP as the data source and the mobile node as the receiver of this data
//
uint16_t port = 9; //Typical port
BulkSendHelper source ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port));
source.SetAttribute ("MaxBytes", UintegerValue(0)); //0 is maximum
ApplicationContainer sourceApps = source.Install(ap.Get(0));
sourceApps.Start (Seconds (0.0));
sourceApps.Stop (Seconds (240.0));
PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port));
ApplicationContainer sinkApps = sink.Install(stas.Get(0));
sinkApps.Start (Seconds (0.0));
sinkApps.Stop (Seconds (240.0));
// This line triggers the calculation of throughput per second, starting at second 0.
Simulator::Schedule (Seconds (0.0), &ThroughputPerSecond, sinkApps.Get(0), 0 , stas.Get(0));
// This line triggers the tracing of the modulation scheme (to get the data rate)
Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback (&PhyTxTrace));
Simulator::Stop (Seconds (240.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}