A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
7 *
8 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
9 * ResiliNets Research Group https://resilinets.org/
10 * Information and Telecommunication Technology Center (ITTC)
11 * and Department of Electrical Engineering and Computer Science
12 * The University of Kansas Lawrence, KS USA.
13 *
14 * Work supported in part by NSF FIND (Future Internet Design) Program
15 * under grant CNS-0626918 (Postmodern Internet Architecture),
16 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
17 * US Department of Defense (DoD), and ITTC at The University of Kansas.
18 */
19
20#include "ns3/applications-module.h"
21#include "ns3/core-module.h"
22#include "ns3/dsr-module.h"
23#include "ns3/internet-module.h"
24#include "ns3/mobility-module.h"
25#include "ns3/network-module.h"
26#include "ns3/yans-wifi-helper.h"
27
28#include <sstream>
29
30using namespace ns3;
31
33
34int
35main(int argc, char* argv[])
36{
37 //
38 // Users may find it convenient to turn on explicit debugging
39 // for selected modules; the below lines suggest how to do this
40 //
41#if 0
42 LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL);
43 LogComponentEnable ("UdpL4Protocol", LOG_LEVEL_ALL);
44 LogComponentEnable ("UdpSocketImpl", LOG_LEVEL_ALL);
45 LogComponentEnable ("NetDevice", LOG_LEVEL_ALL);
46 LogComponentEnable ("Ipv4EndPointDemux", LOG_LEVEL_ALL);
47#endif
48
49#if 0
50 LogComponentEnable ("DsrOptions", LOG_LEVEL_ALL);
51 LogComponentEnable ("DsrHelper", LOG_LEVEL_ALL);
52 LogComponentEnable ("DsrRouting", LOG_LEVEL_ALL);
53 LogComponentEnable ("DsrOptionHeader", LOG_LEVEL_ALL);
54 LogComponentEnable ("DsrFsHeader", LOG_LEVEL_ALL);
55 LogComponentEnable ("DsrGraReplyTable", LOG_LEVEL_ALL);
56 LogComponentEnable ("DsrSendBuffer", LOG_LEVEL_ALL);
57 LogComponentEnable ("DsrRouteCache", LOG_LEVEL_ALL);
58 LogComponentEnable ("DsrMaintainBuffer", LOG_LEVEL_ALL);
59 LogComponentEnable ("DsrRreqTable", LOG_LEVEL_ALL);
60 LogComponentEnable ("DsrErrorBuffer", LOG_LEVEL_ALL);
61 LogComponentEnable ("DsrNetworkQueue", LOG_LEVEL_ALL);
62#endif
63
64 NS_LOG_INFO("creating the nodes");
65
66 // General parameters
67 uint32_t nWifis = 50;
68 uint32_t nSinks = 10;
69 double TotalTime = 600.0;
70 double dataTime = 500.0;
71 double ppers = 1;
73 double dataStart = 100.0; // start sending data at 100s
74
75 // mobility parameters
76 double pauseTime = 0.0;
77 double nodeSpeed = 20.0;
78 double txpDistance = 250.0;
79
80 std::string rate = "0.512kbps";
81 std::string dataMode("DsssRate11Mbps");
82 std::string phyMode("DsssRate11Mbps");
83
84 // Allow users to override the default parameters and set it to new ones from CommandLine.
85 CommandLine cmd(__FILE__);
86 cmd.AddValue("nWifis", "Number of wifi nodes", nWifis);
87 cmd.AddValue("nSinks", "Number of SINK traffic nodes", nSinks);
88 cmd.AddValue("rate", "CBR traffic rate(in kbps), Default:8", rate);
89 cmd.AddValue("nodeSpeed", "Node speed in RandomWayPoint model, Default:20", nodeSpeed);
90 cmd.AddValue("packetSize", "The packet size", packetSize);
91 cmd.AddValue("txpDistance", "Specify node's transmit range, Default:300", txpDistance);
92 cmd.AddValue("pauseTime", "pauseTime for mobility model, Default: 0", pauseTime);
93 cmd.Parse(argc, argv);
94
97
98 NodeContainer adhocNodes;
99 adhocNodes.Create(nWifis);
100 NetDeviceContainer allDevices;
101
102 NS_LOG_INFO("setting the default phy and channel parameters");
103 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
104 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("2200"));
105 // disable fragmentation for frames below 2200 bytes
106 Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold",
107 StringValue("2200"));
108
109 NS_LOG_INFO("setting the default phy and channel parameters ");
111 wifi.SetStandard(WIFI_STANDARD_80211b);
112 YansWifiPhyHelper wifiPhy;
113
114 YansWifiChannelHelper wifiChannel;
115 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
116 wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel",
117 "MaxRange",
118 DoubleValue(txpDistance));
119 wifiPhy.SetChannel(wifiChannel.Create());
120
121 // Add a mac and disable rate control
122 WifiMacHelper wifiMac;
123 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
124 "DataMode",
125 StringValue(dataMode),
126 "ControlMode",
127 StringValue(phyMode));
128
129 wifiMac.SetType("ns3::AdhocWifiMac");
130 allDevices = wifi.Install(wifiPhy, wifiMac, adhocNodes);
131
132 NS_LOG_INFO("Configure Tracing.");
133
134 AsciiTraceHelper ascii;
135 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("dsrtest.tr");
136 wifiPhy.EnableAsciiAll(stream);
137
138 MobilityHelper adhocMobility;
139 ObjectFactory pos;
140 pos.SetTypeId("ns3::RandomRectanglePositionAllocator");
141 pos.Set("X", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=300.0]"));
142 pos.Set("Y", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"));
143 Ptr<PositionAllocator> taPositionAlloc = pos.Create()->GetObject<PositionAllocator>();
144
145 std::ostringstream speedUniformRandomVariableStream;
146 speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" << nodeSpeed
147 << "]";
148
149 std::ostringstream pauseConstantRandomVariableStream;
150 pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" << pauseTime
151 << "]";
152
153 adhocMobility.SetMobilityModel(
154 "ns3::RandomWaypointMobilityModel",
155 // "Speed", StringValue
156 // ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"),
157 "Speed",
158 StringValue(speedUniformRandomVariableStream.str()),
159 "Pause",
160 StringValue(pauseConstantRandomVariableStream.str()),
161 "PositionAllocator",
162 PointerValue(taPositionAlloc));
163 adhocMobility.Install(adhocNodes);
164
166 DsrMainHelper dsrMain;
167 DsrHelper dsr;
168 internet.Install(adhocNodes);
169 dsrMain.Install(dsr, adhocNodes);
170
171 NS_LOG_INFO("assigning ip address");
173 address.SetBase("10.1.1.0", "255.255.255.0");
174 Ipv4InterfaceContainer allInterfaces;
175 allInterfaces = address.Assign(allDevices);
176
177 uint16_t port = 9;
178 double randomStartTime =
179 (1 / ppers) / nSinks; // distributed btw 1s evenly as we are sending 4pkt/s
180
181 for (uint32_t i = 0; i < nSinks; ++i)
182 {
183 PacketSinkHelper sink("ns3::UdpSocketFactory",
185 ApplicationContainer apps_sink = sink.Install(adhocNodes.Get(i));
186 apps_sink.Start(Seconds(0.0));
187 apps_sink.Stop(Seconds(TotalTime));
188
189 OnOffHelper onoff1("ns3::UdpSocketFactory",
190 Address(InetSocketAddress(allInterfaces.GetAddress(i), port)));
191 onoff1.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1.0]"));
192 onoff1.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
193 onoff1.SetAttribute("PacketSize", UintegerValue(packetSize));
194 onoff1.SetAttribute("DataRate", DataRateValue(DataRate(rate)));
195
196 ApplicationContainer apps1 = onoff1.Install(adhocNodes.Get(i + nWifis - nSinks));
197 apps1.Start(Seconds(dataStart + i * randomStartTime));
198 apps1.Stop(Seconds(dataTime + i * randomStartTime));
199 }
200
201 NS_LOG_INFO("Run Simulation.");
202 Simulator::Stop(Seconds(TotalTime));
205
206 return 0;
207}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
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.
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.
Class for representing data rates.
Definition data-rate.h:78
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
DSR helper class to manage creation of DSR routing instance and to insert it on a node as a sublayer ...
Definition dsr-helper.h:42
Helper class that adds DSR routing to nodes.
void Install(DsrHelper &dsrHelper, NodeContainer nodes)
Install routing to the nodes.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
void SetMobilityModel(std::string type, Ts &&... args)
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.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
AttributeValue implementation for Pointer.
Allocate a set of positions.
Smart pointer class similar to boost::intrusive_ptr.
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
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)
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
@ WIFI_STANDARD_80211b
address
Definition first.py:36
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
wifi
Definition third.py:84
static const uint32_t packetSize
Packet size generated at the AP.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44