blob: 1908dd0188dd28e3e979eb8f762c7ffb896d8c55 [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 Afanasyevfaa01f92013-07-10 18:34:31 -070028#include "ns3/packet.h"
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070029#include "ns3/simulator.h"
Alexander Afanasyev5a595072011-11-25 14:49:07 -080030#include "ns3/log.h"
31
32#include <boost/lambda/lambda.hpp>
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080033#include <boost/lambda/bind.hpp>
Alexander Afanasyev78057c32012-07-06 15:18:46 -070034#include <boost/foreach.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080035namespace ll = boost::lambda;
36
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037NS_LOG_COMPONENT_DEFINE ("ndn.pit.Entry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070038
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039namespace ns3 {
40namespace ndn {
41namespace pit {
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070042
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043Entry::Entry (Pit &container,
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070044 Ptr<const Interest> header,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045 Ptr<fib::Entry> fibEntry)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070046 : m_container (container)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070047 , m_interest (header)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070048 , m_fibEntry (fibEntry)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080049 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070050{
Alexander Afanasyevcdeefb42013-05-01 16:04:53 -070051 NS_LOG_FUNCTION (this);
52
53 // UpdateLifetime is (and should) be called from the forwarding strategy
54
Alexander Afanasyev2890e572013-05-02 12:33:38 -070055 UpdateLifetime ((!header->GetInterestLifetime ().IsZero ()?
56 header->GetInterestLifetime ():
57 Seconds (1.0)));
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070058}
59
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070060Entry::~Entry ()
61{
62 NS_LOG_FUNCTION (GetPrefix ());
63}
64
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080065void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070066Entry::UpdateLifetime (const Time &offsetTime)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080067{
Alexander Afanasyevcdeefb42013-05-01 16:04:53 -070068 NS_LOG_FUNCTION (this);
Alexander Afanasyeveec66292013-02-06 16:23:21 -080069
Alexander Afanasyevcdeefb42013-05-01 16:04:53 -070070 Time newExpireTime = Simulator::Now () + (m_container.GetMaxPitEntryLifetime ().IsZero () ?
71 offsetTime :
72 std::min (m_container.GetMaxPitEntryLifetime (), offsetTime));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080073 if (newExpireTime > m_expireTime)
74 m_expireTime = newExpireTime;
Alexander Afanasyeveec66292013-02-06 16:23:21 -080075
76 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 -080077}
78
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070079void
80Entry::OffsetLifetime (const Time &offsetTime)
81{
82 m_expireTime += offsetTime;
83 if (m_expireTime < Simulator::Now ())
84 {
85 m_expireTime = Simulator::Now ();
86 }
Alexander Afanasyeveec66292013-02-06 16:23:21 -080087 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 -070088}
89
90
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070091const Name &
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070092Entry::GetPrefix () const
93{
94 return m_interest->GetName ();
95}
96
97const Time &
98Entry::GetExpireTime () const
99{
100 return m_expireTime;
101}
102
103bool
104Entry::IsNonceSeen (uint32_t nonce) const
105{
106 return m_seenNonces.find (nonce) != m_seenNonces.end ();
107}
108
109void
110Entry::AddSeenNonce (uint32_t nonce)
111{
112 m_seenNonces.insert (nonce);
113}
114
115
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116Entry::in_iterator
117Entry::AddIncoming (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800118{
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800119 std::pair<in_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700120 m_incoming.insert (IncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800121
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700122 // NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800123
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800124 return ret.first;
125}
126
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800127void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700128Entry::RemoveIncoming (Ptr<Face> face)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800129{
130 m_incoming.erase (face);
131}
132
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700133void
134Entry::ClearIncoming ()
135{
136 m_incoming.clear ();
137}
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800138
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700139Entry::out_iterator
140Entry::AddOutgoing (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800141{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700142 std::pair<out_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700143 m_outgoing.insert (OutgoingFace (face));
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800144
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800145 if (!ret.second)
146 { // outgoing face already exists
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700147 const_cast<OutgoingFace&>(*ret.first).UpdateOnRetransmit ();
148 // m_outgoing.modify (ret.first,
149 // ll::bind (&OutgoingFace::UpdateOnRetransmit, ll::_1));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800150 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800151
152 return ret.first;
153}
154
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800155void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700156Entry::ClearOutgoing ()
157{
158 m_outgoing.clear ();
159}
160
161void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700162Entry::RemoveAllReferencesToFace (Ptr<Face> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800163{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700164 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700165
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 if (incoming != m_incoming.end ())
167 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700168
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700169 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800170 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700171
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800172 if (outgoing != m_outgoing.end ())
173 m_outgoing.erase (outgoing);
174}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700175
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700176// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700177// Entry::SetWaitingInVain (Entry::out_iterator face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700178// {
179// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800180
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700181// m_outgoing.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700182// (&ll::_1)->*&EntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700183// }
184
185void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700186Entry::SetWaitingInVain (Ptr<Face> face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700187{
188 // NS_LOG_DEBUG (boost::cref (*face->m_face));
189
190 out_iterator item = m_outgoing.find (face);
191 if (item == m_outgoing.end ())
192 return;
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700193
194 const_cast<OutgoingFace&>(*item).m_waitingInVain = true;
195 // m_outgoing.modify (item,
196 // (&ll::_1)->*&OutgoingFace::m_waitingInVain = true);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800197}
198
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700199
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800200bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201Entry::AreAllOutgoingInVain () const
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800202{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800203 NS_LOG_DEBUG (m_outgoing.size ());
204
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800205 bool inVain = true;
206 std::for_each (m_outgoing.begin (), m_outgoing.end (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207 ll::var(inVain) &= (&ll::_1)->*&OutgoingFace::m_waitingInVain);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800208
209 NS_LOG_DEBUG ("inVain " << inVain);
210 return inVain;
211}
212
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800213bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214Entry::AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800215{
216 bool inVain = true;
217 std::for_each (m_outgoing.begin (), m_outgoing.end (),
218 ll::var(inVain) &=
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700219 ((&ll::_1)->*&OutgoingFace::m_face == face ||
220 (&ll::_1)->*&OutgoingFace::m_waitingInVain));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800221
222 return !inVain;
223}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800224
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800225void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700226Entry::IncreaseAllowedRetxCount ()
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800227{
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700228 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700229 {
230 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700231 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700232 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700233 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700234 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800235}
236
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700237Ptr<fib::Entry>
238Entry::GetFibEntry ()
239{
240 return m_fibEntry;
241};
242
243const Entry::in_container &
244Entry::GetIncoming () const
245{
246 return m_incoming;
247}
248
249const Entry::out_container &
250Entry::GetOutgoing () const
251{
252 return m_outgoing;
253}
254
255uint32_t
Alexander Afanasyevff033952012-08-23 15:45:52 -0700256Entry::GetOutgoingCount () const
257{
258 return m_outgoing.size ();
259}
260
261
262uint32_t
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700263Entry::GetMaxRetxCount () const
264{
265 return m_maxRetxCount;
266}
267
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700268Ptr<const Interest>
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700269Entry::GetInterest () const
270{
271 return m_interest;
272}
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700273
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700274std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800275{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700276 os << "Prefix: " << entry.GetPrefix () << "\n";
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800277 os << "In: ";
278 bool first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700279 BOOST_FOREACH (const IncomingFace &face, entry.m_incoming)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800280 {
281 if (!first)
282 os << ",";
283 else
284 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800285
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800286 os << *face.m_face;
287 }
288
289 os << "\nOut: ";
290 first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700291 BOOST_FOREACH (const OutgoingFace &face, entry.m_outgoing)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800292 {
293 if (!first)
294 os << ",";
295 else
296 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800297
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800298 os << *face.m_face;
299 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700300 os << "\nNonces: ";
301 first = true;
302 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
303 {
304 if (!first)
305 os << ",";
306 else
307 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800308
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700309 os << nonce;
310 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800311
312 return os;
313}
314
315
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700316} // namespace pit
317} // namespace ndn
318} // namespace ns3