A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-rsendbuff.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
7 *
8 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
9 * ResiliNets Research Group https://resilinets.org/
10 * Information and Telecommunication Technology Center (ITTC)
11 * and Department of Electrical Engineering and Computer Science
12 * The University of Kansas Lawrence, KS USA.
13 *
14 * Work supported in part by NSF FIND (Future Internet Design) Program
15 * under grant CNS-0626918 (Postmodern Internet Architecture),
16 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
17 * US Department of Defense (DoD), and ITTC at The University of Kansas.
18 */
19
20#include "dsr-rsendbuff.h"
21
22#include "ns3/ipv4-route.h"
23#include "ns3/log.h"
24#include "ns3/socket.h"
25
26#include <algorithm>
27#include <functional>
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("DsrSendBuffer");
33
34namespace dsr
35{
36
39{
40 Purge();
41 return m_sendBuffer.size();
42}
43
44bool
46{
47 Purge();
48 for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
49 {
50 // NS_LOG_DEBUG ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket
51 // ()->GetUid ()
52 // << " dst " << i->GetDestination () << " " <<
53 // entry.GetDestination ());
54
55 if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
56 (i->GetDestination() == entry.GetDestination()))
57 {
58 return false;
59 }
60 }
61
62 entry.SetExpireTime(m_sendBufferTimeout); // Initialize the send buffer timeout
63 /*
64 * Drop the most aged packet when buffer reaches to max
65 */
66 if (m_sendBuffer.size() >= m_maxLen)
67 {
68 Drop(m_sendBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
69 m_sendBuffer.erase(m_sendBuffer.begin());
70 }
71 // enqueue the entry
72 m_sendBuffer.push_back(entry);
73 return true;
74}
75
76void
78{
79 NS_LOG_FUNCTION(this << dst);
80 Purge();
81 /*
82 * Drop the packet with destination address dst
83 */
84 for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
85 {
86 if (i->GetDestination() == dst)
87 {
88 Drop(*i, "DropPacketWithDst");
89 }
90 }
91 auto new_end =
92 std::remove_if(m_sendBuffer.begin(), m_sendBuffer.end(), [&](const DsrSendBuffEntry& en) {
93 return en.GetDestination() == dst;
94 });
95 m_sendBuffer.erase(new_end, m_sendBuffer.end());
96}
97
98bool
100{
101 Purge();
102 /*
103 * Dequeue the entry with destination address dst
104 */
105 for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
106 {
107 if (i->GetDestination() == dst)
108 {
109 entry = *i;
110 i = m_sendBuffer.erase(i);
111 NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
112 return true;
113 }
114 }
115 return false;
116}
117
118bool
120{
121 /*
122 * Make sure if the send buffer contains entry with certain dst
123 */
124 for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
125 {
126 if (i->GetDestination() == dst)
127 {
128 NS_LOG_DEBUG("Found the packet");
129 return true;
130 }
131 }
132 return false;
133}
134
136{
137 /**
138 * comparison operator
139 * @param e entry to compare
140 * @return true if expired
141 */
142 bool operator()(const DsrSendBuffEntry& e) const
143 {
144 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
145 return (e.GetExpireTime().IsStrictlyNegative());
146 }
147};
148
149void
151{
152 /*
153 * Purge the buffer to eliminate expired entries
154 */
155 NS_LOG_INFO("The send buffer size " << m_sendBuffer.size());
156 IsExpired pred;
157 for (auto i = m_sendBuffer.begin(); i != m_sendBuffer.end(); ++i)
158 {
159 if (pred(*i))
160 {
161 NS_LOG_DEBUG("Dropping Queue Packets");
162 Drop(*i, "Drop out-dated packet ");
163 }
164 }
165 m_sendBuffer.erase(std::remove_if(m_sendBuffer.begin(), m_sendBuffer.end(), pred),
166 m_sendBuffer.end());
167}
168
169void
171{
172 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
173 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
174 // Socket::ERROR_NOROUTETOHOST);
175}
176} // namespace dsr
177} // namespace ns3
Ipv4 addresses are stored in host order in this class.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
uint64_t GetUid() const
Returns the packet's Uid.
Definition packet.cc:401
bool IsStrictlyNegative() const
Exactly equivalent to t < 0.
Definition nstime.h:331
DSR Send Buffer Entry.
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.
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:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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:264
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const DsrSendBuffEntry &e) const
comparison operator