blob: 384750390cba7412ed40f12c04d7aae5f054f89a [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-pit-entry.h"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070022
Alexander Afanasyevcdeefb42013-05-01 16:04:53 -070023#include "ns3/ndn-pit.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070024#include "ns3/ndn-fib.h"
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070025#include "ns3/ndn-name.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080026#include "ns3/ndn-interest.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070027
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070028#include "ns3/simulator.h"
Alexander Afanasyev5a595072011-11-25 14:49:07 -080029#include "ns3/log.h"
30
31#include <boost/lambda/lambda.hpp>
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080032#include <boost/lambda/bind.hpp>
Alexander Afanasyev78057c32012-07-06 15:18:46 -070033#include <boost/foreach.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080034namespace ll = boost::lambda;
35
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036NS_LOG_COMPONENT_DEFINE ("ndn.pit.Entry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070037
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038namespace ns3 {
39namespace ndn {
40namespace pit {
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070041
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042Entry::Entry (Pit &container,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070043 Ptr<const Interest> header,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044 Ptr<fib::Entry> fibEntry)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070045 : m_container (container)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070046 , m_interest (header)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070047 , m_fibEntry (fibEntry)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080048 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070049{
Alexander Afanasyevcdeefb42013-05-01 16:04:53 -070050 NS_LOG_FUNCTION (this);
51
52 // UpdateLifetime is (and should) be called from the forwarding strategy
53
54 // UpdateLifetime ((!header->GetInterestLifetime ().IsZero ()?
55 // header->GetInterestLifetime ():
56 // Seconds (1.0)));
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070057}
58
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070059Entry::~Entry ()
60{
61 NS_LOG_FUNCTION (GetPrefix ());
62}
63
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080064void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065Entry::UpdateLifetime (const Time &offsetTime)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080066{
Alexander Afanasyevcdeefb42013-05-01 16:04:53 -070067 NS_LOG_FUNCTION (this);
Alexander Afanasyeveec66292013-02-06 16:23:21 -080068
Alexander Afanasyevcdeefb42013-05-01 16:04:53 -070069 Time newExpireTime = Simulator::Now () + (m_container.GetMaxPitEntryLifetime ().IsZero () ?
70 offsetTime :
71 std::min (m_container.GetMaxPitEntryLifetime (), offsetTime));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080072 if (newExpireTime > m_expireTime)
73 m_expireTime = newExpireTime;
Alexander Afanasyeveec66292013-02-06 16:23:21 -080074
75 NS_LOG_INFO (this->GetPrefix () << ", Updated lifetime to " << m_expireTime.ToDouble (Time::S) << "s, " << (m_expireTime-Simulator::Now ()).ToDouble (Time::S) << "s left");
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080076}
77
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070078void
79Entry::OffsetLifetime (const Time &offsetTime)
80{
81 m_expireTime += offsetTime;
82 if (m_expireTime < Simulator::Now ())
83 {
84 m_expireTime = Simulator::Now ();
85 }
Alexander Afanasyeveec66292013-02-06 16:23:21 -080086 NS_LOG_INFO (this->GetPrefix () << ", Offsetting lifetime to " << m_expireTime.ToDouble (Time::S) << "s, " << (m_expireTime-Simulator::Now ()).ToDouble (Time::S) << "s left");
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070087}
88
89
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070090const Name &
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070091Entry::GetPrefix () const
92{
93 return m_interest->GetName ();
94}
95
96const Time &
97Entry::GetExpireTime () const
98{
99 return m_expireTime;
100}
101
102bool
103Entry::IsNonceSeen (uint32_t nonce) const
104{
105 return m_seenNonces.find (nonce) != m_seenNonces.end ();
106}
107
108void
109Entry::AddSeenNonce (uint32_t nonce)
110{
111 m_seenNonces.insert (nonce);
112}
113
114
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115Entry::in_iterator
116Entry::AddIncoming (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800117{
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800118 std::pair<in_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119 m_incoming.insert (IncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800120
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700121 // NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800122
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800123 return ret.first;
124}
125
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800126void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127Entry::RemoveIncoming (Ptr<Face> face)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800128{
129 m_incoming.erase (face);
130}
131
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700132void
133Entry::ClearIncoming ()
134{
135 m_incoming.clear ();
136}
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800137
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138Entry::out_iterator
139Entry::AddOutgoing (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800140{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700141 std::pair<out_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700142 m_outgoing.insert (OutgoingFace (face));
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800143
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800144 if (!ret.second)
145 { // outgoing face already exists
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700146 const_cast<OutgoingFace&>(*ret.first).UpdateOnRetransmit ();
147 // m_outgoing.modify (ret.first,
148 // ll::bind (&OutgoingFace::UpdateOnRetransmit, ll::_1));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800149 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800150
151 return ret.first;
152}
153
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800154void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700155Entry::ClearOutgoing ()
156{
157 m_outgoing.clear ();
158}
159
160void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700161Entry::RemoveAllReferencesToFace (Ptr<Face> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700163 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700164
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800165 if (incoming != m_incoming.end ())
166 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700167
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700168 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800169 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700170
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800171 if (outgoing != m_outgoing.end ())
172 m_outgoing.erase (outgoing);
173}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700174
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700175// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700176// Entry::SetWaitingInVain (Entry::out_iterator face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700177// {
178// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800179
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700180// m_outgoing.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181// (&ll::_1)->*&EntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700182// }
183
184void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185Entry::SetWaitingInVain (Ptr<Face> face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700186{
187 // NS_LOG_DEBUG (boost::cref (*face->m_face));
188
189 out_iterator item = m_outgoing.find (face);
190 if (item == m_outgoing.end ())
191 return;
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700192
193 const_cast<OutgoingFace&>(*item).m_waitingInVain = true;
194 // m_outgoing.modify (item,
195 // (&ll::_1)->*&OutgoingFace::m_waitingInVain = true);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800196}
197
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700198
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800199bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700200Entry::AreAllOutgoingInVain () const
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800201{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800202 NS_LOG_DEBUG (m_outgoing.size ());
203
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800204 bool inVain = true;
205 std::for_each (m_outgoing.begin (), m_outgoing.end (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700206 ll::var(inVain) &= (&ll::_1)->*&OutgoingFace::m_waitingInVain);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800207
208 NS_LOG_DEBUG ("inVain " << inVain);
209 return inVain;
210}
211
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800212bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213Entry::AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800214{
215 bool inVain = true;
216 std::for_each (m_outgoing.begin (), m_outgoing.end (),
217 ll::var(inVain) &=
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700218 ((&ll::_1)->*&OutgoingFace::m_face == face ||
219 (&ll::_1)->*&OutgoingFace::m_waitingInVain));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800220
221 return !inVain;
222}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800223
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800224void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700225Entry::IncreaseAllowedRetxCount ()
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800226{
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700227 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700228 {
229 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700230 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700231 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700232 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700233 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800234}
235
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700236Ptr<fib::Entry>
237Entry::GetFibEntry ()
238{
239 return m_fibEntry;
240};
241
242const Entry::in_container &
243Entry::GetIncoming () const
244{
245 return m_incoming;
246}
247
248const Entry::out_container &
249Entry::GetOutgoing () const
250{
251 return m_outgoing;
252}
253
254uint32_t
Alexander Afanasyevff033952012-08-23 15:45:52 -0700255Entry::GetOutgoingCount () const
256{
257 return m_outgoing.size ();
258}
259
260
261uint32_t
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700262Entry::GetMaxRetxCount () const
263{
264 return m_maxRetxCount;
265}
266
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700267Ptr<const Interest>
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700268Entry::GetInterest () const
269{
270 return m_interest;
271}
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700272
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700273std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800274{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700275 os << "Prefix: " << entry.GetPrefix () << "\n";
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800276 os << "In: ";
277 bool first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700278 BOOST_FOREACH (const IncomingFace &face, entry.m_incoming)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800279 {
280 if (!first)
281 os << ",";
282 else
283 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800284
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800285 os << *face.m_face;
286 }
287
288 os << "\nOut: ";
289 first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700290 BOOST_FOREACH (const OutgoingFace &face, entry.m_outgoing)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800291 {
292 if (!first)
293 os << ",";
294 else
295 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800296
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800297 os << *face.m_face;
298 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700299 os << "\nNonces: ";
300 first = true;
301 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
302 {
303 if (!first)
304 os << ",";
305 else
306 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800307
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700308 os << nonce;
309 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800310
311 return os;
312}
313
314
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700315} // namespace pit
316} // namespace ndn
317} // namespace ns3