A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dsr-maintain-buff.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-maintain-buff.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 ("DsrMaintainBuffer");
40 
41 namespace ns3 {
42 namespace dsr {
43 
44 uint32_t
45 MaintainBuffer::GetSize ()
46 {
47  Purge ();
48  return m_maintainBuffer.size ();
49 }
50 
51 bool
52 MaintainBuffer::Enqueue (MaintainBuffEntry & entry)
53 {
54  Purge ();
55  for (std::vector<MaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
56  != m_maintainBuffer.end (); ++i)
57  {
58 // NS_LOG_INFO ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our add " << i->GetOurAdd () << " " << entry.GetOurAdd ()
59 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
60 // << " ackId " << i->GetAckId () << " " << entry.GetAckId () << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " << (uint32_t)entry.GetSegsLeft ()
61 // );
62 
63  if ((i->GetNextHop () == entry.GetNextHop ()) && (i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetSrc () == entry.GetSrc ())
64  && (i->GetDst () == entry.GetDst ()) && (i->GetAckId () == entry.GetAckId ()) && (i->GetSegsLeft () == entry.GetSegsLeft ()))
65  {
66  NS_LOG_DEBUG ("Same maintenance entry found");
67  return false;
68  }
69  }
70 
71  entry.SetExpireTime (m_maintainBufferTimeout);
72  if (m_maintainBuffer.size () >= m_maxLen)
73  {
74  NS_LOG_DEBUG ("Drop the most aged packet");
75  m_maintainBuffer.erase (m_maintainBuffer.begin ()); // Drop the most aged packet
76  }
77  m_maintainBuffer.push_back (entry);
78  return true;
79 }
80 
81 void
82 MaintainBuffer::DropPacketWithNextHop (Ipv4Address nextHop)
83 {
84  NS_LOG_FUNCTION (this << nextHop);
85  Purge ();
86  NS_LOG_INFO ("Drop Packet With next hop " << nextHop);
87  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (),
88  std::bind2nd (std::ptr_fun (MaintainBuffer::IsEqual), nextHop)), m_maintainBuffer.end ());
89 }
90 
91 bool
92 MaintainBuffer::Dequeue (Ipv4Address nextHop, MaintainBuffEntry & entry)
93 {
94  Purge ();
95  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i != m_maintainBuffer.end (); ++i)
96  {
97  if (i->GetNextHop () == nextHop)
98  {
99  entry = *i;
100  m_maintainBuffer.erase (i);
101  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
102  return true;
103  }
104  }
105  return false;
106 }
107 
108 bool
109 MaintainBuffer::Find (Ipv4Address nextHop)
110 {
111  for (std::vector<MaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
112  != m_maintainBuffer.end (); ++i)
113  {
114  if (i->GetNextHop () == nextHop)
115  {
116  NS_LOG_DEBUG ("Found the packet in maintenance buffer");
117  return true;
118  }
119  }
120  return false;
121 }
122 
123 bool
124 MaintainBuffer::AllEqual (MaintainBuffEntry & entry)
125 {
126  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
127  != m_maintainBuffer.end (); ++i)
128  {
129 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
130 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
131 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
132 
133  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
134  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
135  && (i->GetAckId () == entry.GetAckId ()) && (i->GetSegsLeft () == entry.GetSegsLeft ()))
136  {
137  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
138  return true;
139  }
140  }
141  return false;
142 }
143 
144 bool
145 MaintainBuffer::NetworkEqual (MaintainBuffEntry & entry)
146 {
147  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
148  != m_maintainBuffer.end (); ++i)
149  {
150 // NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
151 // << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
152 // << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
153 
154  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
155  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
156  && (i->GetAckId () == entry.GetAckId ()))
157  {
158  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
159  return true;
160  }
161  }
162  return false;
163 }
164 
165 bool
166 MaintainBuffer::PromiscEqual (MaintainBuffEntry & entry)
167 {
168  NS_LOG_DEBUG ("The maintenance buffer size " << m_maintainBuffer.size ());
169  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
170  != m_maintainBuffer.end (); ++i)
171  {
172 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
173 // << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " << (uint32_t)entry.GetSegsLeft () << " ackId " << (uint32_t)i->GetAckId () << " "
174 // << (uint32_t)entry.GetAckId ()
175 // );
176 
177  if ((i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
178  && (i->GetSegsLeft () == entry.GetSegsLeft ()) && (i->GetAckId () == entry.GetAckId ())
179  )
180  {
181  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the promisc received packet
182  return true;
183  }
184  }
185  return false;
186 }
187 
188 bool
189 MaintainBuffer::LinkEqual (MaintainBuffEntry & entry)
190 {
191  NS_LOG_DEBUG ("The maintenance buffer size " << m_maintainBuffer.size ());
192  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
193  != m_maintainBuffer.end (); ++i)
194  {
195 // NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
196 // << " OurAddress " << i->GetOurAdd () << " " << entry.GetOurAdd () << " next hop " << i->GetNextHop () << " "
197 // << entry.GetNextHop ()
198 // );
199 
200  if ((i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ()) && (i->GetOurAdd () == entry.GetOurAdd ())
201  && (i->GetNextHop () == entry.GetNextHop ())
202  )
203  {
204  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the promisc received packet
205  return true;
206  }
207  }
208  return false;
209 }
210 
211 struct IsExpired
212 {
213  bool
214  operator() (MaintainBuffEntry const & e) const
215  {
216  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
217  return (e.GetExpireTime () < Seconds (0));
218  }
219 };
220 
221 void
222 MaintainBuffer::Purge ()
223 {
224  NS_LOG_DEBUG ("Purging Maintenance Buffer");
225  IsExpired pred;
226  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (), pred),
227  m_maintainBuffer.end ());
228 }
229 
230 } // namespace dsr
231 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_INFO(msg)
Definition: log.h:264
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