一个wif ad hoc的例子

#include <iostream>
#include <string>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/adhoc-wifi-mac.h"

// Default Network Topology
//
//   Wifi 10.1.3.0
//
//  *    *    *    * ... *
//  |    |    |    |     |
// n0   n1   n2   n3   nWifi-1
//

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("ThirdScriptExampleWifiOnly");

static Vector
GetPosition (Ptr<Node> node)
{
  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
  return mobility->GetPosition();

}

int
main (int argc, char *argv[])
{
  bool verbose = true;
  bool twoBroadcast = false;
  uint32_t nWifi = 10;
  double latDistance = 50;
  double rndStart = 0.000016;
  UniformVariable rndStartValue(0.0, rndStart);

  Config::SetDefault ("ns3::OnOffApplication::PacketSize", StringValue ("500"));
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("6000kb/s"));

  CommandLine cmd;
// use --<argument>=0 for false when <argument> is boolean
  cmd.AddValue ("nWifi", "Number of wifi adhoc devices", nWifi);
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
  cmd.AddValue ("latDistance", "Distance between nodes, in meters", latDistance);
  cmd.AddValue ("twoBroadcast", "Sets a second node to broadcast if true", twoBroadcast);

  cmd.Parse (argc,argv);

  if (verbose)
    {
      LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
      LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
    }

  NodeContainer wifiStaNodes;
  wifiStaNodes.Create (nWifi);

  YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
  YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
  phy.SetChannel (channel.Create ());

  WifiHelper wifi = WifiHelper::Default ();
  phy.Set ("EnergyDetectionThreshold", DoubleValue (-100.0) );
  phy.Set ("CcaMode1Threshold", DoubleValue (-90.0) );
  phy.Set ("TxGain", DoubleValue (5.0) );
  // I used higher gain to extend range to about 300 meters
  phy.Set ("RxGain", DoubleValue (5.0) );
  phy.Set ("TxPowerLevels", UintegerValue (1) );
  phy.Set ("TxPowerEnd", DoubleValue (16.0206) );
  phy.Set ("TxPowerStart", DoubleValue (16.0206) );
  phy.Set ("RxNoiseFigure", DoubleValue (3) );

  wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
                                "DataMode", StringValue ("OfdmRate6Mbps"));

  NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();
  mac.SetType ("ns3::AdhocWifiMac",
               "Slot", StringValue ("16us"));

  NetDeviceContainer staDevices;
  staDevices = wifi.Install (phy, mac, wifiStaNodes);
  MobilityHelper mobility;
  Ptr<ListPositionAllocator> positionAlloc =
    CreateObject<ListPositionAllocator> ();
  for (uint32_t x = 0; x < nWifi; x++)
    {
        positionAlloc->Add (Vector ((x*latDistance), 0.0, 0.0));
    }
  mobility.SetPositionAllocator (positionAlloc);

// Nodes are stationary
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (wifiStaNodes);

// print current location of the nodes
    for (uint32_t i = 0; i < nWifi; ++i)
    {
      Vector pos = GetPosition(wifiStaNodes.Get(i));
      std::cout << "Node " << i << " " << pos.x << " " << pos.y <<
std::endl;
    }

  InternetStackHelper stack;
  stack.Install (wifiStaNodes);

  Ipv4AddressHelper address;

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

  NS_LOG_INFO ("Create Applications.");
  uint16_t port = 9;   // Discard port is 9 (RFC 863)
  Ipv4Address remoteAddr = "255.255.255.255"; // send to the broadcast address

  OnOffHelper onoff ("ns3::UdpSocketFactory",
                     Address (InetSocketAddress (remoteAddr, port)));
  onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (10)));
  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));

// set Node 0 to broadcast
  ApplicationContainer apps;
    Ptr<Node> appSource = NodeList::GetNode (0);
    apps = onoff.Install (appSource);
    apps.Start (Seconds (rndStartValue.GetValue()));
    apps.Stop (Seconds (2.0));

// set furthest node to broadcast also
  if (twoBroadcast == true)
  {
    Ptr<Node> appSource = NodeList::GetNode (nWifi-1);
    apps = onoff.Install (appSource);
    apps.Start (Seconds (rndStartValue.GetValue()));
    apps.Stop (Seconds (2.0));
  }

// Create a packet sink to receive these packets on each of the middle Nodes
// Node (0) and Node (nWifi-1) do not have a sink installed
// Output does not change if the sink is not installed
  PacketSinkHelper sink ("ns3::UdpSocketFactory",
                         InetSocketAddress (Ipv4Address::GetAny (), port));
  for (uint32_t nNodes = 1; nNodes < (nWifi-1); nNodes++)
  {
    std::cout << "Node " << nNodes << " is a sink." << std::endl;
    Ptr<Node> appSink = NodeList::GetNode (nNodes);
    apps = sink.Install (appSink);
    apps.Start (Seconds (0.0));
  }

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

// Tracing configuration
  NS_LOG_INFO ("Configure Tracing.");
 // std::ofstream ascii;
 // ascii.open ("third_adhoc.txt");
   AsciiTraceHelper wifiAscii;
   Ptr<OutputStreamWrapper> wifiStream = wifiAscii.CreateFileStream ("gpf-wifi-ascii.tr");
   phy.EnableAsciiAll (wifiStream);

//  AsciiTraceHelperForIpv4 internetAscii;
//  Ptr<OutputStreamWrapper> internetStream = internetAscii.CreateFileStream ("gpf-internet-ascii.tr");
  stack.EnableAsciiIpv4All ("internetStream");
// YansWifiPhyHelper::EnableAsciiAll ("asciiPhy");
//  InternetStackHelper::EnableAsciiAll (ascii);
  phy.EnablePcapAll ("third_adhoc");

  Simulator::Stop (Seconds (10.0));

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}




  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实验内容1. 在上一个实验的基础上,建立ad hoc节点拓扑模型,网络中节点均具有路由转发功能; 2. 进入~/tarballs/ns-allinone-3.2x/ns-3.2x/examples/wireless$ 目录,在wireless文件夹下,有很多wifi传输的示例。在wifi-simple-adhoc-grid.cc代码基础上修改,文件拷贝到scratch文件夹下,重命名为你的姓名缩写+学号,例如学生姓名钢铁侠,学号20191314,c文件命名为gtx20191314.cc; 3. 读懂wifi-simple-adhoc-grid.cc代码,掌握在NS-3中网络拓扑布局,ad hoc传输方式、网络路由的简单配置等,成功运行wifi-simple-adhoc-grid.cc; 4. 掌握拓扑建模方法,在wifi-simple-adhoc-grid.cc代码中扩展建立到100个节点,节点初始呈现网格布局; 5. 深入研究移动模型建模方法,完成对移动模型参数设置,使得节点移动在可视化界面更加明显; 6. 深入研究信道模型建模方法,在代码中完成对信道模型的替换,例如Friis、LogDistancePropagationLossModel信道模型。通过射频参数,了解发射机、路径损耗和接收机间增益的计算方法。可以根据参数配置想要的发射距离; 7. 掌握网路层路由配置方法,可以配置多种ad hoc路由模式,例如OLSR、aodv等。查阅不同路由协议的工作原理和方法; 8. 掌握统计模型的使用方法,可以统计相应节点的时延、丢包率等重要指标; 9. 掌握能耗模型的使用方法,配置节点能量,设备能量,可以统计节点的能量消耗; 10. 掌握统计数据作图方法,对于获得的统计数据,图形化的形式得出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值