A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsdv-manet.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hemanth Narra
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 * Author: Hemanth Narra <hemanth@ittc.ku.com>
18 *
19 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20 * ResiliNets Research Group https://resilinets.org/
21 * Information and Telecommunication Technology Center (ITTC)
22 * and Department of Electrical Engineering and Computer Science
23 * The University of Kansas Lawrence, KS USA.
24 *
25 * Work supported in part by NSF FIND (Future Internet Design) Program
26 * under grant CNS-0626918 (Postmodern Internet Architecture),
27 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28 * US Department of Defense (DoD), and ITTC at The University of Kansas.
29 */
30
31#include "ns3/applications-module.h"
32#include "ns3/core-module.h"
33#include "ns3/dsdv-helper.h"
34#include "ns3/internet-module.h"
35#include "ns3/mobility-module.h"
36#include "ns3/network-module.h"
37#include "ns3/yans-wifi-helper.h"
38
39#include <cmath>
40#include <iostream>
41
42using namespace ns3;
43
44uint16_t port = 9;
45
46NS_LOG_COMPONENT_DEFINE("DsdvManetExample");
47
60{
61 public:
77 void CaseRun(uint32_t nWifis,
78 uint32_t nSinks,
79 double totalTime,
80 std::string rate,
81 std::string phyMode,
82 uint32_t nodeSpeed,
83 uint32_t periodicUpdateInterval,
84 uint32_t settlingTime,
85 double dataStart,
86 bool printRoutes,
87 std::string CSVfileName);
88
89 private:
92 double m_totalTime;
93 std::string m_rate;
94 std::string m_phyMode;
98 double m_dataStart;
102 std::string m_CSVfileName;
103
107
108 private:
110 void CreateNodes();
115 void CreateDevices(std::string tr_name);
120 void InstallInternetStack(std::string tr_name);
122 void InstallApplications();
124 void SetupMobility();
129 void ReceivePacket(Ptr<Socket> socket);
138 void CheckThroughput();
139};
140
141int
142main(int argc, char** argv)
143{
145 uint32_t nWifis = 30;
146 uint32_t nSinks = 10;
147 double totalTime = 100.0;
148 std::string rate("8kbps");
149 std::string phyMode("DsssRate11Mbps");
150 uint32_t nodeSpeed = 10; // in m/s
151 std::string appl = "all";
152 uint32_t periodicUpdateInterval = 15;
153 uint32_t settlingTime = 6;
154 double dataStart = 50.0;
155 bool printRoutingTable = true;
156 std::string CSVfileName = "DsdvManetExample.csv";
157
158 CommandLine cmd(__FILE__);
159 cmd.AddValue("nWifis", "Number of wifi nodes[Default:30]", nWifis);
160 cmd.AddValue("nSinks", "Number of wifi sink nodes[Default:10]", nSinks);
161 cmd.AddValue("totalTime", "Total Simulation time[Default:100]", totalTime);
162 cmd.AddValue("phyMode", "Wifi Phy mode[Default:DsssRate11Mbps]", phyMode);
163 cmd.AddValue("rate", "CBR traffic rate[Default:8kbps]", rate);
164 cmd.AddValue("nodeSpeed", "Node speed in RandomWayPoint model[Default:10]", nodeSpeed);
165 cmd.AddValue("periodicUpdateInterval",
166 "Periodic Interval Time[Default=15]",
167 periodicUpdateInterval);
168 cmd.AddValue("settlingTime",
169 "Settling Time before sending out an update for changed metric[Default=6]",
170 settlingTime);
171 cmd.AddValue("dataStart",
172 "Time at which nodes start to transmit data[Default=50.0]",
173 dataStart);
174 cmd.AddValue("printRoutingTable",
175 "print routing table for nodes[Default:1]",
176 printRoutingTable);
177 cmd.AddValue("CSVfileName",
178 "The name of the CSV output file name[Default:DsdvManetExample.csv]",
179 CSVfileName);
180 cmd.Parse(argc, argv);
181
182 std::ofstream out(CSVfileName);
183 out << "SimulationSecond,"
184 << "ReceiveRate,"
185 << "PacketsReceived,"
186 << "NumberOfSinks," << std::endl;
187 out.close();
188
190
191 Config::SetDefault("ns3::OnOffApplication::PacketSize", StringValue("1000"));
192 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue(rate));
193 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
194 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("2000"));
195
197 test.CaseRun(nWifis,
198 nSinks,
199 totalTime,
200 rate,
201 phyMode,
202 nodeSpeed,
203 periodicUpdateInterval,
204 settlingTime,
205 dataStart,
206 printRoutingTable,
207 CSVfileName);
208
209 return 0;
210}
211
213 : bytesTotal(0),
215{
216}
217
218void
220{
221 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << " Received one packet!");
222 Ptr<Packet> packet;
223 while ((packet = socket->Recv()))
224 {
225 bytesTotal += packet->GetSize();
226 packetsReceived += 1;
227 }
228}
229
230void
232{
233 double kbs = (bytesTotal * 8.0) / 1000;
234 bytesTotal = 0;
235
236 std::ofstream out(m_CSVfileName, std::ios::app);
237
238 out << (Simulator::Now()).GetSeconds() << "," << kbs << "," << packetsReceived << ","
239 << m_nSinks << std::endl;
240
241 out.close();
242 packetsReceived = 0;
244}
245
248{
249 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
252 sink->Bind(local);
253 sink->SetRecvCallback(MakeCallback(&DsdvManetExample::ReceivePacket, this));
254
255 return sink;
256}
257
258void
260 uint32_t nSinks,
261 double totalTime,
262 std::string rate,
263 std::string phyMode,
264 uint32_t nodeSpeed,
265 uint32_t periodicUpdateInterval,
266 uint32_t settlingTime,
267 double dataStart,
268 bool printRoutes,
269 std::string CSVfileName)
270{
271 m_nWifis = nWifis;
272 m_nSinks = nSinks;
273 m_totalTime = totalTime;
274 m_rate = rate;
275 m_phyMode = phyMode;
276 m_nodeSpeed = nodeSpeed;
277 m_periodicUpdateInterval = periodicUpdateInterval;
278 m_settlingTime = settlingTime;
279 m_dataStart = dataStart;
280 m_printRoutes = printRoutes;
281 m_CSVfileName = CSVfileName;
282
283 std::stringstream ss;
284 ss << m_nWifis;
285 std::string t_nodes = ss.str();
286
287 std::stringstream ss3;
288 ss3 << m_totalTime;
289 std::string sTotalTime = ss3.str();
290
291 std::string tr_name = "Dsdv_Manet_" + t_nodes + "Nodes_" + sTotalTime + "SimTime";
292 std::cout << "Trace file generated is " << tr_name << ".tr\n";
293
294 CreateNodes();
295 CreateDevices(tr_name);
297 InstallInternetStack(tr_name);
299
300 std::cout << "\nStarting simulation for " << m_totalTime << " s ...\n";
301
303
307}
308
309void
311{
312 std::cout << "Creating " << (unsigned)m_nWifis << " nodes.\n";
315 "Sinks must be less or equal to the number of nodes in network");
316}
317
318void
320{
321 MobilityHelper mobility;
322 ObjectFactory pos;
323 pos.SetTypeId("ns3::RandomRectanglePositionAllocator");
324 pos.Set("X", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
325 pos.Set("Y", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
326
327 std::ostringstream speedConstantRandomVariableStream;
328 speedConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" << m_nodeSpeed
329 << "]";
330
331 Ptr<PositionAllocator> taPositionAlloc = pos.Create()->GetObject<PositionAllocator>();
332 mobility.SetMobilityModel("ns3::RandomWaypointMobilityModel",
333 "Speed",
334 StringValue(speedConstantRandomVariableStream.str()),
335 "Pause",
336 StringValue("ns3::ConstantRandomVariable[Constant=2.0]"),
337 "PositionAllocator",
338 PointerValue(taPositionAlloc));
339 mobility.SetPositionAllocator(taPositionAlloc);
340 mobility.Install(nodes);
341}
342
343void
345{
346 WifiMacHelper wifiMac;
347 wifiMac.SetType("ns3::AdhocWifiMac");
348 YansWifiPhyHelper wifiPhy;
349 YansWifiChannelHelper wifiChannel;
350 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
351 wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
352 wifiPhy.SetChannel(wifiChannel.Create());
353 WifiHelper wifi;
354 wifi.SetStandard(WIFI_STANDARD_80211b);
355 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
356 "DataMode",
358 "ControlMode",
360 devices = wifi.Install(wifiPhy, wifiMac, nodes);
361
362 AsciiTraceHelper ascii;
363 wifiPhy.EnableAsciiAll(ascii.CreateFileStream(tr_name + ".tr"));
364 wifiPhy.EnablePcapAll(tr_name);
365}
366
367void
369{
370 DsdvHelper dsdv;
371 dsdv.Set("PeriodicUpdateInterval", TimeValue(Seconds(m_periodicUpdateInterval)));
372 dsdv.Set("SettlingTime", TimeValue(Seconds(m_settlingTime)));
374 stack.SetRoutingHelper(dsdv); // has effect on the next Install ()
375 stack.Install(nodes);
376 Ipv4AddressHelper address;
377 address.SetBase("10.1.1.0", "255.255.255.0");
378 interfaces = address.Assign(devices);
379 if (m_printRoutes)
380 {
381 Ptr<OutputStreamWrapper> routingStream =
382 Create<OutputStreamWrapper>((tr_name + ".routes"), std::ios::out);
384 }
385}
386
387void
389{
390 for (uint32_t i = 0; i <= m_nSinks - 1; i++)
391 {
393 Ipv4Address nodeAddress = node->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal();
394 Ptr<Socket> sink = SetupPacketReceive(nodeAddress, node);
395 }
396
397 for (uint32_t clientNode = 0; clientNode <= m_nWifis - 1; clientNode++)
398 {
399 for (uint32_t j = 0; j <= m_nSinks - 1; j++)
400 {
401 OnOffHelper onoff1("ns3::UdpSocketFactory",
403 onoff1.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1.0]"));
404 onoff1.SetAttribute("OffTime",
405 StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
406
407 if (j != clientNode)
408 {
409 ApplicationContainer apps1 = onoff1.Install(nodes.Get(clientNode));
410 Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable>();
411 apps1.Start(Seconds(var->GetValue(m_dataStart, m_dataStart + 1)));
412 apps1.Stop(Seconds(m_totalTime));
413 }
414 }
415 }
416}
DSDV Manet example.
Definition: dsdv-manet.cc:60
uint32_t m_nSinks
number of receiver nodes
Definition: dsdv-manet.cc:91
void InstallApplications()
Create data sinks and sources.
Definition: dsdv-manet.cc:388
NodeContainer nodes
the collection of nodes
Definition: dsdv-manet.cc:104
void ReceivePacket(Ptr< Socket > socket)
Packet receive function.
Definition: dsdv-manet.cc:219
double m_dataStart
time to start data transmissions (seconds)
Definition: dsdv-manet.cc:98
std::string m_CSVfileName
CSV file name.
Definition: dsdv-manet.cc:102
uint32_t packetsReceived
total packets received by all nodes
Definition: dsdv-manet.cc:100
std::string m_rate
network bandwidth
Definition: dsdv-manet.cc:93
uint32_t bytesTotal
total bytes received by all nodes
Definition: dsdv-manet.cc:99
void CreateNodes()
Create and initialize all nodes.
Definition: dsdv-manet.cc:310
bool m_printRoutes
print routing table
Definition: dsdv-manet.cc:101
void InstallInternetStack(std::string tr_name)
Create network.
Definition: dsdv-manet.cc:368
uint32_t m_nodeSpeed
mobility speed
Definition: dsdv-manet.cc:95
void CreateDevices(std::string tr_name)
Create and initialize all devices.
Definition: dsdv-manet.cc:344
void CaseRun(uint32_t nWifis, uint32_t nSinks, double totalTime, std::string rate, std::string phyMode, uint32_t nodeSpeed, uint32_t periodicUpdateInterval, uint32_t settlingTime, double dataStart, bool printRoutes, std::string CSVfileName)
Run function.
Definition: dsdv-manet.cc:259
uint32_t m_settlingTime
routing setting time
Definition: dsdv-manet.cc:97
Ipv4InterfaceContainer interfaces
the collection of interfaces
Definition: dsdv-manet.cc:106
void CheckThroughput()
Check network throughput.
Definition: dsdv-manet.cc:231
void SetupMobility()
Setup mobility model.
Definition: dsdv-manet.cc:319
NetDeviceContainer devices
the collection of devices
Definition: dsdv-manet.cc:105
double m_totalTime
total simulation time (in seconds)
Definition: dsdv-manet.cc:92
std::string m_phyMode
remote station manager data mode
Definition: dsdv-manet.cc:94
Ptr< Socket > SetupPacketReceive(Ipv4Address addr, Ptr< Node > node)
Setup packet receivers.
Definition: dsdv-manet.cc:247
uint32_t m_periodicUpdateInterval
routing update interval
Definition: dsdv-manet.cc:96
uint32_t m_nWifis
total number of nodes
Definition: dsdv-manet.cc:90
a polymophic address class
Definition: address.h:101
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.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
void SetAttribute(const std::string &name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
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:174
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
Helper class that adds DSDV routing to nodes.
Definition: dsdv-helper.h:47
void Set(std::string name, const AttributeValue &value)
Definition: dsdv-helper.cc:65
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.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at a particular time.
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.
static Ptr< Node > GetNode(uint32_t n)
Definition: node-list.cc:251
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:522
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Pointer.
Definition: pointer.h:48
Allocate a set of positions.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:72
Hold variables of type string.
Definition: string.h:56
@ S
second
Definition: nstime.h:116
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
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:44
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ WIFI_STANDARD_80211b
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:704
-ns3 Test suite for the ns3 wrapper script
std::map< Mac48Address, uint64_t > packetsReceived
Map that stores the total packets received per STA (and addressed to that STA)
Definition: wifi-bianchi.cc:72
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55