A Discrete-Event Network Simulator
API
dsr-rsendbuff.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Yufei Cheng
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
18  *
19  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20  * ResiliNets Research Group https://resilinets.org/
21  * Information and Telecommunication Technology Center (ITTC)
22  * and Department of Electrical Engineering and Computer Science
23  * The University of Kansas Lawrence, KS USA.
24  *
25  * Work supported in part by NSF FIND (Future Internet Design) Program
26  * under grant CNS-0626918 (Postmodern Internet Architecture),
27  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28  * US Department of Defense (DoD), and ITTC at The University of Kansas.
29  */
30 
31 #include "dsr-rsendbuff.h"
32 
33 #include "ns3/ipv4-route.h"
34 #include "ns3/log.h"
35 #include "ns3/socket.h"
36 
37 #include <algorithm>
38 #include <functional>
39 
40 namespace ns3
41 {
42 
43 NS_LOG_COMPONENT_DEFINE("DsrSendBuffer");
44 
45 namespace dsr
46 {
47 
48 uint32_t
50 {
51  Purge();
52  return m_sendBuffer.size();
53 }
54 
55 bool
57 {
58  Purge();
59  for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
60  {
61  // NS_LOG_DEBUG ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
62  // ()->GetUid ()
63  // << " dst " << i->GetDestination () << " " <<
64  // entry.GetDestination ());
65 
66  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
67  (i->GetDestination() == entry.GetDestination()))
68  {
69  return false;
70  }
71  }
72 
73  entry.SetExpireTime(m_sendBufferTimeout); // Initialize the send buffer timeout
74  /*
75  * Drop the most aged packet when buffer reaches to max
76  */
77  if (m_sendBuffer.size() >= m_maxLen)
78  {
79  Drop(m_sendBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
80  m_sendBuffer.erase(m_sendBuffer.begin());
81  }
82  // enqueue the entry
83  m_sendBuffer.push_back(entry);
84  return true;
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION(this << dst);
91  Purge();
92  /*
93  * Drop the packet with destination address dst
94  */
95  for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
96  {
97  if (i->GetDestination() == dst)
98  {
99  Drop(*i, "DropPacketWithDst");
100  }
101  }
102  auto new_end =
103  std::remove_if(m_sendBuffer.begin(), m_sendBuffer.end(), [&](const DsrSendBuffEntry& en) {
104  return en.GetDestination() == dst;
105  });
106  m_sendBuffer.erase(new_end, m_sendBuffer.end());
107 }
108 
109 bool
111 {
112  Purge();
113  /*
114  * Dequeue the entry with destination address dst
115  */
116  for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
117  {
118  if (i->GetDestination() == dst)
119  {
120  entry = *i;
121  i = m_sendBuffer.erase(i);
122  NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
123  return true;
124  }
125  }
126  return false;
127 }
128 
129 bool
131 {
132  /*
133  * Make sure if the send buffer contains entry with certain dst
134  */
135  for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
136  {
137  if (i->GetDestination() == dst)
138  {
139  NS_LOG_DEBUG("Found the packet");
140  return true;
141  }
142  }
143  return false;
144 }
145 
146 struct IsExpired
147 {
153  bool operator()(const DsrSendBuffEntry& e) const
154  {
155  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
156  return (e.GetExpireTime() < Seconds(0));
157  }
158 };
159 
160 void
162 {
163  /*
164  * Purge the buffer to eliminate expired entries
165  */
166  NS_LOG_INFO("The send buffer size " << m_sendBuffer.size());
167  IsExpired pred;
168  for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
169  {
170  if (pred(*i))
171  {
172  NS_LOG_DEBUG("Dropping Queue Packets");
173  Drop(*i, "Drop out-dated packet ");
174  }
175  }
176  m_sendBuffer.erase(std::remove_if(m_sendBuffer.begin(), m_sendBuffer.end(), pred),
177  m_sendBuffer.end());
178 }
179 
180 void
181 DsrSendBuffer::Drop(DsrSendBuffEntry en, std::string reason)
182 {
183  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
184  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
185  // Socket::ERROR_NOROUTETOHOST);
186 }
187 } // namespace dsr
188 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
DSR Send Buffer Entry.
Definition: dsr-rsendbuff.h:48
Time GetExpireTime() const
Get expire time for entry.
Ipv4Address GetDestination() const
Get destination address of entry.
void SetExpireTime(Time exp)
Set expire time for entry.
Ptr< const Packet > GetPacket() const
Get pointer to entry's packet.
Definition: dsr-rsendbuff.h:84
uint32_t GetSize()
Number of entries.
bool Dequeue(Ipv4Address dst, DsrSendBuffEntry &entry)
Return first found (the earliest) entry for the given destination.
void Purge()
Remove all expired entries.
Time m_sendBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
std::vector< DsrSendBuffEntry > m_sendBuffer
The send buffer to cache unsent packet.
bool Enqueue(DsrSendBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
bool Find(Ipv4Address dst)
Check if a packet with destination dst exists in the queue.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
void Drop(DsrSendBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
void DropPacketWithDst(Ipv4Address dst)
Remove all packets with destination IP address dst.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const DsrSendBuffEntry &e) const
comparison operator