blob: 6d16048ea370ff43bc199a16f78c201352e4e487 [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 Afanasyev4aac5572012-08-09 10:49:55 -070023#include "ns3/ndn-fib.h"
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070024#include "ns3/ndn-name.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080025#include "ns3/ndn-interest.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070026
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070027#include "ns3/simulator.h"
Alexander Afanasyev5a595072011-11-25 14:49:07 -080028#include "ns3/log.h"
29
30#include <boost/lambda/lambda.hpp>
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080031#include <boost/lambda/bind.hpp>
Alexander Afanasyev78057c32012-07-06 15:18:46 -070032#include <boost/foreach.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080033namespace ll = boost::lambda;
34
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070035NS_LOG_COMPONENT_DEFINE ("ndn.pit.Entry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070036
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037namespace ns3 {
38namespace ndn {
39namespace pit {
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070040
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041Entry::Entry (Pit &container,
42 Ptr<const InterestHeader> header,
43 Ptr<fib::Entry> fibEntry)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070044 : m_container (container)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070045 , m_interest (header)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070046 , m_fibEntry (fibEntry)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070047 , m_expireTime (Simulator::Now () + (!header->GetInterestLifetime ().IsZero ()?
48 header->GetInterestLifetime ():
49 Seconds (1.0)))
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080050 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070051{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070052 NS_LOG_FUNCTION (GetPrefix () << m_expireTime);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070053 // note that if interest lifetime is not set, the behavior is undefined
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070054}
55
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070056Entry::~Entry ()
57{
58 NS_LOG_FUNCTION (GetPrefix ());
59}
60
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080061void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070062Entry::UpdateLifetime (const Time &offsetTime)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080063{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070064 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
Alexander Afanasyeveec66292013-02-06 16:23:21 -080065
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080066 Time newExpireTime = Simulator::Now () + offsetTime;
67 if (newExpireTime > m_expireTime)
68 m_expireTime = newExpireTime;
Alexander Afanasyeveec66292013-02-06 16:23:21 -080069
70 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 -080071}
72
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070073void
74Entry::OffsetLifetime (const Time &offsetTime)
75{
76 m_expireTime += offsetTime;
77 if (m_expireTime < Simulator::Now ())
78 {
79 m_expireTime = Simulator::Now ();
80 }
Alexander Afanasyeveec66292013-02-06 16:23:21 -080081 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 -070082}
83
84
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070085const Name &
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070086Entry::GetPrefix () const
87{
88 return m_interest->GetName ();
89}
90
91const Time &
92Entry::GetExpireTime () const
93{
94 return m_expireTime;
95}
96
97bool
98Entry::IsNonceSeen (uint32_t nonce) const
99{
100 return m_seenNonces.find (nonce) != m_seenNonces.end ();
101}
102
103void
104Entry::AddSeenNonce (uint32_t nonce)
105{
106 m_seenNonces.insert (nonce);
107}
108
109
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110Entry::in_iterator
111Entry::AddIncoming (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800112{
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800113 std::pair<in_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700114 m_incoming.insert (IncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800115
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700116 // NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800117
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800118 return ret.first;
119}
120
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800121void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122Entry::RemoveIncoming (Ptr<Face> face)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800123{
124 m_incoming.erase (face);
125}
126
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700127void
128Entry::ClearIncoming ()
129{
130 m_incoming.clear ();
131}
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800132
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700133Entry::out_iterator
134Entry::AddOutgoing (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800135{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700136 std::pair<out_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137 m_outgoing.insert (OutgoingFace (face));
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800138
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800139 if (!ret.second)
140 { // outgoing face already exists
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700141 const_cast<OutgoingFace&>(*ret.first).UpdateOnRetransmit ();
142 // m_outgoing.modify (ret.first,
143 // ll::bind (&OutgoingFace::UpdateOnRetransmit, ll::_1));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800144 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800145
146 return ret.first;
147}
148
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800149void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700150Entry::ClearOutgoing ()
151{
152 m_outgoing.clear ();
153}
154
155void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700156Entry::RemoveAllReferencesToFace (Ptr<Face> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800157{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700158 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700159
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800160 if (incoming != m_incoming.end ())
161 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700162
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700163 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700165
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 if (outgoing != m_outgoing.end ())
167 m_outgoing.erase (outgoing);
168}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700169
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700170// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700171// Entry::SetWaitingInVain (Entry::out_iterator face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700172// {
173// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800174
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700175// m_outgoing.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700176// (&ll::_1)->*&EntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700177// }
178
179void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180Entry::SetWaitingInVain (Ptr<Face> face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700181{
182 // NS_LOG_DEBUG (boost::cref (*face->m_face));
183
184 out_iterator item = m_outgoing.find (face);
185 if (item == m_outgoing.end ())
186 return;
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700187
188 const_cast<OutgoingFace&>(*item).m_waitingInVain = true;
189 // m_outgoing.modify (item,
190 // (&ll::_1)->*&OutgoingFace::m_waitingInVain = true);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800191}
192
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700193
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800194bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700195Entry::AreAllOutgoingInVain () const
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800196{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800197 NS_LOG_DEBUG (m_outgoing.size ());
198
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800199 bool inVain = true;
200 std::for_each (m_outgoing.begin (), m_outgoing.end (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201 ll::var(inVain) &= (&ll::_1)->*&OutgoingFace::m_waitingInVain);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800202
203 NS_LOG_DEBUG ("inVain " << inVain);
204 return inVain;
205}
206
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800207bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700208Entry::AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800209{
210 bool inVain = true;
211 std::for_each (m_outgoing.begin (), m_outgoing.end (),
212 ll::var(inVain) &=
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213 ((&ll::_1)->*&OutgoingFace::m_face == face ||
214 (&ll::_1)->*&OutgoingFace::m_waitingInVain));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800215
216 return !inVain;
217}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800218
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800219void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700220Entry::IncreaseAllowedRetxCount ()
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800221{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800222 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700223 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700224 {
225 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700226 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700227 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700228 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700229 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800230}
231
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700232Ptr<fib::Entry>
233Entry::GetFibEntry ()
234{
235 return m_fibEntry;
236};
237
238const Entry::in_container &
239Entry::GetIncoming () const
240{
241 return m_incoming;
242}
243
244const Entry::out_container &
245Entry::GetOutgoing () const
246{
247 return m_outgoing;
248}
249
250uint32_t
Alexander Afanasyevff033952012-08-23 15:45:52 -0700251Entry::GetOutgoingCount () const
252{
253 return m_outgoing.size ();
254}
255
256
257uint32_t
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700258Entry::GetMaxRetxCount () const
259{
260 return m_maxRetxCount;
261}
262
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700263Ptr<const InterestHeader>
264Entry::GetInterest () const
265{
266 return m_interest;
267}
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700268
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700269std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800270{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700271 os << "Prefix: " << entry.GetPrefix () << "\n";
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800272 os << "In: ";
273 bool first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700274 BOOST_FOREACH (const IncomingFace &face, entry.m_incoming)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800275 {
276 if (!first)
277 os << ",";
278 else
279 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800280
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800281 os << *face.m_face;
282 }
283
284 os << "\nOut: ";
285 first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700286 BOOST_FOREACH (const OutgoingFace &face, entry.m_outgoing)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800287 {
288 if (!first)
289 os << ",";
290 else
291 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800292
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800293 os << *face.m_face;
294 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700295 os << "\nNonces: ";
296 first = true;
297 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
298 {
299 if (!first)
300 os << ",";
301 else
302 first = false;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800303
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700304 os << nonce;
305 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800306
307 return os;
308}
309
310
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700311} // namespace pit
312} // namespace ndn
313} // namespace ns3