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 https://resilinets.org/
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
39namespace ns3 {
40
41NS_LOG_COMPONENT_DEFINE ("DsrSendBuffer");
42
43namespace dsr {
44
47{
48 Purge ();
49 return m_sendBuffer.size ();
50}
51
52bool
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
83void
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
104bool
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
124bool
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
142struct IsExpired
143{
149 bool
151 {
152 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
153 return (e.GetExpireTime () < Seconds (0));
154 }
155};
156
157void
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
178void
179DsrSendBuffer::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}
185} // namespace dsr
186} // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
DSR Send Buffer Entry.
Definition: dsr-rsendbuff.h:46
Time GetExpireTime() const
Get expire time for entry.
Ipv4Address GetDestination() const
Get destination address of entry.
Definition: dsr-rsendbuff.h:95
void SetExpireTime(Time exp)
Set expire time for entry.
Ptr< const Packet > GetPacket() const
Get pointer to entry's packet.
Definition: dsr-rsendbuff.h:79
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:206
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:274
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:290
#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:282
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1245
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(DsrErrorBuffEntry const &e) const
comparison operator