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 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 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("DsrSendBuffer");
42 
43 namespace dsr {
44 
45 uint32_t
47 {
48  Purge ();
49  return m_sendBuffer.size ();
50 }
51 
52 bool
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 
83 void
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 (IsEqual (*i, dst))
95  {
96  Drop (*i, "DropPacketWithDst");
97  }
98  }
99  m_sendBuffer.erase (std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (),
100  std::bind2nd (std::ptr_fun (DsrSendBuffer::IsEqual), dst)), m_sendBuffer.end ());
101 }
102 
103 bool
105 {
106  Purge ();
107  /*
108  * Dequeue the entry with destination address dst
109  */
110  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin (); i != m_sendBuffer.end (); ++i)
111  {
112  if (i->GetDestination () == dst)
113  {
114  entry = *i;
115  i = m_sendBuffer.erase (i);
116  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
117  return true;
118  }
119  }
120  return false;
121 }
122 
123 bool
125 {
126  /*
127  * Make sure if the send buffer contains entry with certain dst
128  */
129  for (std::vector<DsrSendBuffEntry>::const_iterator i = m_sendBuffer.begin (); i
130  != m_sendBuffer.end (); ++i)
131  {
132  if (i->GetDestination () == dst)
133  {
134  NS_LOG_DEBUG ("Found the packet");
135  return true;
136  }
137  }
138  return false;
139 }
140 
141 struct IsExpired
142 {
143  bool
144  operator() (DsrSendBuffEntry const & e) const
145  {
146  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
147  return (e.GetExpireTime () < Seconds (0));
148  }
149 };
150 
151 void
153 {
154  /*
155  * Purge the buffer to eliminate expired entries
156  */
157  NS_LOG_INFO ("The send buffer size " << m_sendBuffer.size ());
158  IsExpired pred;
159  for (std::vector<DsrSendBuffEntry>::iterator i = m_sendBuffer.begin (); i
160  != m_sendBuffer.end (); ++i)
161  {
162  if (pred (*i))
163  {
164  NS_LOG_DEBUG ("Dropping Queue Packets");
165  Drop (*i, "Drop out-dated packet ");
166  }
167  }
168  m_sendBuffer.erase (std::remove_if (m_sendBuffer.begin (), m_sendBuffer.end (), pred),
169  m_sendBuffer.end ());
170 }
171 
172 void
173 DsrSendBuffer::Drop (DsrSendBuffEntry en, std::string reason)
174 {
175  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
176 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
177 // Socket::ERROR_NOROUTETOHOST);
178  return;
179 }
180 } // namespace dsr
181 } // namespace ns3
uint32_t GetSize()
Number of entries.
static bool IsEqual(DsrSendBuffEntry en, const Ipv4Address dst)
Check if the send buffer entry is the same or not.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
std::vector< DsrSendBuffEntry > m_sendBuffer
The send buffer to cache unsent packet.
Ptr< const Packet > GetPacket() const
Definition: dsr-rsendbuff.h:75
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:368
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool operator()(DsrErrorBuffEntry const &e) const
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
void Purge()
Remove all expired entries.
void SetExpireTime(Time exp)
Definition: dsr-rsendbuff.h:91
void DropPacketWithDst(Ipv4Address dst)
Remove all packets with destination IP address dst.
Time m_sendBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
void Drop(DsrSendBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
bool Find(Ipv4Address dst)
Check if a packet with destination dst exists in the queue.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ipv4Address GetDestination() const
Definition: dsr-rsendbuff.h:83
bool Enqueue(DsrSendBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
DSR Send Buffer Entry.
Definition: dsr-rsendbuff.h:45
Time GetExpireTime() const
Definition: dsr-rsendbuff.h:95
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
bool Dequeue(Ipv4Address dst, DsrSendBuffEntry &entry)
Return first found (the earliest) entry for the given destination.