#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-module.h"
using namespace ns3;
int main (int argc, char *argv[])
{
// 创建一个星型无线网络
NodeContainer ap;
ap.Create (1);
NodeContainer sta;
sta.Create (5);
// 安装WiFi设备
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ);
wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
WifiMacHelper mac;
Ssid ssid = Ssid ("ns3-ssid");
mac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid));
NetDeviceContainer apDevices = wifi.Install (phy, mac, ap);
mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid));
NetDeviceContainer staDevices = wifi.Install (phy, mac, sta);
// 设置移动模型
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (5.0),
"GridWidth", UintegerValue (3),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (sta);
// 安装网络协议栈
InternetStackHelper stack;
stack.Install (ap);
stack.Install (sta);
// 分配IP地址
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer apInterfaces = address.Assign (apDevices);
Ipv4InterfaceContainer staInterfaces = address.Assign (staDevices);
// 创建应用程序
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (ap.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (apInterfaces.GetAddress (0), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (5));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (sta);
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// 启动流量监控器
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
// 运行仿真
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
// 输出链路吞吐量
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin (); iter != stats.end (); ++iter)
{
Ipv4FlowClassifier::FiveTuple tuple = classifier->FindFlow (iter->first);
std::cout << "Flow " << iter->first << " (" << tuple.sourceAddress << " -> " << tuple.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << iter->second.txBytes << "\n";
std::cout << " Rx Bytes: " << iter->second.rxBytes << "\n";
std::cout << " Throughput: " << iter->second.rxBytes * 8.0 / 10.0 / 1024 / 1024 << " Mbps\n";
}
// 生成XML文件
flowmon.SerializeToXmlFile ("mytopology.xml", true, true);
Simulator::Destroy ();
return 0;
}