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 (i->GetDestination () == dst)
95  {
96  Drop (*i, "DropPacketWithDst");
97  }
98  }
99  auto new_end = std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (), [&](const DsrSendBuffEntry& en)
100  { return en.GetDestination () == dst; });
101  m_sendBuffer.erase (new_end, m_sendBuffer.end ());
102 }
103 
104 bool
106 {
107  Purge ();
108  /*
109  * Dequeue the entry with destination address dst
110  */
111  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin (); i != m_sendBuffer.end (); ++i)
112  {
113  if (i->GetDestination () == dst)
114  {
115  entry = *i;
116  i = m_sendBuffer.erase (i);
117  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
118  return true;
119  }
120  }
121  return false;
122 }
123 
124 bool
126 {
127  /*
128  * Make sure if the send buffer contains entry with certain dst
129  */
130  for (std::vector<DsrSendBuffEntry>::const_iterator i = m_sendBuffer.begin (); i
131  != m_sendBuffer.end (); ++i)
132  {
133  if (i->GetDestination () == dst)
134  {
135  NS_LOG_DEBUG ("Found the packet");
136  return true;
137  }
138  }
139  return false;
140 }
141 
142 struct IsExpired
143 {
149  bool
150  operator() (DsrSendBuffEntry const & e) const
151  {
152  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
153  return (e.GetExpireTime () < Seconds (0));
154  }
155 };
156 
157 void
159 {
160  /*
161  * Purge the buffer to eliminate expired entries
162  */
163  NS_LOG_INFO ("The send buffer size " << m_sendBuffer.size ());
164  IsExpired pred;
165  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin (); i
166  != m_sendBuffer.end (); ++i)
167  {
168  if (pred (*i))
169  {
170  NS_LOG_DEBUG ("Dropping Queue Packets");
171  Drop (*i, "Drop out-dated packet ");
172  }
173  }
174  m_sendBuffer.erase (std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (), pred),
175  m_sendBuffer.end ());
176 }
177 
178 void
179 DsrSendBuffer::Drop (DsrSendBuffEntry en, std::string reason)
180 {
181  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
182 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
183 // Socket::ERROR_NOROUTETOHOST);
184  return;
185 }
186 } // namespace dsr
187 } // namespace ns3
uint32_t GetSize()
Number of entries.
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 "...
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:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
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.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
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
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1288
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.