A Discrete-Event Network Simulator
API
dsdv-packet-queue.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 "dsdv-packet-queue.h"
32 #include <algorithm>
33 #include <functional>
34 #include "ns3/ipv4-route.h"
35 #include "ns3/socket.h"
36 #include "ns3/log.h"
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("DsdvPacketQueue");
41 
42 namespace dsdv {
43 uint32_t
45 {
46  Purge ();
47  return m_queue.size ();
48 }
49 
50 bool
52 {
53  NS_LOG_FUNCTION ("Enqueing packet destined for" << entry.GetIpv4Header ().GetDestination ());
54  Purge ();
55  uint32_t numPacketswithdst;
56  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
57  != m_queue.end (); ++i)
58  {
59  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ())
60  && (i->GetIpv4Header ().GetDestination ()
61  == entry.GetIpv4Header ().GetDestination ()))
62  {
63  return false;
64  }
65  }
66  numPacketswithdst = GetCountForPacketsWithDst (entry.GetIpv4Header ().GetDestination ());
67  NS_LOG_DEBUG ("Number of packets with this destination: " << numPacketswithdst);
69  if (numPacketswithdst >= m_maxLenPerDst || m_queue.size () >= m_maxLen)
70  {
71  NS_LOG_DEBUG ("Max packets reached for this destination. Not queuing any further packets");
72  return false;
73  }
74  else
75  {
76  // NS_LOG_DEBUG("Packet size while enqueing "<<entry.GetPacket()->GetSize());
78  m_queue.push_back (entry);
79  return true;
80  }
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION ("Dropping packet to " << dst);
87  Purge ();
88  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i
89  != m_queue.end (); ++i)
90  {
91  if (IsEqual (*i, dst))
92  {
93  Drop (*i, "DropPacketWithDst ");
94  }
95  }
96  m_queue.erase (std::remove_if (m_queue.begin (), m_queue.end (),
97  std::bind2nd (std::ptr_fun (PacketQueue::IsEqual), dst)), m_queue.end ());
98 }
99 
100 bool
102 {
103  NS_LOG_FUNCTION ("Dequeueing packet destined for" << dst);
104  Purge ();
105  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i != m_queue.end (); ++i)
106  {
107  if (i->GetIpv4Header ().GetDestination () == dst)
108  {
109  entry = *i;
110  m_queue.erase (i);
111  return true;
112  }
113  }
114  return false;
115 }
116 
117 bool
119 {
120  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
121  != m_queue.end (); ++i)
122  {
123  if (i->GetIpv4Header ().GetDestination () == dst)
124  {
125  NS_LOG_DEBUG ("Find");
126  return true;
127  }
128  }
129  return false;
130 }
131 
132 uint32_t
134 {
135  uint32_t count = 0;
136  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
137  != m_queue.end (); ++i)
138  {
139  if (i->GetIpv4Header ().GetDestination () == dst)
140  {
141  count++;
142  }
143  }
144  return count;
145 }
146 
150 struct IsExpired
151 {
157  bool
158  operator() (QueueEntry const & e) const
159  {
160  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
161  return (e.GetExpireTime () < Seconds (0));
162  }
163 };
164 
165 void
167 {
168  // NS_LOG_DEBUG("Purging Queue");
169  IsExpired pred;
170  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i
171  != m_queue.end (); ++i)
172  {
173  if (pred (*i))
174  {
175  NS_LOG_DEBUG ("Dropping outdated Packets");
176  Drop (*i, "Drop outdated packet ");
177  }
178  }
179  m_queue.erase (std::remove_if (m_queue.begin (), m_queue.end (), pred),
180  m_queue.end ());
181 }
182 
183 void
184 PacketQueue::Drop (QueueEntry en, std::string reason)
185 {
186  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetIpv4Header ().GetDestination ());
187  // en.GetErrorCallback () (en.GetPacket (), en.GetIpv4Header (),
188  // Socket::ERROR_NOROUTETOHOST);
189  return;
190 }
191 
192 }
193 }
std::vector< QueueEntry > m_queue
the queue
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Ptr< const Packet > GetPacket() const
Get packet.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint32_t GetCountForPacketsWithDst(Ipv4Address dst)
Get count of packets with destination dst in the queue.
DSDV Queue Entry.
static bool IsEqual(QueueEntry en, const Ipv4Address dst)
Determine if queue entries are equal.
bool operator()(QueueEntry const &e) const
Check for expired entry.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void DropPacketWithDst(Ipv4Address dst)
Remove all packets with destination IP address dst.
bool Dequeue(Ipv4Address dst, QueueEntry &entry)
Return first found (the earliest) entry for given destination.
Time GetExpireTime() const
Get expire time.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
void Purge()
Remove all expired entries.
void SetExpireTime(Time exp)
Set expire time.
Ipv4Address GetDestination(void) const
Definition: ipv4-header.cc:304
IsExpired structure.
Ipv4Header GetIpv4Header() const
Get IP header.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
uint32_t GetSize()
Get the number of entries.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
void Drop(QueueEntry en, std::string reason)
Notify that the packet is dropped from queue due to timeout.
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
Time m_queueTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
uint32_t m_maxLenPerDst
The maximum number of packets that we allow per destination to buffer.
bool Enqueue(QueueEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...