A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("DsdvPacketQueue");
39 
40 namespace ns3 {
41 namespace dsdv {
42 uint32_t
44 {
45  Purge ();
46  return m_queue.size ();
47 }
48 
49 bool
51 {
52  NS_LOG_FUNCTION ("Enqueing packet destined for" << entry.GetIpv4Header ().GetDestination ());
53  Purge ();
54  uint32_t numPacketswithdst;
55  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
56  != m_queue.end (); ++i)
57  {
58  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ())
59  && (i->GetIpv4Header ().GetDestination ()
60  == entry.GetIpv4Header ().GetDestination ()))
61  {
62  return false;
63  }
64  }
65  numPacketswithdst = GetCountForPacketsWithDst (entry.GetIpv4Header ().GetDestination ());
66  NS_LOG_DEBUG ("Number of packets with this destination: " << numPacketswithdst);
68  if (numPacketswithdst >= m_maxLenPerDst || m_queue.size () >= m_maxLen)
69  {
70  NS_LOG_DEBUG ("Max packets reached for this destination. Not queuing any further packets");
71  return false;
72  }
73  else
74  {
75  // NS_LOG_DEBUG("Packet size while enqueing "<<entry.GetPacket()->GetSize());
77  m_queue.push_back (entry);
78  return true;
79  }
80 }
81 
82 void
84 {
85  NS_LOG_FUNCTION ("Dropping packet to " << dst);
86  Purge ();
87  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i
88  != m_queue.end (); ++i)
89  {
90  if (IsEqual (*i, dst))
91  {
92  Drop (*i, "DropPacketWithDst ");
93  }
94  }
95  m_queue.erase (std::remove_if (m_queue.begin (), m_queue.end (),
96  std::bind2nd (std::ptr_fun (PacketQueue::IsEqual), dst)), m_queue.end ());
97 }
98 
99 bool
101 {
102  NS_LOG_FUNCTION ("Dequeueing packet destined for" << dst);
103  Purge ();
104  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i != m_queue.end (); ++i)
105  {
106  if (i->GetIpv4Header ().GetDestination () == dst)
107  {
108  entry = *i;
109  m_queue.erase (i);
110  return true;
111  }
112  }
113  return false;
114 }
115 
116 bool
118 {
119  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
120  != m_queue.end (); ++i)
121  {
122  if (i->GetIpv4Header ().GetDestination () == dst)
123  {
124  NS_LOG_DEBUG ("Find");
125  return true;
126  }
127  }
128  return false;
129 }
130 
131 uint32_t
133 {
134  uint32_t count = 0;
135  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
136  != m_queue.end (); ++i)
137  {
138  if (i->GetIpv4Header ().GetDestination () == dst)
139  {
140  count++;
141  }
142  }
143  return count;
144 }
145 
146 struct IsExpired
147 {
148  bool
149  operator() (QueueEntry const & e) const
150  {
151  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
152  return (e.GetExpireTime () < Seconds (0));
153  }
154 };
155 
156 void
158 {
159  // NS_LOG_DEBUG("Purging Queue");
160  IsExpired pred;
161  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i
162  != m_queue.end (); ++i)
163  {
164  if (pred (*i))
165  {
166  NS_LOG_DEBUG ("Dropping outdated Packets");
167  Drop (*i, "Drop outdated packet ");
168  }
169  }
170  m_queue.erase (std::remove_if (m_queue.begin (), m_queue.end (), pred),
171  m_queue.end ());
172 }
173 
174 void
175 PacketQueue::Drop (QueueEntry en, std::string reason)
176 {
177  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetIpv4Header ().GetDestination ());
178  // en.GetErrorCallback () (en.GetPacket (), en.GetIpv4Header (),
179  // Socket::ERROR_NOROUTETOHOST);
180  return;
181 }
182 
183 }
184 }
std::vector< QueueEntry > m_queue
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Ipv4Header GetIpv4Header() const
uint64_t GetUid(void) const
A packet is allocated a new uid when it is created empty or with zero-filled payload.
Definition: packet.cc:393
Ipv4Address GetDestination(void) const
Definition: ipv4-header.cc:304
uint32_t GetCountForPacketsWithDst(Ipv4Address dst)
Get count of packets with destination dst in the queue.
NS_LOG_COMPONENT_DEFINE("DsdvPacketQueue")
DSDV Queue Entry.
static bool IsEqual(QueueEntry en, const Ipv4Address dst)
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
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.
void Purge()
Remove all expired entries.
void SetExpireTime(Time exp)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
Ptr< const Packet > GetPacket() const
bool operator()(QueueEntry const &e) const
uint32_t GetSize()
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 packet is dropped from queue by 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.
Time GetExpireTime() const
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...