NS-3 学习之wifi-simple-adhoc.cc 分析和变种
作者:互联网
代码源自:ns-3 3.35 examples/wireless/wifi-simple-adhoc.cc,稍加修改
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2009 The Boeing Company
*
* 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 script configures two nodes on an 802.11b physical layer, with
// 802.11b NICs in adhoc mode, and by default, sends one packet of 1000
// (application) bytes to the other node. The physical layer is configured
// to receive at a fixed RSS (regardless of the distance and transmit
// power); therefore, changing position of the nodes has no effect.
// 节点间的信号强度RSS 是固定的,所以更改节点间的位置(距离)是不会影响节点间的信号传输的。
// There are a number of command-line options available to control
// the default behavior. The list of available command-line options
// can be listed with the following command:
// ./waf --run "wifi-simple-adhoc --help"
// 有一些有用的命令行选项可供使用
//
// For instance, for this configuration, the physical layer will
// stop successfully receiving packets when rss drops below -97 dBm.
// To see this effect, try running:
// 例如:
// ./waf --run "wifi-simple-adhoc --rss=-97 --numPackets=20"
// ./waf --run "wifi-simple-adhoc --rss=-98 --numPackets=20"
// ./waf --run "wifi-simple-adhoc --rss=-99 --numPackets=20"
//
// Note that all ns-3 attributes (not just the ones exposed in the below
// script) can be changed at command line; see the documentation.
// 请注意,所有 ns-3 属性(不仅仅是下面公开的那些脚本)可以在命令行更改; 请参阅文档。
// This script can also be helpful to put the Wifi layer into verbose
// logging mode; this command will turn on all wifi logging:
// 该脚本还有助于将 Wifi 层置于详细日志记录模式; 此命令将打开所有 wifi 日志记录:
// ./waf --run "wifi-simple-adhoc --verbose=1"
//
// When you are done, you will notice two pcap trace files in your directory.
// If you have tcpdump installed, you can try this:
// 完成后,您会注意到目录中有两个 pcap 跟踪文件。可以使用tcpdump 打开。
// tcpdump -r wifi-simple-adhoc-0-0.pcap -nn -tt
//
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/double.h"
#include "ns3/string.h"
#include "ns3/log.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/mobility-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/yans-wifi-channel.h"
#include "ns3/mobility-model.h"
#include "ns3/internet-stack-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("WifiSimpleAdhoc");
void ReceivePacket(Ptr<Socket> socket) {
Address from;
while (socket->RecvFrom(from)) {
// std::string nodeId = std::to_string(socket->GetNode()->GetId());
NS_LOG_UNCOND(
"Node" << socket->GetNode()->GetId()<< " Received one packet from " <<InetSocketAddress::ConvertFrom (from).GetIpv4 ());
}
}
static void GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval) {
if (pktCount > 0) {
socket->Send(Create<Packet>(pktSize));
//迭代发数据
Simulator::Schedule(pktInterval, &GenerateTraffic, socket, pktSize,
pktCount - 1, pktInterval);
} else {
socket->Close();
}
}
int main(int argc, char *argv[]) {
std::string phyMode("DsssRate1Mbps");
double rss = -81; // -dBm Jerry:经测试,最小是-81,低于-81则通信不正常。
uint32_t packetSize = 1000; // bytes
uint32_t numPackets = 20;
double interval = 1.0; // seconds
bool verbose = false;
CommandLine cmd(__FILE__);
cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
cmd.AddValue("rss", "received signal strength", rss);
cmd.AddValue("packetSize", "size of application packet sent", packetSize);
cmd.AddValue("numPackets", "number of packets generated", numPackets);
cmd.AddValue("interval", "interval (seconds) between packets", interval);
cmd.AddValue("verbose", "turn on all WifiNetDevice log components",
verbose);
cmd.Parse(argc, argv);
// Convert to time object
Time interPacketInterval = Seconds(interval);
// Fix non-unicast data rate to be the same as that of unicast
// 设定非单播数据速率与单播相同
Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode",
StringValue(phyMode));
NodeContainer c;
c.Create(3); //创建两个节点
// The below set of helpers will help us to put together the wifi NICs we want
// 下面的一组助手将帮助我们把我们想要的 wifi 网卡放在一起
WifiHelper wifi;
if (verbose) {
wifi.EnableLogComponents(); // Turn on all Wifi logging
}
wifi.SetStandard(WIFI_STANDARD_80211b);
YansWifiPhyHelper wifiPhy;
// This is one parameter that matters when using FixedRssLossModel
// set it to zero; otherwise, gain will be added
//这是使用 FixedRssLossModel 时重要的一个参数,将其设置为零; 否则,将增加增益
wifiPhy.Set("RxGain", DoubleValue(0));
// ns-3 supports RadioTap and Prism tracing extensions for 802.11b
// ns-3 支持 802.11b 的 RadioTap 和 Prism 跟踪扩展
wifiPhy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
// The below FixedRssLossModel will cause the rss to be fixed regardless
// of the distance between the two stations, and the transmit power
/*
* 下面的 FixedRssLossModel 将导致 rss 固定,无论两个站之间的距离如何,以及发射功率
* Jerry:也即两节点之间距离不管多大,都能以设置的rss 信号强度进行通信。
*/
wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss",
DoubleValue(rss));
wifiPhy.SetChannel(wifiChannel.Create());
// Add a mac and disable rate control,添加一个mac并禁用速率控制
WifiMacHelper wifiMac;
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode",
StringValue(phyMode), "ControlMode", StringValue(phyMode));
// Set it to adhoc mode,设置为adhoc 模式
wifiMac.SetType("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c);
// Note that with FixedRssLossModel, the positions below are not
// used for received signal strength.
// 请注意,对于 FixedRssLossModel,以下位置不用于接收信号强度。
// Jerry:再次强调在固定信号强度模式下,节点的位置关系对信号强弱没有影响
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<
ListPositionAllocator>();
positionAlloc->Add(Vector(50.0, 50.0, 0.0));
positionAlloc->Add(Vector(100.0, 50.0, 0.0));
positionAlloc->Add(Vector(50.0, 100.0, 0.0));
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); //恒定位置移动模型
mobility.Install(c);
InternetStackHelper internet;
internet.Install(c); //安装internet 协议栈
Ipv4AddressHelper ipv4;
NS_LOG_INFO("Assign IP Addresses.");
ipv4.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign(devices);
TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
Ptr<Socket> source = Socket::CreateSocket(c.Get(0), tid); //源socket
//255.255.255.255,广播地址;80,端口号
InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"),
80);
source->SetAllowBroadcast(true);
source->Connect(remote);
Ptr<Socket> source1 = Socket::CreateSocket(c.Get(1), tid); //源socket
source1->SetAllowBroadcast(true);
source1->Connect(remote);
Ptr<Socket> recvSink0 = Socket::CreateSocket(c.Get(0), tid); //目的socket
InetSocketAddress local0 = InetSocketAddress(Ipv4Address::GetAny(), 80);
recvSink0->Bind(local0);
recvSink0->SetRecvCallback(MakeCallback(&ReceivePacket));
Ptr<Socket> recvSink1 = Socket::CreateSocket(c.Get(1), tid); //目的socket
InetSocketAddress local1 = InetSocketAddress(Ipv4Address::GetAny(), 80);
recvSink1->Bind(local1);
recvSink1->SetRecvCallback(MakeCallback(&ReceivePacket));
Ptr<Socket> recvSink2 = Socket::CreateSocket(c.Get(2), tid); //目的socket
InetSocketAddress local2 = InetSocketAddress(Ipv4Address::GetAny(), 80);
recvSink2->Bind(local2);
recvSink2->SetRecvCallback(MakeCallback(&ReceivePacket));
// Tracing
wifiPhy.EnablePcap("wifi-simple-adhoc", devices);
// Output what we are doing
NS_LOG_UNCOND(
"Testing " << numPackets << " packets sent with receiver rss " << rss);
Simulator::ScheduleWithContext(source->GetNode()->GetId(), Seconds(1.0),
&GenerateTraffic, source, packetSize, numPackets,
interPacketInterval);
Simulator::ScheduleWithContext(source1->GetNode()->GetId(),
Seconds(1.00001), &GenerateTraffic, source1, packetSize, numPackets,
interPacketInterval);
Simulator::Run();
Simulator::Destroy();
return 0;
}
将程序小修改了一下,增加了一个节点,让节点0和节点1均广播消息,三个节点均可以接收广播消息。这样能看到,节点0和1不能同时发广播消息,时间上要错开。节点0可以接收节点1发的广播消息,不能接收自己发的广播消息。节点1同理。节点2不发送广播消息,能接收节点0和1发的广播消息。
1.关于通信距离
通过该例子,我尝试让两个点之间的距离拉大到7500米,两个点之间仍然能够通信。上网查了下,理论上,wifi 距离可以达到200多km远。那么为什么aodv.cc 那个例子里却只有51米远呢?对比两个例子,发现在设置AddPropagationLoss()的时候有所不同。当前的例子设置为:
wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss",DoubleValue(rss));
该段代码上方已经给出注释了:
下面的 FixedRssLossModel 将导致 rss 固定,无论两个站之间的距离如何,以及发射功率
Jerry:也即两节点之间距离不管多大,都能通信。
(后来从头到尾读了下源码的注释,发现关于信号强度RSS 注释说的已经很明了,怪自己上来就调试代码,没仔细读注释。)
2.关于广播冲突
调试时发现如果两个节点在同一时刻广播,则无广播数据发出,比如下一段代码,两个节点都从1秒时开始发送广播数据,则两个节点谁也没能发出广播数据:
Simulator::ScheduleWithContext(source->GetNode()->GetId(), Seconds(1.0),
&GenerateTraffic, source, packetSize, numPackets,
interPacketInterval);
Simulator::ScheduleWithContext(source1->GetNode()->GetId(),
Seconds(1.0), &GenerateTraffic, source1, packetSize, numPackets,
interPacketInterval);
3.关于如何通过接收socket 消息的回调函数获取远程发送消息节点信息
从接受到的Socket 中可以获取到当前消息接收节点的id 和远程发送节点的ip,暂时未找到获取远程发送节点的node id 的方法。
标签:socket,simple,wifi,adhoc,--,ns3,节点,rss 来源: https://blog.csdn.net/Jerry_liu20080504/article/details/122683944