blob: 95b81dd4353a920f3e1c0edae373b4b292920a8a [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"
24#include "ns3/ndn-name-components.h"
25#include "ns3/ndn-interest-header.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));
65
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080066 Time newExpireTime = Simulator::Now () + offsetTime;
67 if (newExpireTime > m_expireTime)
68 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070069
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070070 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080071}
72
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070073const NameComponents &
74Entry::GetPrefix () const
75{
76 return m_interest->GetName ();
77}
78
79const Time &
80Entry::GetExpireTime () const
81{
82 return m_expireTime;
83}
84
85bool
86Entry::IsNonceSeen (uint32_t nonce) const
87{
88 return m_seenNonces.find (nonce) != m_seenNonces.end ();
89}
90
91void
92Entry::AddSeenNonce (uint32_t nonce)
93{
94 m_seenNonces.insert (nonce);
95}
96
97
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098Entry::in_iterator
99Entry::AddIncoming (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800100{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700101 std::pair<in_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102 m_incoming.insert (IncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800103
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700104 // NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800105
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800106 return ret.first;
107}
108
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800109void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110Entry::RemoveIncoming (Ptr<Face> face)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800111{
112 m_incoming.erase (face);
113}
114
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700115void
116Entry::ClearIncoming ()
117{
118 m_incoming.clear ();
119}
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800120
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121Entry::out_iterator
122Entry::AddOutgoing (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800123{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700124 std::pair<out_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125 m_outgoing.insert (OutgoingFace (face));
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800126
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800127 if (!ret.second)
128 { // outgoing face already exists
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700129 const_cast<OutgoingFace&>(*ret.first).UpdateOnRetransmit ();
130 // m_outgoing.modify (ret.first,
131 // ll::bind (&OutgoingFace::UpdateOnRetransmit, ll::_1));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800132 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800133
134 return ret.first;
135}
136
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700138Entry::ClearOutgoing ()
139{
140 m_outgoing.clear ();
141}
142
143void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700144Entry::RemoveAllReferencesToFace (Ptr<Face> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700146 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700147
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148 if (incoming != m_incoming.end ())
149 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700150
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700151 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800152 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700153
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800154 if (outgoing != m_outgoing.end ())
155 m_outgoing.erase (outgoing);
156}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700157
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700158// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159// Entry::SetWaitingInVain (Entry::out_iterator face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700160// {
161// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800162
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700163// m_outgoing.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164// (&ll::_1)->*&EntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700165// }
166
167void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700168Entry::SetWaitingInVain (Ptr<Face> face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700169{
170 // NS_LOG_DEBUG (boost::cref (*face->m_face));
171
172 out_iterator item = m_outgoing.find (face);
173 if (item == m_outgoing.end ())
174 return;
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700175
176 const_cast<OutgoingFace&>(*item).m_waitingInVain = true;
177 // m_outgoing.modify (item,
178 // (&ll::_1)->*&OutgoingFace::m_waitingInVain = true);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800179}
180
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700181
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800182bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700183Entry::AreAllOutgoingInVain () const
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800184{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800185 NS_LOG_DEBUG (m_outgoing.size ());
186
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800187 bool inVain = true;
188 std::for_each (m_outgoing.begin (), m_outgoing.end (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700189 ll::var(inVain) &= (&ll::_1)->*&OutgoingFace::m_waitingInVain);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800190
191 NS_LOG_DEBUG ("inVain " << inVain);
192 return inVain;
193}
194
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800195bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700196Entry::AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800197{
198 bool inVain = true;
199 std::for_each (m_outgoing.begin (), m_outgoing.end (),
200 ll::var(inVain) &=
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201 ((&ll::_1)->*&OutgoingFace::m_face == face ||
202 (&ll::_1)->*&OutgoingFace::m_waitingInVain));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800203
204 return !inVain;
205}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800206
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800207void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700208Entry::IncreaseAllowedRetxCount ()
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800209{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800210 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700211 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700212 {
213 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700214 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700215 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700216 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700217 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800218}
219
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700220Ptr<fib::Entry>
221Entry::GetFibEntry ()
222{
223 return m_fibEntry;
224};
225
226const Entry::in_container &
227Entry::GetIncoming () const
228{
229 return m_incoming;
230}
231
232const Entry::out_container &
233Entry::GetOutgoing () const
234{
235 return m_outgoing;
236}
237
238uint32_t
239Entry::GetMaxRetxCount () const
240{
241 return m_maxRetxCount;
242}
243
244
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700245std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800246{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700247 os << "Prefix: " << entry.GetPrefix () << "\n";
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800248 os << "In: ";
249 bool first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700250 BOOST_FOREACH (const IncomingFace &face, entry.m_incoming)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800251 {
252 if (!first)
253 os << ",";
254 else
255 first = false;
256
257 os << *face.m_face;
258 }
259
260 os << "\nOut: ";
261 first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700262 BOOST_FOREACH (const OutgoingFace &face, entry.m_outgoing)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800263 {
264 if (!first)
265 os << ",";
266 else
267 first = false;
268
269 os << *face.m_face;
270 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700271 os << "\nNonces: ";
272 first = true;
273 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
274 {
275 if (!first)
276 os << ",";
277 else
278 first = false;
279
280 os << nonce;
281 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800282
283 return os;
284}
285
286
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700287} // namespace pit
288} // namespace ndn
289} // namespace ns3