2014-11-24 830 views
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; 
} 

回答

1

我不熟悉的NS3真的,但通过您的代码后看,也许如果你改变了这一行

Simulator::Schedule (Seconds (0.0), &ThroughputPerSecond, sink1Apps, totalPacketsThrough, node); 

Simulator::Schedule (Seconds (1.0), &ThroughputPerSecond, sink1Apps, totalPacketsThrough, node); 

,或者您想要的任何值秒数增加,它可能不会无限循环。

至于你为什么得到错误的数据;我害怕,正如我上面所说,我不太熟悉NS3,所以不能进一步帮助。