author | Tom Henderson <tomh@tomh.org> |
Sun, 13 May 2012 10:47:11 -0700 | |
changeset 8756 | 9a34e618f40b |
parent 8753 | 8d84b96e26bf |
child 9719 | 4031f7fdce5c |
permissions | -rw-r--r-- |
8751 | 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 |
NS_LOG_COMPONENT_DEFINE ("DsrSendBuffer"); |
|
40 |
||
41 |
namespace ns3 { |
|
42 |
namespace dsr { |
|
43 |
||
44 |
uint32_t |
|
45 |
SendBuffer::GetSize () |
|
46 |
{ |
|
47 |
Purge (); |
|
48 |
return m_sendBuffer.size (); |
|
49 |
} |
|
50 |
||
51 |
bool |
|
52 |
SendBuffer::Enqueue (SendBuffEntry & entry) |
|
53 |
{ |
|
54 |
Purge (); |
|
55 |
for (std::vector<SendBuffEntry>::const_iterator i = m_sendBuffer.begin (); i |
|
56 |
!= m_sendBuffer.end (); ++i) |
|
57 |
{ |
|
58 |
NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () |
|
8756
9a34e618f40b
check-style.py run on src/dsr
Tom Henderson <tomh@tomh.org>
parents:
8753
diff
changeset
|
59 |
<< " dst " << i->GetDestination () << " " << entry.GetDestination ()); |
8751 | 60 |
|
61 |
if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) |
|
62 |
&& (i->GetDestination () == entry.GetDestination ())) |
|
63 |
{ |
|
64 |
return false; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
entry.SetExpireTime (m_sendBufferTimeout); // Initialize the send buffer timeout |
|
69 |
/* |
|
70 |
* Drop the most aged packet when buffer reaches to max |
|
71 |
*/ |
|
72 |
if (m_sendBuffer.size () >= m_maxLen) |
|
73 |
{ |
|
74 |
Drop (m_sendBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet |
|
75 |
m_sendBuffer.erase (m_sendBuffer.begin ()); |
|
76 |
} |
|
77 |
// enqueue the entry |
|
78 |
m_sendBuffer.push_back (entry); |
|
79 |
return true; |
|
80 |
} |
|
81 |
||
82 |
void |
|
83 |
SendBuffer::DropPacketWithDst (Ipv4Address dst) |
|
84 |
{ |
|
85 |
NS_LOG_FUNCTION (this << dst); |
|
86 |
Purge (); |
|
87 |
/* |
|
88 |
* Drop the packet with destination address dst |
|
89 |
*/ |
|
90 |
for (std::vector<SendBuffEntry>::iterator i = m_sendBuffer.begin (); i |
|
91 |
!= m_sendBuffer.end (); ++i) |
|
92 |
{ |
|
93 |
if (IsEqual (*i, dst)) |
|
94 |
{ |
|
95 |
Drop (*i, "DropPacketWithDst"); |
|
96 |
} |
|
97 |
} |
|
98 |
m_sendBuffer.erase (std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (), |
|
99 |
std::bind2nd (std::ptr_fun (SendBuffer::IsEqual), dst)), m_sendBuffer.end ()); |
|
100 |
} |
|
101 |
||
102 |
bool |
|
103 |
SendBuffer::Dequeue (Ipv4Address dst, SendBuffEntry & entry) |
|
104 |
{ |
|
105 |
Purge (); |
|
106 |
/* |
|
107 |
* Dequeue the entry with destination address dst |
|
108 |
*/ |
|
109 |
for (std::vector<SendBuffEntry>::iterator i = m_sendBuffer.begin (); i != m_sendBuffer.end (); ++i) |
|
110 |
{ |
|
111 |
if (i->GetDestination () == dst) |
|
112 |
{ |
|
113 |
entry = *i; |
|
114 |
m_sendBuffer.erase (i); |
|
115 |
NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ()); |
|
116 |
return true; |
|
117 |
} |
|
118 |
} |
|
119 |
return false; |
|
120 |
} |
|
121 |
||
122 |
bool |
|
123 |
SendBuffer::Find (Ipv4Address dst) |
|
124 |
{ |
|
125 |
/* |
|
126 |
* Make sure if the send buffer contains entry with certain dst |
|
127 |
*/ |
|
128 |
for (std::vector<SendBuffEntry>::const_iterator i = m_sendBuffer.begin (); i |
|
129 |
!= m_sendBuffer.end (); ++i) |
|
130 |
{ |
|
131 |
if (i->GetDestination () == dst) |
|
132 |
{ |
|
133 |
NS_LOG_DEBUG ("Found the packet"); |
|
134 |
return true; |
|
135 |
} |
|
136 |
} |
|
137 |
return false; |
|
138 |
} |
|
139 |
||
140 |
struct IsExpired |
|
141 |
{ |
|
142 |
bool |
|
143 |
operator() (SendBuffEntry const & e) const |
|
144 |
{ |
|
145 |
// NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ()); |
|
146 |
return (e.GetExpireTime () < Seconds (0)); |
|
147 |
} |
|
148 |
}; |
|
149 |
||
150 |
void |
|
151 |
SendBuffer::Purge () |
|
152 |
{ |
|
153 |
/* |
|
154 |
* Purge the buffer to eliminate expired entries |
|
155 |
*/ |
|
156 |
NS_LOG_DEBUG ("The send buffer size " << m_sendBuffer.size ()); |
|
157 |
IsExpired pred; |
|
158 |
for (std::vector<SendBuffEntry>::iterator i = m_sendBuffer.begin (); i |
|
159 |
!= m_sendBuffer.end (); ++i) |
|
160 |
{ |
|
161 |
if (pred (*i)) |
|
162 |
{ |
|
163 |
NS_LOG_DEBUG ("Dropping Queue Packets"); |
|
164 |
Drop (*i, "Drop out-dated packet "); |
|
165 |
} |
|
166 |
} |
|
167 |
m_sendBuffer.erase (std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (), pred), |
|
168 |
m_sendBuffer.end ()); |
|
169 |
} |
|
170 |
||
171 |
void |
|
172 |
SendBuffer::Drop (SendBuffEntry en, std::string reason) |
|
173 |
{ |
|
174 |
NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ()); |
|
175 |
// en.GetErrorCallback () (en.GetPacket (), en.GetDestination (), |
|
176 |
// Socket::ERROR_NOROUTETOHOST); |
|
177 |
return; |
|
178 |
} |
|
179 |
} // namespace dsr |
|
180 |
} // namespace ns3 |