/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include "ns3/gpsr-module.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/wifi-module.h"
#include "ns3/v4ping-helper.h"
#include "ns3/udp-echo-server.h"
#include "ns3/udp-echo-client.h"
#include "ns3/udp-echo-helper.h"
#include <iostream>
#include <cmath>
using namespace ns3;
class GpsrExample
{
public:
GpsrExample ();
/// Configure script parameters, \return true on successful configuration配置脚本参数,\成功配置时返回true
bool Configure (int argc, char **argv);
/// Run simulation 运行仿真
void Run ();
/// Report results 报告结果
void Report (std::ostream & os);
private:
///\name parameters 名称参数
//\{
/// Number of nodes 节点的数量
uint32_t size;
/// Width of the Node Grid 节点网络的宽度,一条线上布置的对象的数量
uint32_t gridWidth;
/// Distance between nodes, meters 节点之间的距离,米
double step;
/// Simulation time, seconds 仿真时间,秒
double totalTime;
/// Write per-device PCAP traces if true 如果为真,则写入每个设备的PCAP跟踪
bool pcap;
//\}
///\name network 命名网络
//\{
NodeContainer nodes;
NetDeviceContainer devices;
Ipv4InterfaceContainer interfaces;
//\}
private:
void CreateNodes ();
void CreateDevices ();
void InstallInternetStack ();
void InstallApplications ();
};
int main (int argc, char **argv)
{
GpsrExample test;
if (! test.Configure(argc, argv))
NS_FATAL_ERROR ("Configuration failed. Aborted.");
test.Run ();
test.Report (std::cout);
return 0;
}
//-----------------------------------------------------------------------------
GpsrExample::GpsrExample () :
// Number of Nodes 节点的数量
size (2),
// Grid Width 节点网络的宽度,一条线上布置的对象的数量
gridWidth(2),
// Distance between nodes 节点之间的距离,米
step (200),
// Simulation time 仿真时间
totalTime (30),
// Generate capture files for each node 为每个节点生成追踪文件
pcap (true)
{
}
bool
GpsrExample::Configure (int argc, char **argv)
{
// Enable GPSR logs by default. Comment this if too noisy 默认启用GPSR日志
//LogComponentEnable("GpsrRoutingProtocol", LOG_LEVEL_ALL);
SeedManager::SetSeed(12345);
CommandLine cmd;
cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
cmd.AddValue ("size", "Number of nodes.", size);
cmd.AddValue ("time", "Simulation time, s.", totalTime);
cmd.AddValue ("step", "Grid step, m", step);
cmd.Parse (argc, argv);
return true;
}
void
GpsrExample::Run ()
{
// Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time.始终启用rtscts。
CreateNodes ();
CreateDevices ();
InstallInternetStack ();
InstallApplications ();
GpsrHelper gpsr;
gpsr.Install ();
std::cout << "Starting simulation for " << totalTime << " s ...\n";
Simulator::Stop (Seconds (totalTime));
Simulator::Run ();
Simulator::Destroy ();
}
void
GpsrExample::Report (std::ostream &)
{
}
void
GpsrExample::CreateNodes ()
{
std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
nodes.Create (size);
// Name nodes 命名节点
for (uint32_t i = 0; i < size; ++i)
{
std::ostringstream os;
os << "node-" << i;
Names::Add (os.str (), nodes.Get (i));
}
// Create static grid 创建静态网格
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (step),
"DeltaY", DoubleValue (step),
"GridWidth", UintegerValue (gridWidth),//一条线上布置的对象的数量
"LayoutType", StringValue ("RowFirst"));//行优先
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (nodes);
}
void
GpsrExample::CreateDevices ()
{
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifiMac.SetType ("ns3::AdhocWifiMac");
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiHelper wifi = WifiHelper::Default ();
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
devices = wifi.Install (wifiPhy, wifiMac, nodes);
if (pcap)
{
wifiPhy.EnablePcapAll (std::string ("gpsr"));
}
}
void
GpsrExample::InstallInternetStack ()
{
GpsrHelper gpsr;
// you can configure GPSR attributes here using gpsr.Set(name, value) 您可以使用gpsr.Set(名称,值)在这里配置GPSR属性
InternetStackHelper stack;
stack.SetRoutingHelper (gpsr);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.255.0.0");
interfaces = address.Assign (devices);
}
void
GpsrExample::InstallApplications ()
{
uint16_t port = 9; // well-known echo port number众所周知的回声端口号
uint32_t packetSize = 1024; // size of the packets being transmitted 正在传输的数据包的大小
uint32_t maxPacketCount = 100; // number of packets to transmit 要传输的数据包数量
Time interPacketInterval = Seconds (1.); // interval between packet transmissions 数据包传输之间的间隔
// Set-up a server Application on the bottom-right node of the grid 在网格的右下角节点上设置服务器应用程序
UdpEchoServerHelper server1 (port);
uint16_t server1Position = size-1; //bottom right 右下角
ApplicationContainer apps = server1.Install (nodes.Get(server1Position));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (totalTime-0.1));
// Set-up a client Application, connected to 'server1', to be run on the top-left node of the grid设置连接到“服务器1”的客户端应用程序,在网格的左上角节点上运行
UdpEchoClientHelper client1 (interfaces.GetAddress (server1Position), port);
client1.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
client1.SetAttribute ("Interval", TimeValue (interPacketInterval));
client1.SetAttribute ("PacketSize", UintegerValue (packetSize));
uint16_t client1Position = 0; //top left 左上角
apps = client1.Install (nodes.Get (client1Position));
apps.Start (Seconds (2.0));
apps.Stop (Seconds (totalTime-0.1));
// Set-up a server Application on the top-right node of the grid 在网格的右上角节点上设置服务器应用程序
UdpEchoServerHelper server2 (port);
uint16_t server2Position = 9;
apps = server2.Install (nodes.Get(server2Position)); //top right 右上角
apps.Start (Seconds (1.0));
apps.Stop (Seconds (totalTime-0.1));
// Set-up a client Application, connected to 'server2', to be run on the bottom-left node of the grid 设置连接到'server2'的客户端应用程序,以便在网格的左下角节点上运行
UdpEchoClientHelper client2 (interfaces.GetAddress (server2Position), port);
client2.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
client2.SetAttribute ("Interval", TimeValue (interPacketInterval));
client2.SetAttribute ("PacketSize", UintegerValue (packetSize));
uint16_t client2Position = size-9; //bottom left 左下角
apps = client2.Install (nodes.Get (client2Position));
apps.Start (Seconds (2.0));
apps.Stop (Seconds (totalTime-0.1));
}