/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2009 IITP RAS
*
* 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
*
* This is an example script for AODV manet routing protocol.
*
* Authors: Pavel Boyko <boyko@iitp.ru>
*/
#include <iostream>
#include <cmath>
#include "ns3/aodv-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/v4ping-helper.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/netanim-module.h"
using namespace ns3;
//创建一维的网络拓扑,第一个节点ping最后一个节点
/* [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4]
* ping 10.0.0.4
* 当经过1/3的模拟时间后,其中一个节点被移出范围,从而破坏拓扑
* 默认情况下,这将导致100个ping中只有34个被接收。
* 如果减小步长以覆盖间隙,那么所有的ping都能被接收
*/
/**
* \ingroup aodv-examples
* \ingroup examples
* \brief Test script.
*
* This script creates 1-dimensional grid topology and then ping last node from the first one:
*
* [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4]
*
* ping 10.0.0.4
*/
class AodvExample
{
public:
AodvExample ();
/**
* \Configure()是配置函数
* \argc是命令行参数计数
* \argv是命令行参数
* \配置成功返回true
*/
bool Configure (int argc, char **argv);
/// 运行仿真模拟
void Run ();
/**
* 报告结果
* \参数os是输出流
*/
void Report (std::ostream & os);
private:
// 参数
/// Number of nodes 节点个数
uint32_t size;
/// Distance between nodes, meters 节点之间的距离,单位 米
double step;
/// Simulation time, seconds 仿真总时间 seconds类型
double totalTime;
/// Write per-device PCAP traces if true 是否pcap输出
bool pcap;
/// Print routes if true 打印路由信息?
bool printRoutes;
// network
/// nodes used in the example 节点
NodeContainer nodes;
/// devices used in the example 网络设备
NetDeviceContainer devices;
/// interfaces used in the example ip协议栈
Ipv4InterfaceContainer interfaces;
private:
/// Create the nodes 创建节点
void CreateNodes ();
/// Create the devices 创建网络设备
void CreateDevices ();
/// Create the network 安装协议栈
void InstallInternetStack ();
/// Create the simulation applications 创建模拟应用层
void InstallApplications ();
};
// 主函数
int main (int argc, char **argv)
{
AodvExample test; // test为AodvExample类型
if (!test.Configure (argc, argv))
//打印错误信息
NS_FATAL_ERROR ("Configuration failed. Aborted.");
//仿真模拟开始运行
test.Run ();
//报告结果
test.Report (std::cout);
return 0;
}
//-----------------------------------------------------------------------------
AodvExample::AodvExample () :
size (10), //10个节点
step (100), //节点之间距离 100米
totalTime (100), //总共模拟100s
pcap (true), //pcpa追踪
printRoutes (true)
{
}
bool
AodvExample::Configure (int argc, char **argv)
{
// Enable AODV logs by default. Comment this if too noisy
// LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
SeedManager::SetSeed (12345);
CommandLine cmd;
//读取命令行参数
cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes);
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
AodvExample::Run ()
{
// Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time.
CreateNodes ();
CreateDevices ();
InstallInternetStack ();
InstallApplications ();
std::cout << "Starting simulation for " << totalTime << " s ...\n";
Simulator::Stop (Seconds (totalTime));
AnimationInterface anim ("aodv.xml");
Simulator::Run ();
Simulator::Destroy ();
}
void
AodvExample::Report (std::ostream &)
{
}
void
AodvExample::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), //x轴间距
"DeltaY", DoubleValue (0), //y轴间距
"GridWidth", UintegerValue (size), //每行最大节点数
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (nodes);
}
void
AodvExample::CreateDevices ()
{
WifiMacHelper wifiMac;
wifiMac.SetType ("ns3::AdhocWifiMac");
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiHelper wifi;
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
devices = wifi.Install (wifiPhy, wifiMac, nodes);
if (pcap)
{
wifiPhy.EnablePcapAll (std::string ("aodv"));
}
}
void
AodvExample::InstallInternetStack ()
{
AodvHelper aodv;
// you can configure AODV attributes here using aodv.Set(name, value)
InternetStackHelper stack;
stack.SetRoutingHelper (aodv); // has effect on the next Install ()
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.0.0.0");
interfaces = address.Assign (devices);
if (printRoutes)
{
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("aodv.routes", std::ios::out);
aodv.PrintRoutingTableAllAt (Seconds (8), routingStream);
}
}
void
AodvExample::InstallApplications ()
{
V4PingHelper ping (interfaces.GetAddress (size - 1));
ping.SetAttribute ("Verbose", BooleanValue (true));
ApplicationContainer p = ping.Install (nodes.Get (0));
p.Start (Seconds (0));
p.Stop (Seconds (totalTime) - Seconds (0.001));
// move node away
Ptr<Node> node = nodes.Get (size/2);
Ptr<MobilityModel> mob = node->GetObject<MobilityModel> ();
Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5));
}
NS-3实例分析——aodv.cc
最新推荐文章于 2022-01-25 14:00:29 发布