【ns-3-dev】Examples Tutorial III third.cc

Examples

Tutorial

Third

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * 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
 */

/* 1.头文件 */
#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/ssid.h"

/* 网络场景。模拟了一个包含点对点(PPP)和CSMA有线网络,以及WIFI无线网络的混合场景。 */
// Default Network Topology
/  /
//   Wifi 10.1.3.0
//                 AP
//  *    *    *    *
//  |    |    |    |    10.1.1.0
// n5   n6   n7   n0 -------------- n1   n2   n3   n4
//                   point-to-point  |    |    |    |
//                                   ================
//                                     LAN 10.1.2.0

/* 2.名字空间 */
using namespace ns3;

/* 3.定义一个LOG模块  */
NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");

/* 4.主函数 */
int 
main (int argc, char *argv[])
{
  bool verbose = true;
  uint32_t nCsma = 3;
  uint32_t nWifi = 3;
  bool tracing = false;

  CommandLine cmd;/* cmd.AddValue读取命令行参数 */
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
  cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
  cmd.AddValue ("tracing", "Enable pcap tracing", tracing);

  cmd.Parse (argc,argv);

  // The underlying restriction of 18 is due to the grid position
  // allocator's configuration; the grid layout will exceed the
  // bounding box if more than 18 nodes are provided.
  if (nWifi > 18)
    {
      std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl;
      return 1;
    }

  if (verbose)
    {/* 打印指定LOG组件信息 */
      LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
      LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
    }

/* 5.创建网络拓扑 */
  NodeContainer p2pNodes;
  p2pNodes.Create (2);/* 创建2个P2P节点*/


/* 设置PPP信道属性 */
  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));/* 设置传输速率为5Mbps */
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));/* 设置信道传播时延为2ms */

  NetDeviceContainer p2pDevices;/* 创建网络设备 */
  p2pDevices = pointToPoint.Install (p2pNodes);/* pointToPoint.Install (n)函数内共创建了2个PPP网络设备对象pointToPointDevice和1个PPP信道对象pointToPointChannel。连接节点与信道 */

  NodeContainer csmaNodes;
  csmaNodes.Add (p2pNodes.Get (1));/* n1节点即是point-to-point节点,又是csma节点 */
  csmaNodes.Create (nCsma);/* 创建3个csma节点n2,n3,n4 */

  CsmaHelper csma;/* csma助手类,设置csma信道属性 */
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));/* 设置传输速率为100Mbps */
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));/* 设置信道延迟为6560ns */

  NetDeviceContainer csmaDevices;/* 创建csma网络设备连接节点与信道 */
  csmaDevices = csma.Install (csmaNodes);

  NodeContainer wifiStaNodes;
  wifiStaNodes.Create (nWifi);/* 创建3个wifi节点n5,n6,n7 */
  NodeContainer wifiApNode = p2pNodes.Get (0);/* n0节点即是point-to-point节点,又是wifi节点 */

  YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();/* 默认传播延迟模型,默认损耗模型 */
  YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();/* 默认误码率模型 */
  phy.SetChannel (channel.Create ());

  WifiHelper wifi;
  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");/* SetRemoteStationManager主要用于wifi的速率控制(rate control) */

  WifiMacHelper mac;
  Ssid ssid = Ssid ("ns-3-ssid");
  mac.SetType ("ns3::StaWifiMac",/* 移动节点 */
               "Ssid", SsidValue (ssid),
               "ActiveProbing", BooleanValue (false));

  NetDeviceContainer staDevices;/* 安装移动节点 */
  staDevices = wifi.Install (phy, mac, wifiStaNodes);

  mac.SetType ("ns3::ApWifiMac",/* AP节点 */
               "Ssid", SsidValue (ssid));

  NetDeviceContainer apDevices;/* 为AP节点安装应用 */
  apDevices = wifi.Install (phy, mac, wifiApNode);

  MobilityHelper mobility;/* 移动模型助手类 */

/* 为移动节点设置移动模型 */
  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                                 "MinX", DoubleValue (0.0),/* 起始坐标(0.0) */
                                 "MinY", DoubleValue (0.0),
                                 "DeltaX", DoubleValue (5.0),/* X轴节点间距:5m */
                                 "DeltaY", DoubleValue (10.0),/* Y轴节点间距:10m */
                                 "GridWidth", UintegerValue (3),/* 每行最大节点数 */
                                 "LayoutType", StringValue ("RowFirst"));

  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                             "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
  mobility.Install (wifiStaNodes);

/* 为AP节点设置移动模型 */
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (wifiApNode);

/* 6.安装TCP/IP协议簇 */
  InternetStackHelper stack;
  stack.Install (csmaNodes);
  stack.Install (wifiApNode);
  stack.Install (wifiStaNodes);

  Ipv4AddressHelper address;

  address.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer p2pInterfaces;
  p2pInterfaces = address.Assign (p2pDevices);

  address.SetBase ("10.1.2.0", "255.255.255.0");
  Ipv4InterfaceContainer csmaInterfaces;
  csmaInterfaces = address.Assign (csmaDevices);

  address.SetBase ("10.1.3.0", "255.255.255.0");
  address.Assign (staDevices);
  address.Assign (apDevices);

/* 7.安装应用程序 */
  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  ApplicationContainer clientApps = 
    echoClient.Install (wifiStaNodes.Get (nWifi - 1));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

/* 8.设置路由 */
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();/* 设置全局路由 */

  Simulator::Stop (Seconds (10.0));

/* 9,数据追踪 */
  if (tracing == true)
    {
      pointToPoint.EnablePcapAll ("third");
      phy.EnablePcap ("third", apDevices.Get (0));
      csma.EnablePcap ("third", csmaDevices.Get (0), true);
    }

/* 10.启动与结束 */
  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

终端运行结果

./waf --run scratch/third

Waf: Entering directory `/home/dragon/repos/ns-3-dev/build'
Waf: Leaving directory `/home/dragon/repos/ns-3-dev/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (3.639s)
At time 2s client sent 1024 bytes to 10.1.2.4 port 9
At time 2.01796s server received 1024 bytes from 10.1.3.3 port 49153
At time 2.01796s server sent 1024 bytes to 10.1.3.3 port 49153
At time 2.03364s client received 1024 bytes from 10.1.2.4 port 9

alt

【学习】无线网络安全 文章被收录于专栏

无线网络安全

全部评论

相关推荐

03-02 08:18
集美大学 Java
钱嘛数字而已:没有赛事奖项么?另外,项目经历字有点多哈,建议突出一下重点:用的什么技术,解决什么问题,达到什么效果。
大家都开始春招面试了吗
点赞 评论 收藏
分享
刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务