blob: b578c0e4e65f6127941e64efe5409a56acd7d110 [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)
45 , m_prefix (header->GetNamePtr ())
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 Afanasyev30f60e32012-07-10 14:21:16 -070052 // note that if interest lifetime is not set, the behavior is undefined
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070053}
54
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080055void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056Entry::UpdateLifetime (const Time &offsetTime)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080057{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070058 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
59
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080060 Time newExpireTime = Simulator::Now () + offsetTime;
61 if (newExpireTime > m_expireTime)
62 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070063
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070064 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080065}
66
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067Entry::in_iterator
68Entry::AddIncoming (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080069{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070070 std::pair<in_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070071 m_incoming.insert (IncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080072
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070073 // NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080074
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080075 return ret.first;
76}
77
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080078void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079Entry::RemoveIncoming (Ptr<Face> face)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080080{
81 m_incoming.erase (face);
82}
83
84
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085Entry::out_iterator
86Entry::AddOutgoing (Ptr<Face> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080087{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070088 std::pair<out_iterator,bool> ret =
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 m_outgoing.insert (OutgoingFace (face));
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080090
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080091 if (!ret.second)
92 { // outgoing face already exists
93 m_outgoing.modify (ret.first,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094 ll::bind (&OutgoingFace::UpdateOnRetransmit, ll::_1));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080095 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080096
97 return ret.first;
98}
99
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800100void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101Entry::RemoveAllReferencesToFace (Ptr<Face> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700103 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 if (incoming != m_incoming.end ())
106 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700107
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700108 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700110
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111 if (outgoing != m_outgoing.end ())
112 m_outgoing.erase (outgoing);
113}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700114
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700115// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116// Entry::SetWaitingInVain (Entry::out_iterator face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700117// {
118// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800119
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700120// m_outgoing.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121// (&ll::_1)->*&EntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700122// }
123
124void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125Entry::SetWaitingInVain (Ptr<Face> face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700126{
127 // NS_LOG_DEBUG (boost::cref (*face->m_face));
128
129 out_iterator item = m_outgoing.find (face);
130 if (item == m_outgoing.end ())
131 return;
132
133 m_outgoing.modify (item,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134 (&ll::_1)->*&OutgoingFace::m_waitingInVain = true);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800135}
136
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700137
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800138bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700139Entry::AreAllOutgoingInVain () const
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800140{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800141 NS_LOG_DEBUG (m_outgoing.size ());
142
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800143 bool inVain = true;
144 std::for_each (m_outgoing.begin (), m_outgoing.end (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700145 ll::var(inVain) &= (&ll::_1)->*&OutgoingFace::m_waitingInVain);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800146
147 NS_LOG_DEBUG ("inVain " << inVain);
148 return inVain;
149}
150
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800151bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152Entry::AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800153{
154 bool inVain = true;
155 std::for_each (m_outgoing.begin (), m_outgoing.end (),
156 ll::var(inVain) &=
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 ((&ll::_1)->*&OutgoingFace::m_face == face ||
158 (&ll::_1)->*&OutgoingFace::m_waitingInVain));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800159
160 return !inVain;
161}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800162
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800163void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164Entry::IncreaseAllowedRetxCount ()
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800165{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800166 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700167 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700168 {
169 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700170 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700171 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700172 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700173 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800174}
175
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700176std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800177{
178 os << "Prefix: " << *entry.m_prefix << "\n";
179 os << "In: ";
180 bool first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181 BOOST_FOREACH (const IncomingFace &face, entry.m_incoming)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800182 {
183 if (!first)
184 os << ",";
185 else
186 first = false;
187
188 os << *face.m_face;
189 }
190
191 os << "\nOut: ";
192 first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193 BOOST_FOREACH (const OutgoingFace &face, entry.m_outgoing)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800194 {
195 if (!first)
196 os << ",";
197 else
198 first = false;
199
200 os << *face.m_face;
201 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700202 os << "\nNonces: ";
203 first = true;
204 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
205 {
206 if (!first)
207 os << ",";
208 else
209 first = false;
210
211 os << nonce;
212 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800213
214 return os;
215}
216
217
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700218} // namespace pit
219} // namespace ndn
220} // namespace ns3