A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsdv-manet.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Hemanth Narra
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  * Author: Hemanth Narra <hemanth@ittc.ku.com>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  */
31 #include "ns3/core-module.h"
32 #include "ns3/network-module.h"
33 #include "ns3/applications-module.h"
34 #include "ns3/mobility-module.h"
35 #include "ns3/config-store-module.h"
36 #include "ns3/wifi-module.h"
37 #include "ns3/internet-module.h"
38 #include "ns3/dsdv-helper.h"
39 #include <iostream>
40 #include <cmath>
41 
42 using namespace ns3;
43 
44 uint16_t port = 9;
45 
46 NS_LOG_COMPONENT_DEFINE ("DsdvManetExample");
47 
49 {
50 public:
52  void CaseRun (uint32_t nWifis,
53  uint32_t nSinks,
54  double totalTime,
55  std::string rate,
56  std::string phyMode,
57  uint32_t nodeSpeed,
58  uint32_t periodicUpdateInterval,
59  uint32_t settlingTime,
60  double dataStart,
61  bool printRoutes,
62  std::string CSVfileName);
63 
64 private:
65  uint32_t m_nWifis;
66  uint32_t m_nSinks;
67  double m_totalTime;
68  std::string m_rate;
69  std::string m_phyMode;
70  uint32_t m_nodeSpeed;
72  uint32_t m_settlingTime;
73  double m_dataStart;
74  uint32_t bytesTotal;
75  uint32_t packetsReceived;
77  std::string m_CSVfileName;
78 
82 
83 private:
84  void CreateNodes ();
85  void CreateDevices (std::string tr_name);
86  void InstallInternetStack (std::string tr_name);
87  void InstallApplications ();
88  void SetupMobility ();
91  void CheckThroughput ();
92 
93 };
94 
95 int main (int argc, char **argv)
96 {
98  uint32_t nWifis = 30;
99  uint32_t nSinks = 10;
100  double totalTime = 100.0;
101  std::string rate ("8kbps");
102  std::string phyMode ("DsssRate11Mbps");
103  uint32_t nodeSpeed = 10; // in m/s
104  std::string appl = "all";
105  uint32_t periodicUpdateInterval = 15;
106  uint32_t settlingTime = 6;
107  double dataStart = 50.0;
108  bool printRoutingTable = true;
109  std::string CSVfileName = "DsdvManetExample.csv";
110 
111  CommandLine cmd;
112  cmd.AddValue ("nWifis", "Number of wifi nodes[Default:30]", nWifis);
113  cmd.AddValue ("nSinks", "Number of wifi sink nodes[Default:10]", nSinks);
114  cmd.AddValue ("totalTime", "Total Simulation time[Default:100]", totalTime);
115  cmd.AddValue ("phyMode", "Wifi Phy mode[Default:DsssRate11Mbps]", phyMode);
116  cmd.AddValue ("rate", "CBR traffic rate[Default:8kbps]", rate);
117  cmd.AddValue ("nodeSpeed", "Node speed in RandomWayPoint model[Default:10]", nodeSpeed);
118  cmd.AddValue ("periodicUpdateInterval", "Periodic Interval Time[Default=15]", periodicUpdateInterval);
119  cmd.AddValue ("settlingTime", "Settling Time before sending out an update for changed metric[Default=6]", settlingTime);
120  cmd.AddValue ("dataStart", "Time at which nodes start to transmit data[Default=50.0]", dataStart);
121  cmd.AddValue ("printRoutingTable", "print routing table for nodes[Default:1]", printRoutingTable);
122  cmd.AddValue ("CSVfileName", "The name of the CSV output file name[Default:DsdvManetExample.csv]", CSVfileName);
123  cmd.Parse (argc, argv);
124 
125  std::ofstream out (CSVfileName.c_str ());
126  out << "SimulationSecond," <<
127  "ReceiveRate," <<
128  "PacketsReceived," <<
129  "NumberOfSinks," <<
130  std::endl;
131  out.close ();
132 
133  SeedManager::SetSeed (12345);
134 
135  Config::SetDefault ("ns3::OnOffApplication::PacketSize", StringValue ("1000"));
136  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (rate));
137  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
138  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2000"));
139 
140  test = DsdvManetExample ();
141  test.CaseRun (nWifis, nSinks, totalTime, rate, phyMode, nodeSpeed, periodicUpdateInterval,
142  settlingTime, dataStart, printRoutingTable, CSVfileName);
143 
144  return 0;
145 }
146 
148  : bytesTotal (0),
149  packetsReceived (0)
150 {
151 }
152 
153 void
155 {
156  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " Received one packet!");
157  Ptr <Packet> packet;
158  while ((packet = socket->Recv ()))
159  {
160  bytesTotal += packet->GetSize ();
161  packetsReceived += 1;
162  }
163 }
164 
165 void
167 {
168  double kbs = (bytesTotal * 8.0) / 1000;
169  bytesTotal = 0;
170 
171  std::ofstream out (m_CSVfileName.c_str (), std::ios::app);
172 
173  out << (Simulator::Now ()).GetSeconds () << "," << kbs << "," << packetsReceived << "," << m_nSinks << std::endl;
174 
175  out.close ();
176  packetsReceived = 0;
177  Simulator::Schedule (Seconds (1.0), &DsdvManetExample::CheckThroughput, this);
178 }
179 
182 {
183 
184  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
185  Ptr <Socket> sink = Socket::CreateSocket (node, tid);
186  InetSocketAddress local = InetSocketAddress (addr, port);
187  sink->Bind (local);
189 
190  return sink;
191 }
192 
193 void
194 DsdvManetExample::CaseRun (uint32_t nWifis, uint32_t nSinks, double totalTime, std::string rate,
195  std::string phyMode, uint32_t nodeSpeed, uint32_t periodicUpdateInterval, uint32_t settlingTime,
196  double dataStart, bool printRoutes, std::string CSVfileName)
197 {
198  m_nWifis = nWifis;
199  m_nSinks = nSinks;
200  m_totalTime = totalTime;
201  m_rate = rate;
202  m_phyMode = phyMode;
203  m_nodeSpeed = nodeSpeed;
204  m_periodicUpdateInterval = periodicUpdateInterval;
205  m_settlingTime = settlingTime;
206  m_dataStart = dataStart;
207  m_printRoutes = printRoutes;
208  m_CSVfileName = CSVfileName;
209 
210  std::stringstream ss;
211  ss << m_nWifis;
212  std::string t_nodes = ss.str ();
213 
214  std::stringstream ss3;
215  ss3 << m_totalTime;
216  std::string sTotalTime = ss3.str ();
217 
218  std::string tr_name = "Dsdv_Manet_" + t_nodes + "Nodes_" + sTotalTime + "SimTime";
219  std::cout << "Trace file generated is " << tr_name << ".tr\n";
220 
221  CreateNodes ();
222  CreateDevices (tr_name);
223  SetupMobility ();
224  InstallInternetStack (tr_name);
226 
227  std::cout << "\nStarting simulation for " << m_totalTime << " s ...\n";
228 
229  CheckThroughput ();
230 
231  Simulator::Stop (Seconds (m_totalTime));
232  Simulator::Run ();
233  Simulator::Destroy ();
234 }
235 
236 void
238 {
239  std::cout << "Creating " << (unsigned) m_nWifis << " nodes.\n";
241  NS_ASSERT_MSG (m_nWifis > m_nSinks, "Sinks must be less or equal to the number of nodes in network");
242 }
243 
244 void
246 {
247  MobilityHelper mobility;
248  ObjectFactory pos;
249  pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
250  pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
251  pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
252 
253  std::ostringstream speedConstantRandomVariableStream;
254  speedConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant="
255  << m_nodeSpeed
256  << "]";
257 
258  Ptr <PositionAllocator> taPositionAlloc = pos.Create ()->GetObject <PositionAllocator> ();
259  mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", "Speed", StringValue (speedConstantRandomVariableStream.str ()),
260  "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"), "PositionAllocator", PointerValue (taPositionAlloc));
261  mobility.SetPositionAllocator (taPositionAlloc);
262  mobility.Install (nodes);
263 }
264 
265 void
266 DsdvManetExample::CreateDevices (std::string tr_name)
267 {
268  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
269  wifiMac.SetType ("ns3::AdhocWifiMac");
270  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
271  YansWifiChannelHelper wifiChannel;
272  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
273  wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
274  wifiPhy.SetChannel (wifiChannel.Create ());
275  WifiHelper wifi;
277  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue (m_phyMode), "ControlMode",
279  devices = wifi.Install (wifiPhy, wifiMac, nodes);
280 
281  AsciiTraceHelper ascii;
282  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (tr_name + ".tr"));
283  wifiPhy.EnablePcapAll (tr_name);
284 }
285 
286 void
288 {
289  DsdvHelper dsdv;
290  dsdv.Set ("PeriodicUpdateInterval", TimeValue (Seconds (m_periodicUpdateInterval)));
291  dsdv.Set ("SettlingTime", TimeValue (Seconds (m_settlingTime)));
293  stack.SetRoutingHelper (dsdv); // has effect on the next Install ()
294  stack.Install (nodes);
296  address.SetBase ("10.1.1.0", "255.255.255.0");
297  interfaces = address.Assign (devices);
298  if (m_printRoutes)
299  {
300  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ((tr_name + ".routes"), std::ios::out);
301  dsdv.PrintRoutingTableAllAt (Seconds (m_periodicUpdateInterval), routingStream);
302  }
303 }
304 
305 void
307 {
308  for (uint32_t i = 0; i <= m_nSinks - 1; i++ )
309  {
310  Ptr<Node> node = NodeList::GetNode (i);
311  Ipv4Address nodeAddress = node->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
312  Ptr<Socket> sink = SetupPacketReceive (nodeAddress, node);
313  }
314 
315  for (uint32_t clientNode = 0; clientNode <= m_nWifis - 1; clientNode++ )
316  {
317  for (uint32_t j = 0; j <= m_nSinks - 1; j++ )
318  {
319  OnOffHelper onoff1 ("ns3::UdpSocketFactory", Address (InetSocketAddress (interfaces.GetAddress (j), port)));
320  onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
321  onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
322 
323  if (j != clientNode)
324  {
325  ApplicationContainer apps1 = onoff1.Install (nodes.Get (clientNode));
326  Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable> ();
327  apps1.Start (Seconds (var->GetValue (m_dataStart, m_dataStart + 1)));
328  apps1.Stop (Seconds (m_totalTime));
329  }
330  }
331  }
332 }
333 
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())
holds a vector of ns3::Application pointers.
Ipv4InterfaceContainer interfaces
Definition: dsdv-manet.cc:81
Manage ASCII trace files for device models.
Definition: trace-helper.h:128
an Inet address class
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
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:19
Make it easy to create and manage PHY objects for the yans model.
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)
Definition: dsdv-manet.cc:194
uint32_t packetsReceived
Definition: dsdv-manet.cc:75
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())
void ReceivePacket(Ptr< Socket > socket)
void SetupMobility()
Definition: dsdv-manet.cc:245
std::string m_CSVfileName
Definition: dsdv-manet.cc:77
double m_totalTime
Definition: dsdv-manet.cc:67
void SetTypeId(TypeId tid)
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Definition: packet.h:650
void InstallApplications()
Definition: dsdv-manet.cc:306
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. ...
helps to create WifiNetDevice objects
Definition: wifi-helper.h:88
double m_dataStart
Definition: dsdv-manet.cc:73
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:102
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
void CreateDevices(std::string tr_name)
Definition: dsdv-manet.cc:266
void SetChannel(Ptr< YansWifiChannel > channel)
void ReceivePacket(Ptr< Socket >)
Definition: dsdv-manet.cc:154
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
uint32_t m_nodeSpeed
Definition: dsdv-manet.cc:70
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 ...
hold objects of type ns3::Time
Definition: nstime.h:961
NodeContainer nodes
Definition: dsdv-manet.cc:79
Ptr< Object > Create(void) const
std::string m_rate
Definition: dsdv-manet.cc:68
uint32_t m_settlingTime
Definition: dsdv-manet.cc:72
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:1238
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
NetDeviceContainer devices
Definition: dsdv-manet.cc:80
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
Parse command-line arguments.
Definition: command-line.h:152
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
uint32_t m_nSinks
Definition: dsdv-manet.cc:66
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
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.
hold objects of type Ptr
Definition: pointer.h:33
uint32_t m_nWifis
Definition: dsdv-manet.cc:65
void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream) const
prints the routing tables of all nodes at a particular time.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
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())
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
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)
Definition: log.h:377
uint32_t bytesTotal
Definition: dsdv-manet.cc:74
void Set(std::string name, const AttributeValue &value)
manage and create wifi channel objects for the yans model.
void InstallInternetStack(std::string tr_name)
Definition: dsdv-manet.cc:287
std::string m_phyMode
Definition: dsdv-manet.cc:69
tuple stack
Definition: first.py:34
static void SetSeed(uint32_t seed)
set the seed it will duplicate the seed value 6 times
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Helper class used to assign positions and mobility models to nodes.
instantiate subclasses of ns3::Object.
void Set(std::string name, const AttributeValue &value)
Definition: dsdv-helper.cc:62
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
int main(int argc, char **argv)
Definition: dsdv-manet.cc:95
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
uint32_t m_periodicUpdateInterval
Definition: dsdv-manet.cc:71
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
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.
tuple address
Definition: first.py:37
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 SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
Helper class that adds DSDV routing to nodes.
Definition: dsdv-helper.h:45
Ptr< T > GetObject(void) const
Definition: object.h:361
a unique identifier for an interface.
Definition: type-id.h:49
void test(void)
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
Ptr< Socket > SetupPacketReceive(Ipv4Address, Ptr< Node >)
Definition: dsdv-manet.cc:181
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Allocate a set of positions.
void CheckThroughput()
Definition: dsdv-manet.cc:166