wifi-simple-adhoc.cc

  #include "ns3/core-module.h"
55  #include "ns3/network-module.h"
56  #include "ns3/mobility-module.h"
57  #include "ns3/config-store-module.h"
58  #include "ns3/wifi-module.h"
59  #include "ns3/internet-module.h"
60 
61  #include <iostream>
62  #include <fstream>
63  #include <vector>
64  #include <string>
65 
66  NS_LOG_COMPONENT_DEFINE ( "WifiSimpleAdhoc");
67 
68  using namespace ns3;
69 
70  void ReceivePacket ( Ptr<Socket> socket)
71 {
72  NS_LOG_UNCOND ( "Received one packet!");
73 }
74 
75  static void GenerateTraffic ( Ptr<Socket> socket, uint32_t pktSize,
76  uint32_t pktCount, Time pktInterval )
77 {
78  if (pktCount > 0)
79  {
80  socket-> Send (Create<Packet> (pktSize));
81  Simulator::Schedule (pktInterval, & GenerateTraffic,
82  socket, pktSize,pktCount-1, pktInterval);
83  }
84  else
85  {
86  socket-> Close ();
87  }
88 }
89 
90 
91  int main ( int argc, char *argv[])
92 {
93  std::string phyMode ( "DsssRate1Mbps");
94  double rss = -80; // -dBm
95  uint32_t packetSize = 1000; // bytes
96  uint32_t numPackets = 1;
97  double interval = 1.0; // seconds
98  bool verbose = false;
99 
100  CommandLine cmd;
101 
102  cmd. AddValue ( "phyMode", "Wifi Phy mode", phyMode);
103  cmd. AddValue ( "rss", "received signal strength", rss);
104  cmd. AddValue ( "packetSize", "size of application packet sent", packetSize);
105  cmd. AddValue ( "numPackets", "number of packets generated", numPackets);
106  cmd. AddValue ( "interval", "interval (seconds) between packets", interval);
107  cmd. AddValue ( "verbose", "turn on all WifiNetDevice log components", verbose);
108 
109  cmd. Parse (argc, argv);
110  // Convert to time object
111  Time interPacketInterval = Seconds (interval);
112 
113  // disable fragmentation for frames below 2200 bytes
114  Config::SetDefault ( "ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ( "2200"));
115  // turn off RTS/CTS for frames below 2200 bytes
116  Config::SetDefault ( "ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ( "2200"));
117  // Fix non-unicast data rate to be the same as that of unicast
118  Config::SetDefault ( "ns3::WifiRemoteStationManager::NonUnicastMode",
119  StringValue (phyMode));
120 
121  NodeContainer c;
122  c. Create (2);
123 
124  // The below set of helpers will help us to put together the wifi NICs we want
125  WifiHelper wifi;
126  if (verbose)
127  {
128  wifi. EnableLogComponents (); // Turn on all Wifi logging
129  }
 
//配置wifiphy
133  // This is one parameter that matters when using FixedRssLossModel
134  // set it to zero; otherwise, gain will be added
135  wifiPhy. Set ( "RxGain", DoubleValue (0) );
136  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b

// ConstantSpeedPropagationDelayModel默认的传播延迟模型
//配置wifichannel
139  YansWifiChannelHelper wifiChannel;
140  wifiChannel. SetPropagationDelay ( "ns3:: ConstantSpeedPropagationDelayModel ");
141  // The below FixedRssLossModel will cause the rss to be fixed regardless
142  // of the distance between the two stations, and the transmit power
143  wifiChannel. AddPropagationLoss ( "ns3::FixedRssLossModel", "Rss", DoubleValue (rss));
144  wifiPhy. SetChannel (wifiChannel. Create ());
145 
146  // Add a non-QoS upper mac, and disable rate control
//ConstantRateWifiManager固定速率12Mbps

//配置没有QoS的MAC
148  wifi. SetRemoteStationManager ( "ns3::ConstantRateWifiManager",
149  "DataMode", StringValue (phyMode),
150  "ControlMode", StringValue (phyMode));
151  // Set it to adhoc mode
152  wifiMac. SetType ( "ns3::AdhocWifiMac");
153  NetDeviceContainer devices = wifi. Install (wifiPhy, wifiMac, c); //创建wifidevice
154 
155  // Note that with FixedRssLossModel, the positions below are not
156  // used for received signal strength.

// 移动模型配置
//通过ListPositionAllocator手工放置??
157  MobilityHelper mobility;
Ptr< ListPositionAllocator > positionAlloc = CreateObject<ListPositionAllocator> ();
159  positionAlloc-> Add ( Vector (0.0, 0.0, 0.0));
160  positionAlloc-> Add ( Vector (5.0, 0.0, 0.0));
161  mobility. SetPositionAllocator (positionAlloc);
162  mobility. SetMobilityModel ( "ns3::ConstantPositionMobilityModel");
163  mobility. Install (c);
164 

165  InternetStackHelper internet;
166  internet. Install (c);
167 
168  Ipv4AddressHelper ipv4;
169  NS_LOG_INFO ( "Assign IP Addresses.");
170  ipv4. SetBase ( "10.1.1.0", "255.255.255.0");
171  Ipv4InterfaceContainer i = ipv4. Assign (devices);
172 
173  TypeId tid = TypeId::LookupByName ( "ns3::UdpSocketFactory");
174  Ptr<Socket> recvSink = Socket::CreateSocket (c. Get (0), tid);
176  recvSink-> Bind (local);
177  recvSink-> SetRecvCallback ( MakeCallback (& ReceivePacket));
178 
179  Ptr<Socket> source = Socket::CreateSocket (c. Get (1), tid);
180  InetSocketAddress remote = InetSocketAddress ( Ipv4Address ( "255.255.255.255"), 80);
181  source-> SetAllowBroadcast ( true);
182  source-> Connect (remote);
183 
184  // Tracing
185  wifiPhy. EnablePcap ( "wifi-simple-adhoc", devices);
186 
187  // Output what we are doing
188  NS_LOG_UNCOND ( "Testing " << numPackets << " packets sent with receiver rss " << rss );
189 
191  Seconds (1.0), & GenerateTraffic,
192  source, packetSize, numPackets, interPacketInterval);
193 
194  Simulator::Run ();
196 
197  return 0;
198 }
199 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验内容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. 掌握统计数据作图方法,对于获得的统计数据,图形化的形式得出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值