A Discrete-Event Network Simulator
API
dsr-rsendbuff.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Yufei Cheng
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: Yufei Cheng <yfcheng@ittc.ku.edu>
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 
32 #include "dsr-rsendbuff.h"
33 #include <algorithm>
34 #include <functional>
35 #include "ns3/ipv4-route.h"
36 #include "ns3/socket.h"
37 #include "ns3/log.h"
38 
39 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("DsrSendBuffer");
42 
43 namespace dsr {
44 
45 uint32_t
47 {
48  Purge ();
49  return m_sendBuffer.size ();
50 }
51 
52 bool
54 {
55  Purge ();
56  for (std::vector<DsrSendBuffEntry>::const_iterator i = m_sendBuffer.begin (); i
57  != m_sendBuffer.end (); ++i)
58  {
59 // NS_LOG_DEBUG ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid ()
60 // << " dst " << i->GetDestination () << " " << entry.GetDestination ());
61 
62  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ())
63  && (i->GetDestination () == entry.GetDestination ()))
64  {
65  return false;
66  }
67  }
68 
69  entry.SetExpireTime (m_sendBufferTimeout); // Initialize the send buffer timeout
70  /*
71  * Drop the most aged packet when buffer reaches to max
72  */
73  if (m_sendBuffer.size () >= m_maxLen)
74  {
75  Drop (m_sendBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet
76  m_sendBuffer.erase (m_sendBuffer.begin ());
77  }
78  // enqueue the entry
79  m_sendBuffer.push_back (entry);
80  return true;
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this << dst);
87  Purge ();
88  /*
89  * Drop the packet with destination address dst
90  */
91  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin (); i
92  != m_sendBuffer.end (); ++i)
93  {
94  if (IsEqual (*i, dst))
95  {
96  Drop (*i, "DropPacketWithDst");
97  }
98  }
99  m_sendBuffer.erase (std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (),
100  std::bind2nd (std::ptr_fun (DsrSendBuffer::IsEqual), dst)), m_sendBuffer.end ());
101 }
102 
103 bool
105 {
106  Purge ();
107  /*
108  * Dequeue the entry with destination address dst
109  */
110  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin (); i != m_sendBuffer.end (); ++i)
111  {
112  if (i->GetDestination () == dst)
113  {
114  entry = *i;
115  i = m_sendBuffer.erase (i);
116  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
117  return true;
118  }
119  }
120  return false;
121 }
122 
123 bool
125 {
126  /*
127  * Make sure if the send buffer contains entry with certain dst
128  */
129  for (std::vector<DsrSendBuffEntry>::const_iterator i = m_sendBuffer.begin (); i
130  != m_sendBuffer.end (); ++i)
131  {
132  if (i->GetDestination () == dst)
133  {
134  NS_LOG_DEBUG ("Found the packet");
135  return true;
136  }
137  }
138  return false;
139 }
140 
141 struct IsExpired
142 {
148  bool
149  operator() (DsrSendBuffEntry 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  /*
160  * Purge the buffer to eliminate expired entries
161  */
162  NS_LOG_INFO ("The send buffer size " << m_sendBuffer.size ());
163  IsExpired pred;
164  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin (); i
165  != m_sendBuffer.end (); ++i)
166  {
167  if (pred (*i))
168  {
169  NS_LOG_DEBUG ("Dropping Queue Packets");
170  Drop (*i, "Drop out-dated packet ");
171  }
172  }
173  m_sendBuffer.erase (std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (), pred),
174  m_sendBuffer.end ());
175 }
176 
177 void
178 DsrSendBuffer::Drop (DsrSendBuffEntry en, std::string reason)
179 {
180  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
181 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
182 // Socket::ERROR_NOROUTETOHOST);
183  return;
184 }
185 } // namespace dsr
186 } // namespace ns3
uint32_t GetSize()
Number of entries.
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
static bool IsEqual(DsrSendBuffEntry en, const Ipv4Address dst)
Check if the send buffer entry is the same or not.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Ipv4Address GetDestination() const
Get destination address of entry.
Definition: dsr-rsendbuff.h:95
std::vector< DsrSendBuffEntry > m_sendBuffer
The send buffer to cache unsent packet.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:280
void Purge()
Remove all expired entries.
Ptr< const Packet > GetPacket() const
Get pointer to entry&#39;s packet.
Definition: dsr-rsendbuff.h:79
void SetExpireTime(Time exp)
Set expire time for entry.
void DropPacketWithDst(Ipv4Address dst)
Remove all packets with destination IP address dst.
Time m_sendBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
void Drop(DsrSendBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
bool Find(Ipv4Address dst)
Check if a packet with destination dst exists in the queue.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time GetExpireTime() const
Get expire time for entry.
bool Enqueue(DsrSendBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
DSR Send Buffer Entry.
Definition: dsr-rsendbuff.h:45
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
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:272
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
bool operator()(DsrErrorBuffEntry const &e) const
comparison operator
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
IsExpired structure.
bool Dequeue(Ipv4Address dst, DsrSendBuffEntry &entry)
Return first found (the earliest) entry for the given destination.