A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
wifi-simple-adhoc-grid.cc
Go to the documentation of this file.
1
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2009 University of Washington
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
*/
19
20
//
21
// This program configures a grid (default 5x5) of nodes on an
22
// 802.11b physical layer, with
23
// 802.11b NICs in adhoc mode, and by default, sends one packet of 1000
24
// (application) bytes to node 1.
25
//
26
// The default layout is like this, on a 2-D grid.
27
//
28
// n20 n21 n22 n23 n24
29
// n15 n16 n17 n18 n19
30
// n10 n11 n12 n13 n14
31
// n5 n6 n7 n8 n9
32
// n0 n1 n2 n3 n4
33
//
34
// the layout is affected by the parameters given to GridPositionAllocator;
35
// by default, GridWidth is 5 and numNodes is 25..
36
//
37
// There are a number of command-line options available to control
38
// the default behavior. The list of available command-line options
39
// can be listed with the following command:
40
// ./waf --run "wifi-simple-adhoc-grid --help"
41
//
42
// Note that all ns-3 attributes (not just the ones exposed in the below
43
// script) can be changed at command line; see the ns-3 documentation.
44
//
45
// For instance, for this configuration, the physical layer will
46
// stop successfully receiving packets when distance increases beyond
47
// the default of 500m.
48
// To see this effect, try running:
49
//
50
// ./waf --run "wifi-simple-adhoc --distance=500"
51
// ./waf --run "wifi-simple-adhoc --distance=1000"
52
// ./waf --run "wifi-simple-adhoc --distance=1500"
53
//
54
// The source node and sink node can be changed like this:
55
//
56
// ./waf --run "wifi-simple-adhoc --sourceNode=20 --sinkNode=10"
57
//
58
// This script can also be helpful to put the Wifi layer into verbose
59
// logging mode; this command will turn on all wifi logging:
60
//
61
// ./waf --run "wifi-simple-adhoc-grid --verbose=1"
62
//
63
// By default, trace file writing is off-- to enable it, try:
64
// ./waf --run "wifi-simple-adhoc-grid --tracing=1"
65
//
66
// When you are done tracing, you will notice many pcap trace files
67
// in your directory. If you have tcpdump installed, you can try this:
68
//
69
// tcpdump -r wifi-simple-adhoc-grid-0-0.pcap -nn -tt
70
//
71
72
#include "ns3/core-module.h"
73
#include "ns3/network-module.h"
74
#include "ns3/mobility-module.h"
75
#include "ns3/config-store-module.h"
76
#include "ns3/wifi-module.h"
77
#include "ns3/internet-module.h"
78
#include "ns3/olsr-helper.h"
79
#include "ns3/ipv4-static-routing-helper.h"
80
#include "ns3/ipv4-list-routing-helper.h"
81
82
#include <iostream>
83
#include <fstream>
84
#include <vector>
85
#include <string>
86
87
NS_LOG_COMPONENT_DEFINE
(
"WifiSimpleAdhocGrid"
);
88
89
using namespace
ns3;
90
91
void
ReceivePacket
(
Ptr<Socket>
socket)
92
{
93
NS_LOG_UNCOND
(
"Received one packet!"
);
94
}
95
96
static
void
GenerateTraffic
(
Ptr<Socket>
socket, uint32_t pktSize,
97
uint32_t pktCount,
Time
pktInterval )
98
{
99
if
(pktCount > 0)
100
{
101
socket->
Send
(Create<Packet> (pktSize));
102
Simulator::Schedule
(pktInterval, &
GenerateTraffic
,
103
socket, pktSize,pktCount-1, pktInterval);
104
}
105
else
106
{
107
socket->
Close
();
108
}
109
}
110
111
112
int
main
(
int
argc,
char
*argv[])
113
{
114
std::string phyMode (
"DsssRate1Mbps"
);
115
double
distance = 500;
// m
116
uint32_t packetSize = 1000;
// bytes
117
uint32_t numPackets = 1;
118
uint32_t numNodes = 25;
// by default, 5x5
119
uint32_t sinkNode = 0;
120
uint32_t sourceNode = 24;
121
double
interval = 1.0;
// seconds
122
bool
verbose
=
false
;
123
bool
tracing =
false
;
124
125
CommandLine
cmd;
126
127
cmd.
AddValue
(
"phyMode"
,
"Wifi Phy mode"
, phyMode);
128
cmd.
AddValue
(
"distance"
,
"distance (m)"
, distance);
129
cmd.
AddValue
(
"packetSize"
,
"size of application packet sent"
, packetSize);
130
cmd.
AddValue
(
"numPackets"
,
"number of packets generated"
, numPackets);
131
cmd.
AddValue
(
"interval"
,
"interval (seconds) between packets"
, interval);
132
cmd.
AddValue
(
"verbose"
,
"turn on all WifiNetDevice log components"
, verbose);
133
cmd.
AddValue
(
"tracing"
,
"turn on ascii and pcap tracing"
, tracing);
134
cmd.
AddValue
(
"numNodes"
,
"number of nodes"
, numNodes);
135
cmd.
AddValue
(
"sinkNode"
,
"Receiver node number"
, sinkNode);
136
cmd.
AddValue
(
"sourceNode"
,
"Sender node number"
, sourceNode);
137
138
cmd.
Parse
(argc, argv);
139
// Convert to time object
140
Time
interPacketInterval = Seconds (interval);
141
142
// disable fragmentation for frames below 2200 bytes
143
Config::SetDefault
(
"ns3::WifiRemoteStationManager::FragmentationThreshold"
,
StringValue
(
"2200"
));
144
// turn off RTS/CTS for frames below 2200 bytes
145
Config::SetDefault
(
"ns3::WifiRemoteStationManager::RtsCtsThreshold"
,
StringValue
(
"2200"
));
146
// Fix non-unicast data rate to be the same as that of unicast
147
Config::SetDefault
(
"ns3::WifiRemoteStationManager::NonUnicastMode"
,
148
StringValue
(phyMode));
149
150
NodeContainer
c;
151
c.
Create
(numNodes);
152
153
// The below set of helpers will help us to put together the wifi NICs we want
154
WifiHelper
wifi;
155
if
(verbose)
156
{
157
wifi.
EnableLogComponents
();
// Turn on all Wifi logging
158
}
159
160
YansWifiPhyHelper
wifiPhy =
YansWifiPhyHelper::Default
();
161
// set it to zero; otherwise, gain will be added
162
wifiPhy.
Set
(
"RxGain"
,
DoubleValue
(-10) );
163
// ns-3 supports RadioTap and Prism tracing extensions for 802.11b
164
wifiPhy.
SetPcapDataLinkType
(
YansWifiPhyHelper::DLT_IEEE802_11_RADIO
);
165
166
YansWifiChannelHelper
wifiChannel;
167
wifiChannel.
SetPropagationDelay
(
"ns3::ConstantSpeedPropagationDelayModel"
);
168
wifiChannel.
AddPropagationLoss
(
"ns3::FriisPropagationLossModel"
);
169
wifiPhy.
SetChannel
(wifiChannel.
Create
());
170
171
// Add a non-QoS upper mac, and disable rate control
172
NqosWifiMacHelper
wifiMac =
NqosWifiMacHelper::Default
();
173
wifi.
SetStandard
(
WIFI_PHY_STANDARD_80211b
);
174
wifi.
SetRemoteStationManager
(
"ns3::ConstantRateWifiManager"
,
175
"DataMode"
,
StringValue
(phyMode),
176
"ControlMode"
,
StringValue
(phyMode));
177
// Set it to adhoc mode
178
wifiMac.
SetType
(
"ns3::AdhocWifiMac"
);
179
NetDeviceContainer
devices
= wifi.
Install
(wifiPhy, wifiMac, c);
180
181
MobilityHelper
mobility;
182
mobility.
SetPositionAllocator
(
"ns3::GridPositionAllocator"
,
183
"MinX"
,
DoubleValue
(0.0),
184
"MinY"
,
DoubleValue
(0.0),
185
"DeltaX"
,
DoubleValue
(distance),
186
"DeltaY"
,
DoubleValue
(distance),
187
"GridWidth"
,
UintegerValue
(5),
188
"LayoutType"
,
StringValue
(
"RowFirst"
));
189
mobility.
SetMobilityModel
(
"ns3::ConstantPositionMobilityModel"
);
190
mobility.
Install
(c);
191
192
// Enable OLSR
193
OlsrHelper
olsr;
194
Ipv4StaticRoutingHelper
staticRouting;
195
196
Ipv4ListRoutingHelper
list
;
197
list.
Add
(staticRouting, 0);
198
list.
Add
(olsr, 10);
199
200
InternetStackHelper
internet;
201
internet.
SetRoutingHelper
(list);
// has effect on the next Install ()
202
internet.
Install
(c);
203
204
Ipv4AddressHelper
ipv4;
205
NS_LOG_INFO
(
"Assign IP Addresses."
);
206
ipv4.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
207
Ipv4InterfaceContainer
i = ipv4.
Assign
(devices);
208
209
TypeId
tid =
TypeId::LookupByName
(
"ns3::UdpSocketFactory"
);
210
Ptr<Socket>
recvSink =
Socket::CreateSocket
(c.
Get
(sinkNode), tid);
211
InetSocketAddress
local =
InetSocketAddress
(
Ipv4Address::GetAny
(), 80);
212
recvSink->
Bind
(local);
213
recvSink->
SetRecvCallback
(
MakeCallback
(&
ReceivePacket
));
214
215
Ptr<Socket>
source =
Socket::CreateSocket
(c.
Get
(sourceNode), tid);
216
InetSocketAddress
remote =
InetSocketAddress
(i.
GetAddress
(sinkNode, 0), 80);
217
source->
Connect
(remote);
218
219
if
(tracing ==
true
)
220
{
221
AsciiTraceHelper
ascii;
222
wifiPhy.
EnableAsciiAll
(ascii.
CreateFileStream
(
"wifi-simple-adhoc-grid.tr"
));
223
wifiPhy.
EnablePcap
(
"wifi-simple-adhoc-grid"
, devices);
224
// Trace routing tables
225
Ptr<OutputStreamWrapper>
routingStream = Create<OutputStreamWrapper> (
"wifi-simple-adhoc-grid.routes"
, std::ios::out);
226
olsr.
PrintRoutingTableAllEvery
(Seconds (2), routingStream);
227
228
// To do-- enable an IP-level trace that shows forwarding events only
229
}
230
231
// Give OLSR time to converge-- 30 seconds perhaps
232
Simulator::Schedule
(Seconds (30.0), &
GenerateTraffic
,
233
source, packetSize, numPackets, interPacketInterval);
234
235
// Output what we are doing
236
NS_LOG_UNCOND
(
"Testing from node "
<< sourceNode <<
" to "
<< sinkNode <<
" with grid distance "
<< distance);
237
238
Simulator::Stop
(Seconds (32.0));
239
Simulator::Run
();
240
Simulator::Destroy
();
241
242
return
0;
243
}
244
examples
wireless
wifi-simple-adhoc-grid.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2