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 (std::vector<DsrSendBuffEntry>::const_iterator i = m_sendBuffer.begin();
60  i != m_sendBuffer.end();
61  ++i)
62  {
63  // NS_LOG_DEBUG ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
64  // ()->GetUid ()
65  // << " dst " << i->GetDestination () << " " <<
66  // entry.GetDestination ());
67 
68  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
69  (i->GetDestination() == entry.GetDestination()))
70  {
71  return false;
72  }
73  }
74 
75  entry.SetExpireTime(m_sendBufferTimeout); // Initialize the send buffer timeout
76  /*
77  * Drop the most aged packet when buffer reaches to max
78  */
79  if (m_sendBuffer.size() >= m_maxLen)
80  {
81  Drop(m_sendBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
82  m_sendBuffer.erase(m_sendBuffer.begin());
83  }
84  // enqueue the entry
85  m_sendBuffer.push_back(entry);
86  return true;
87 }
88 
89 void
91 {
92  NS_LOG_FUNCTION(this << dst);
93  Purge();
94  /*
95  * Drop the packet with destination address dst
96  */
97  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin(); i != m_sendBuffer.end();
98  ++i)
99  {
100  if (i->GetDestination() == dst)
101  {
102  Drop(*i, "DropPacketWithDst");
103  }
104  }
105  auto new_end =
106  std::remove_if(m_sendBuffer.begin(), m_sendBuffer.end(), [&](const DsrSendBuffEntry& en) {
107  return en.GetDestination() == dst;
108  });
109  m_sendBuffer.erase(new_end, m_sendBuffer.end());
110 }
111 
112 bool
114 {
115  Purge();
116  /*
117  * Dequeue the entry with destination address dst
118  */
119  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin(); i != m_sendBuffer.end();
120  ++i)
121  {
122  if (i->GetDestination() == dst)
123  {
124  entry = *i;
125  i = m_sendBuffer.erase(i);
126  NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
127  return true;
128  }
129  }
130  return false;
131 }
132 
133 bool
135 {
136  /*
137  * Make sure if the send buffer contains entry with certain dst
138  */
139  for (std::vector<DsrSendBuffEntry>::const_iterator i = m_sendBuffer.begin();
140  i != m_sendBuffer.end();
141  ++i)
142  {
143  if (i->GetDestination() == dst)
144  {
145  NS_LOG_DEBUG("Found the packet");
146  return true;
147  }
148  }
149  return false;
150 }
151 
152 struct IsExpired
153 {
159  bool operator()(const DsrSendBuffEntry& e) const
160  {
161  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
162  return (e.GetExpireTime() < Seconds(0));
163  }
164 };
165 
166 void
168 {
169  /*
170  * Purge the buffer to eliminate expired entries
171  */
172  NS_LOG_INFO("The send buffer size " << m_sendBuffer.size());
173  IsExpired pred;
174  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin(); i != m_sendBuffer.end();
175  ++i)
176  {
177  if (pred(*i))
178  {
179  NS_LOG_DEBUG("Dropping Queue Packets");
180  Drop(*i, "Drop out-dated packet ");
181  }
182  }
183  m_sendBuffer.erase(std::remove_if(m_sendBuffer.begin(), m_sendBuffer.end(), pred),
184  m_sendBuffer.end());
185 }
186 
187 void
188 DsrSendBuffer::Drop(DsrSendBuffEntry en, std::string reason)
189 {
190  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
191  // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
192  // Socket::ERROR_NOROUTETOHOST);
193 }
194 } // namespace dsr
195 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
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:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const DsrSendBuffEntry &e) const
comparison operator