A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dsr-errorbuff.cc
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-errorbuff.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 ("DsrErrorBuffer");
40 
41 namespace ns3 {
42 namespace dsr {
43 
44 uint32_t
45 ErrorBuffer::GetSize ()
46 {
47  Purge ();
48  return m_errorBuffer.size ();
49 }
50 
51 bool
53 {
54  Purge ();
55  for (std::vector<ErrorBuffEntry>::const_iterator i = m_errorBuffer.begin (); i
56  != m_errorBuffer.end (); ++i)
57  {
58  NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource ()
59  << " next hop " << i->GetNextHop () << " " << entry.GetNextHop () << " dst " << i->GetDestination () << " " << entry.GetDestination ());
60 
62  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetSource ())
63  && (i->GetDestination () == entry.GetDestination ()))
64  {
65  return false;
66  }
67  }
68 
69  entry.SetExpireTime (m_errorBufferTimeout); // Initialize the send buffer timeout
70  /*
71  * Drop the most aged packet when buffer reaches to max
72  */
73  if (m_errorBuffer.size () >= m_maxLen)
74  {
75  Drop (m_errorBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet
76  m_errorBuffer.erase (m_errorBuffer.begin ());
77  }
78  // enqueue the entry
79  m_errorBuffer.push_back (entry);
80  return true;
81 }
82 
83 void
84 ErrorBuffer::DropPacketForErrLink (Ipv4Address source, Ipv4Address nextHop)
85 {
86  NS_LOG_FUNCTION (this << source << nextHop);
87  Purge ();
88  std::vector<Ipv4Address> list;
89  list.push_back (source);
90  list.push_back (nextHop);
91  const std::vector<Ipv4Address> link = list;
92  /*
93  * Drop the packet with the error link source----------nextHop
94  */
95  for (std::vector<ErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i
96  != m_errorBuffer.end (); ++i)
97  {
98  if (LinkEqual (*i, link))
99  {
100  DropLink (*i, "DropPacketForErrLink");
101  }
102  }
103  m_errorBuffer.erase (std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (),
104  std::bind2nd (std::ptr_fun (ErrorBuffer::LinkEqual), link)), m_errorBuffer.end ());
105 }
106 
107 bool
108 ErrorBuffer::Dequeue (Ipv4Address dst, ErrorBuffEntry & entry)
109 {
110  Purge ();
111  /*
112  * Dequeue the entry with destination address dst
113  */
114  for (std::vector<ErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i != m_errorBuffer.end (); ++i)
115  {
116  if (i->GetDestination () == dst)
117  {
118  entry = *i;
119  m_errorBuffer.erase (i);
120  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
121  return true;
122  }
123  }
124  return false;
125 }
126 
127 bool
128 ErrorBuffer::Find (Ipv4Address dst)
129 {
130  /*
131  * Make sure if the send buffer contains entry with certain dst
132  */
133  for (std::vector<ErrorBuffEntry>::const_iterator i = m_errorBuffer.begin (); i
134  != m_errorBuffer.end (); ++i)
135  {
136  if (i->GetDestination () == dst)
137  {
138  NS_LOG_DEBUG ("Found the packet");
139  return true;
140  }
141  }
142  return false;
143 }
144 
145 struct IsExpired
146 {
147  bool
148  operator() (ErrorBuffEntry const & e) const
149  {
150  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
151  return (e.GetExpireTime () < Seconds (0));
152  }
153 };
154 
155 void
156 ErrorBuffer::Purge ()
157 {
158  /*
159  * Purge the buffer to eliminate expired entries
160  */
161  NS_LOG_DEBUG ("The error buffer size " << m_errorBuffer.size ());
162  IsExpired pred;
163  for (std::vector<ErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i
164  != m_errorBuffer.end (); ++i)
165  {
166  if (pred (*i))
167  {
168  NS_LOG_DEBUG ("Dropping Queue Packets");
169  Drop (*i, "Drop out-dated packet ");
170  }
171  }
172  m_errorBuffer.erase (std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (), pred),
173  m_errorBuffer.end ());
174 }
175 
176 void
177 ErrorBuffer::Drop (ErrorBuffEntry en, std::string reason)
178 {
179  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ());
180 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
181 // Socket::ERROR_NOROUTETOHOST);
182  return;
183 }
184 
185 void
186 ErrorBuffer::DropLink (ErrorBuffEntry en, std::string reason)
187 {
188  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ());
189 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
190 // Socket::ERROR_NOROUTETOHOST);
191  return;
192 }
193 } // namespace dsr
194 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
uint64_t GetUid(void) const
Definition: packet.cc:412
bool Enqueue(ErrorBuffEntry &entry)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_INFO(msg)
Definition: log.h:264
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
DSR Error Buffer Entry.
Definition: dsr-errorbuff.h:45
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
#define NS_LOG_DEBUG(msg)
Definition: log.h:255