A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsr-maintain-buff.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-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
46 {
47  Purge ();
48  return m_maintainBuffer.size ();
49 }
50 
51 bool
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 
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  NS_LOG_DEBUG ("The maintain size " << m_maintainBuffer.size ());
79  return true;
80 }
81 
82 void
84 {
85  NS_LOG_FUNCTION (this << nextHop);
86  Purge ();
87  NS_LOG_DEBUG ("Drop Packet With next hop " << nextHop);
88  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (),
89  std::bind2nd (std::ptr_fun (MaintainBuffer::IsEqual), nextHop)), m_maintainBuffer.end ());
90 }
91 
92 bool
94 {
95  Purge ();
96  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i != m_maintainBuffer.end (); ++i)
97  {
98  if (i->GetNextHop () == nextHop)
99  {
100  entry = *i;
101  m_maintainBuffer.erase (i);
102  NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ());
103  return true;
104  }
105  }
106  return false;
107 }
108 
109 //bool
110 //MaintainBuffer::FindMaintainEntry (Ptr<Packet> packet, Ipv4Address ourAdd, Ipv4Address src, Ipv4Address nextHop, Ipv4Address dst, NetworkKey networkKey)
111 //{
112 // // TODO if necessary this one can strip the packet header and have more information
113 // for (std::vector<MaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
114 // != m_maintainBuffer.end (); ++i)
115 // {
116 // NS_LOG_INFO ("packet " << i->GetPacket () << " " << packet << " nexthop " << i->GetNextHop () << " " << nextHop
117 // << " our add " << i->GetOurAdd () << " " << ourAdd << " src " << i->GetSrc ()
118 // << " " << src << " dst " << i->GetDst () << " " << dst
119 // );
120 //
121 // if ((i->GetPacket () == packet) && (i->GetNextHop () == nextHop) && (i->GetOurAdd () == ourAdd) && (i->GetSrc () == src)
122 // && (i->GetDst () == dst))
123 // {
124 // NS_LOG_DEBUG ("Same maintenance entry found");
125 // networkKey.m_ackId = newEntry.GetAckId ();
126 // networkKey.m_ourAdd = newEntry.GetOurAdd ();
127 // networkKey.m_nextHop = newEntry.GetNextHop ();
128 // networkKey.m_source = newEntry.GetSrc ();
129 // networkKey.m_destination = newEntry.GetDst ();
130 // // TODO may need a different network key to have
131 // return true;
132 // }
133 // }
134 // return false;
135 //}
136 
137 bool
139 {
140  for (std::vector<MaintainBuffEntry>::const_iterator i = m_maintainBuffer.begin (); i
141  != m_maintainBuffer.end (); ++i)
142  {
143  if (i->GetNextHop () == nextHop)
144  {
145  NS_LOG_DEBUG ("Found the packet in maintenance buffer");
146  return true;
147  }
148  }
149  return false;
150 }
151 
152 bool
154 {
155  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
156  != m_maintainBuffer.end (); ++i)
157  {
158 
159  NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
160  << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
161  << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
162 
163  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
164  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
165  && (i->GetAckId () == entry.GetAckId ()) && (i->GetSegsLeft () == entry.GetSegsLeft ()))
166  {
167  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
168  return true;
169  }
170  }
171  return false;
172 }
173 
174 bool
176 {
177  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
178  != m_maintainBuffer.end (); ++i)
179  {
180 
181  NS_LOG_DEBUG ("nexthop " << i->GetNextHop () << " " << entry.GetNextHop () << " our address " << i->GetOurAdd () << " " << entry.GetOurAdd ()
182  << " src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
183  << " ackId " << i->GetAckId () << " " << entry.GetAckId ());
184 
185  if ((i->GetOurAdd () == entry.GetOurAdd ()) && (i->GetNextHop () == entry.GetNextHop ())
186  && (i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
187  && (i->GetAckId () == entry.GetAckId ()))
188  {
189  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the received packet
190  return true;
191  }
192  }
193  return false;
194 }
195 
196 bool
198 {
199  NS_LOG_DEBUG ("The maintenance buffer size " << m_maintainBuffer.size ());
200  for (std::vector<MaintainBuffEntry>::iterator i = m_maintainBuffer.begin (); i
201  != m_maintainBuffer.end (); ++i)
202  {
203  NS_LOG_DEBUG ("src " << i->GetSrc () << " " << entry.GetSrc () << " dst " << i->GetDst () << " " << entry.GetDst ()
204  << " SegsLeft " << (uint32_t)i->GetSegsLeft () << " " << (uint32_t)entry.GetSegsLeft () << " ackId " << (uint32_t)i->GetAckId () << " "
205  << (uint32_t)entry.GetAckId ()
206  );
207 
208  if ((i->GetSrc () == entry.GetSrc ()) && (i->GetDst () == entry.GetDst ())
209  && (i->GetSegsLeft () == entry.GetSegsLeft ()) && (i->GetAckId () == entry.GetAckId ())
210  )
211  {
212  m_maintainBuffer.erase (i); // Erase the same maintain buffer entry for the promisc received packet
213  return true;
214  }
215  }
216  return false;
217 }
218 
219 struct IsExpired
220 {
221  bool
223  {
224  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
225  return (e.GetExpireTime () < Seconds (0));
226  }
227 };
228 
229 void
231 {
232  NS_LOG_DEBUG ("Purging Maintenance Buffer");
233  IsExpired pred;
234  m_maintainBuffer.erase (std::remove_if (m_maintainBuffer.begin (), m_maintainBuffer.end (), pred),
235  m_maintainBuffer.end ());
236 }
237 
238 } // namespace dsr
239 } // namespace ns3