blob: 765693f31d296ec6c30f0c43f25224d5e35f5d66 [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
Alexander Afanasyevc202fd92012-09-03 21:46:00 -070093 const_cast<OutgoingFace&>(*ret.first).UpdateOnRetransmit ();
94 // m_outgoing.modify (ret.first,
95 // ll::bind (&OutgoingFace::UpdateOnRetransmit, ll::_1));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080096 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080097
98 return ret.first;
99}
100
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800101void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102Entry::RemoveAllReferencesToFace (Ptr<Face> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700104 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700105
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106 if (incoming != m_incoming.end ())
107 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700108
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700109 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700111
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112 if (outgoing != m_outgoing.end ())
113 m_outgoing.erase (outgoing);
114}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700115
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700116// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700117// Entry::SetWaitingInVain (Entry::out_iterator face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700118// {
119// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800120
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700121// m_outgoing.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122// (&ll::_1)->*&EntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700123// }
124
125void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700126Entry::SetWaitingInVain (Ptr<Face> face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700127{
128 // NS_LOG_DEBUG (boost::cref (*face->m_face));
129
130 out_iterator item = m_outgoing.find (face);
131 if (item == m_outgoing.end ())
132 return;
Alexander Afanasyevc202fd92012-09-03 21:46:00 -0700133
134 const_cast<OutgoingFace&>(*item).m_waitingInVain = true;
135 // m_outgoing.modify (item,
136 // (&ll::_1)->*&OutgoingFace::m_waitingInVain = true);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800137}
138
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700139
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800140bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141Entry::AreAllOutgoingInVain () const
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800142{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800143 NS_LOG_DEBUG (m_outgoing.size ());
144
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800145 bool inVain = true;
146 std::for_each (m_outgoing.begin (), m_outgoing.end (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700147 ll::var(inVain) &= (&ll::_1)->*&OutgoingFace::m_waitingInVain);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800148
149 NS_LOG_DEBUG ("inVain " << inVain);
150 return inVain;
151}
152
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800153bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700154Entry::AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800155{
156 bool inVain = true;
157 std::for_each (m_outgoing.begin (), m_outgoing.end (),
158 ll::var(inVain) &=
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159 ((&ll::_1)->*&OutgoingFace::m_face == face ||
160 (&ll::_1)->*&OutgoingFace::m_waitingInVain));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800161
162 return !inVain;
163}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800164
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800165void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700166Entry::IncreaseAllowedRetxCount ()
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800167{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800168 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700169 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700170 {
171 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700172 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700173 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700174 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700175 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800176}
177
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700178std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800179{
180 os << "Prefix: " << *entry.m_prefix << "\n";
181 os << "In: ";
182 bool first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700183 BOOST_FOREACH (const IncomingFace &face, entry.m_incoming)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800184 {
185 if (!first)
186 os << ",";
187 else
188 first = false;
189
190 os << *face.m_face;
191 }
192
193 os << "\nOut: ";
194 first = true;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700195 BOOST_FOREACH (const OutgoingFace &face, entry.m_outgoing)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800196 {
197 if (!first)
198 os << ",";
199 else
200 first = false;
201
202 os << *face.m_face;
203 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700204 os << "\nNonces: ";
205 first = true;
206 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
207 {
208 if (!first)
209 os << ",";
210 else
211 first = false;
212
213 os << nonce;
214 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800215
216 return os;
217}
218
219
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700220} // namespace pit
221} // namespace ndn
222} // namespace ns3