A Discrete-Event Network Simulator
API
 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 
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 
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
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
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 
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);
212  recvSink->Bind (local);
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 ();
241 
242  return 0;
243 }
244 
void AddPropagationLoss(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
Manage ASCII trace files for device models.
Definition: trace-helper.h:141
an Inet address class
static Ipv4Address GetAny(void)
tuple devices
Definition: first.py:32
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:73
hold variables of type string
Definition: string.h:18
Make it easy to create and manage PHY objects for the yans model.
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
void ReceivePacket(Ptr< Socket > socket)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
aggregate IP/TCP/UDP functionality to existing Nodes.
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:38
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
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. ...
void Set(std::string name, const AttributeValue &v)
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
helps to create WifiNetDevice objects
Definition: wifi-helper.h:88
int main(int argc, char *argv[])
Include Radiotap link layer information.
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:102
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:96
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
static NqosWifiMacHelper Default(void)
Create a mac helper in a default working state.
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:70
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
Parse command-line arguments.
Definition: command-line.h:177
#define list
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
keep track of a set of node pointers.
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionaly.
manage and create wifi channel objects for the yans model.
void Add(const Ipv4RoutingHelper &routing, int16_t priority)
Helper class used to assign positions and mobility models to nodes.
static void EnableLogComponents(void)
Helper to enable all WifiNetDevice log components with one statement.
Definition: wifi-helper.cc:142
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void PrintRoutingTableAllEvery(Time printInterval, Ptr< OutputStreamWrapper > stream) const
prints the routing tables of all nodes at regular intervals specified by user.
Helper class that adds ns3::Ipv4StaticRouting objects.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:435
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Helper class that adds ns3::Ipv4ListRouting objects.
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPropagationDelay(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
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.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
virtual int Close(void)=0
Close a socket.
Hold a floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:49
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
bool verbose
static TypeId LookupByName(std::string name)
Definition: type-id.cc:535
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const