A Discrete-Event Network Simulator
API
wifi-simple-adhoc-grid.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
18
19//
20// This program configures a grid (default 5x5) of nodes on an
21// 802.11b physical layer, with
22// 802.11b NICs in adhoc mode, and by default, sends one packet of 1000
23// (application) bytes to node 1.
24//
25// The default layout is like this, on a 2-D grid.
26//
27// n20 n21 n22 n23 n24
28// n15 n16 n17 n18 n19
29// n10 n11 n12 n13 n14
30// n5 n6 n7 n8 n9
31// n0 n1 n2 n3 n4
32//
33// the layout is affected by the parameters given to GridPositionAllocator;
34// by default, GridWidth is 5 and numNodes is 25..
35//
36// There are a number of command-line options available to control
37// the default behavior. The list of available command-line options
38// can be listed with the following command:
39// ./ns3 run "wifi-simple-adhoc-grid --help"
40//
41// Note that all ns-3 attributes (not just the ones exposed in the below
42// script) can be changed at command line; see the ns-3 documentation.
43//
44// For instance, for this configuration, the physical layer will
45// stop successfully receiving packets when distance increases beyond
46// the default of 500m.
47// To see this effect, try running:
48//
49// ./ns3 run "wifi-simple-adhoc-grid --distance=500"
50// ./ns3 run "wifi-simple-adhoc-grid --distance=1000"
51// ./ns3 run "wifi-simple-adhoc-grid --distance=1500"
52//
53// The source node and sink node can be changed like this:
54//
55// ./ns3 run "wifi-simple-adhoc-grid --sourceNode=20 --sinkNode=10"
56//
57// This script can also be helpful to put the Wifi layer into verbose
58// logging mode; this command will turn on all wifi logging:
59//
60// ./ns3 run "wifi-simple-adhoc-grid --verbose=1"
61//
62// By default, trace file writing is off-- to enable it, try:
63// ./ns3 run "wifi-simple-adhoc-grid --tracing=1"
64//
65// When you are done tracing, you will notice many pcap trace files
66// in your directory. If you have tcpdump installed, you can try this:
67//
68// tcpdump -r wifi-simple-adhoc-grid-0-0.pcap -nn -tt
69//
70
71#include "ns3/command-line.h"
72#include "ns3/config.h"
73#include "ns3/double.h"
74#include "ns3/internet-stack-helper.h"
75#include "ns3/ipv4-address-helper.h"
76#include "ns3/ipv4-list-routing-helper.h"
77#include "ns3/ipv4-static-routing-helper.h"
78#include "ns3/log.h"
79#include "ns3/mobility-helper.h"
80#include "ns3/mobility-model.h"
81#include "ns3/olsr-helper.h"
82#include "ns3/string.h"
83#include "ns3/uinteger.h"
84#include "ns3/yans-wifi-channel.h"
85#include "ns3/yans-wifi-helper.h"
86
87using namespace ns3;
88
89NS_LOG_COMPONENT_DEFINE("WifiSimpleAdhocGrid");
90
96void
98{
99 while (socket->Recv())
100 {
101 NS_LOG_UNCOND("Received one packet!");
102 }
103}
104
113static void
115{
116 if (pktCount > 0)
117 {
118 socket->Send(Create<Packet>(pktSize));
119 Simulator::Schedule(pktInterval,
121 socket,
122 pktSize,
123 pktCount - 1,
124 pktInterval);
125 }
126 else
127 {
128 socket->Close();
129 }
130}
131
132int
133main(int argc, char* argv[])
134{
135 std::string phyMode("DsssRate1Mbps");
136 double distance = 500; // m
137 uint32_t packetSize = 1000; // bytes
138 uint32_t numPackets = 1;
139 uint32_t numNodes = 25; // by default, 5x5
140 uint32_t sinkNode = 0;
141 uint32_t sourceNode = 24;
142 double interval = 1.0; // seconds
143 bool verbose = false;
144 bool tracing = false;
145
146 CommandLine cmd(__FILE__);
147 cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
148 cmd.AddValue("distance", "distance (m)", distance);
149 cmd.AddValue("packetSize", "size of application packet sent", packetSize);
150 cmd.AddValue("numPackets", "number of packets generated", numPackets);
151 cmd.AddValue("interval", "interval (seconds) between packets", interval);
152 cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
153 cmd.AddValue("tracing", "turn on ascii and pcap tracing", tracing);
154 cmd.AddValue("numNodes", "number of nodes", numNodes);
155 cmd.AddValue("sinkNode", "Receiver node number", sinkNode);
156 cmd.AddValue("sourceNode", "Sender node number", sourceNode);
157 cmd.Parse(argc, argv);
158 // Convert to time object
159 Time interPacketInterval = Seconds(interval);
160
161 // Fix non-unicast data rate to be the same as that of unicast
162 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
163
165 c.Create(numNodes);
166
167 // The below set of helpers will help us to put together the wifi NICs we want
169 if (verbose)
170 {
171 wifi.EnableLogComponents(); // Turn on all Wifi logging
172 }
173
174 YansWifiPhyHelper wifiPhy;
175 // set it to zero; otherwise, gain will be added
176 wifiPhy.Set("RxGain", DoubleValue(-10));
177 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
178 wifiPhy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
179
180 YansWifiChannelHelper wifiChannel;
181 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
182 wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
183 wifiPhy.SetChannel(wifiChannel.Create());
184
185 // Add an upper mac and disable rate control
186 WifiMacHelper wifiMac;
187 wifi.SetStandard(WIFI_STANDARD_80211b);
188 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
189 "DataMode",
190 StringValue(phyMode),
191 "ControlMode",
192 StringValue(phyMode));
193 // Set it to adhoc mode
194 wifiMac.SetType("ns3::AdhocWifiMac");
195 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c);
196
198 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
199 "MinX",
200 DoubleValue(0.0),
201 "MinY",
202 DoubleValue(0.0),
203 "DeltaX",
204 DoubleValue(distance),
205 "DeltaY",
206 DoubleValue(distance),
207 "GridWidth",
208 UintegerValue(5),
209 "LayoutType",
210 StringValue("RowFirst"));
211 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
212 mobility.Install(c);
213
214 // Enable OLSR
216 Ipv4StaticRoutingHelper staticRouting;
217
219 list.Add(staticRouting, 0);
220 list.Add(olsr, 10);
221
222 InternetStackHelper internet;
223 internet.SetRoutingHelper(list); // has effect on the next Install ()
224 internet.Install(c);
225
227 NS_LOG_INFO("Assign IP Addresses.");
228 ipv4.SetBase("10.1.1.0", "255.255.255.0");
230
231 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
232 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(sinkNode), tid);
233 InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 80);
234 recvSink->Bind(local);
236
237 Ptr<Socket> source = Socket::CreateSocket(c.Get(sourceNode), tid);
238 InetSocketAddress remote = InetSocketAddress(i.GetAddress(sinkNode, 0), 80);
239 source->Connect(remote);
240
241 if (tracing == true)
242 {
243 AsciiTraceHelper ascii;
244 wifiPhy.EnableAsciiAll(ascii.CreateFileStream("wifi-simple-adhoc-grid.tr"));
245 wifiPhy.EnablePcap("wifi-simple-adhoc-grid", devices);
246 // Trace routing tables
247 Ptr<OutputStreamWrapper> routingStream =
248 Create<OutputStreamWrapper>("wifi-simple-adhoc-grid.routes", std::ios::out);
249 olsr.PrintRoutingTableAllEvery(Seconds(2), routingStream);
250 Ptr<OutputStreamWrapper> neighborStream =
251 Create<OutputStreamWrapper>("wifi-simple-adhoc-grid.neighbors", std::ios::out);
252 olsr.PrintNeighborCacheAllEvery(Seconds(2), neighborStream);
253
254 // To do-- enable an IP-level trace that shows forwarding events only
255 }
256
257 // Give OLSR time to converge-- 30 seconds perhaps
258 Simulator::Schedule(Seconds(30.0),
260 source,
262 numPackets,
263 interPacketInterval);
264
265 // Output what we are doing
266 NS_LOG_UNCOND("Testing from node " << sourceNode << " to " << sinkNode << " with grid distance "
267 << distance);
268
269 Simulator::Stop(Seconds(33.0));
270 Simulator::Run();
271 Simulator::Destroy();
272
273 return 0;
274}
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class that adds ns3::Ipv4ListRouting objects.
Helper class that adds ns3::Ipv4StaticRouting objects.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:42
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Hold variables of type string.
Definition: string.h:42
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:60
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:325
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:543
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:163
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
@ WIFI_STANDARD_80211b
devices
Definition: first.py:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:691
Definition: olsr.py:1
cmd
Definition: second.py:33
wifi
Definition: third.py:88
mobility
Definition: third.py:96
#define list
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.
uint32_t pktSize
packet size used for the simulation (in bytes)
static const uint32_t packetSize
Packet size generated at the AP.
void ReceivePacket(Ptr< Socket > socket)
Function called when a packet is received.
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Generate traffic.