blob: bc6ca0e94cbedcbbcee487e30380bcf55bb2d102 [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 Afanasyev4aac5572012-08-09 10:49:55 -070035NS_LOG_COMPONENT_DEFINE ("NdnPitEntry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070036
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070037namespace ns3
38{
39
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040NdnPitEntry::NdnPitEntry (NdnPit &container,
41 Ptr<const NdnInterestHeader> header,
42 Ptr<NdnFibEntry> fibEntry)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070043 : m_container (container)
44 , m_prefix (header->GetNamePtr ())
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070045 , m_fibEntry (fibEntry)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070046 , m_expireTime (Simulator::Now () + (!header->GetInterestLifetime ().IsZero ()?
47 header->GetInterestLifetime ():
48 Seconds (1.0)))
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080049 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070050{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070051 // note that if interest lifetime is not set, the behavior is undefined
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070052}
53
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080054void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070055NdnPitEntry::UpdateLifetime (const Time &offsetTime)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080056{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070057 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
58
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080059 Time newExpireTime = Simulator::Now () + offsetTime;
60 if (newExpireTime > m_expireTime)
61 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070062
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070063 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080064}
65
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070066NdnPitEntry::in_iterator
67NdnPitEntry::AddIncoming (Ptr<NdnFace> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080068{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070069 std::pair<in_iterator,bool> ret =
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070070 m_incoming.insert (NdnPitEntryIncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080071
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070072 // NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080073
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080074 return ret.first;
75}
76
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080077void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070078NdnPitEntry::RemoveIncoming (Ptr<NdnFace> face)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080079{
80 m_incoming.erase (face);
81}
82
83
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070084NdnPitEntry::out_iterator
85NdnPitEntry::AddOutgoing (Ptr<NdnFace> face)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080086{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070087 std::pair<out_iterator,bool> ret =
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070088 m_outgoing.insert (NdnPitEntryOutgoingFace (face));
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080089
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080090 if (!ret.second)
91 { // outgoing face already exists
92 m_outgoing.modify (ret.first,
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070093 ll::bind (&NdnPitEntryOutgoingFace::UpdateOnRetransmit, ll::_1));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080094 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080095
96 return ret.first;
97}
98
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700100NdnPitEntry::RemoveAllReferencesToFace (Ptr<NdnFace> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800101{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700102 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700103
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800104 if (incoming != m_incoming.end ())
105 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700106
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700107 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700109
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110 if (outgoing != m_outgoing.end ())
111 m_outgoing.erase (outgoing);
112}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700113
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700114// void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700115// NdnPitEntry::SetWaitingInVain (NdnPitEntry::out_iterator face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700116// {
117// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800118
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700119// m_outgoing.modify (face,
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700120// (&ll::_1)->*&NdnPitEntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700121// }
122
123void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700124NdnPitEntry::SetWaitingInVain (Ptr<NdnFace> face)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700125{
126 // NS_LOG_DEBUG (boost::cref (*face->m_face));
127
128 out_iterator item = m_outgoing.find (face);
129 if (item == m_outgoing.end ())
130 return;
131
132 m_outgoing.modify (item,
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700133 (&ll::_1)->*&NdnPitEntryOutgoingFace::m_waitingInVain = true);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800134}
135
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700136
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800137bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700138NdnPitEntry::AreAllOutgoingInVain () const
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800139{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800140 NS_LOG_DEBUG (m_outgoing.size ());
141
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800142 bool inVain = true;
143 std::for_each (m_outgoing.begin (), m_outgoing.end (),
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700144 ll::var(inVain) &= (&ll::_1)->*&NdnPitEntryOutgoingFace::m_waitingInVain);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800145
146 NS_LOG_DEBUG ("inVain " << inVain);
147 return inVain;
148}
149
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800150bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700151NdnPitEntry::AreTherePromisingOutgoingFacesExcept (Ptr<NdnFace> face) const
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800152{
153 bool inVain = true;
154 std::for_each (m_outgoing.begin (), m_outgoing.end (),
155 ll::var(inVain) &=
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700156 ((&ll::_1)->*&NdnPitEntryOutgoingFace::m_face == face ||
157 (&ll::_1)->*&NdnPitEntryOutgoingFace::m_waitingInVain));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800158
159 return !inVain;
160}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800161
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800162void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700163NdnPitEntry::IncreaseAllowedRetxCount ()
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800164{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800165 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700166 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700167 {
168 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700169 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700170 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700171 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700172 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800173}
174
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700175std::ostream& operator<< (std::ostream& os, const NdnPitEntry &entry)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800176{
177 os << "Prefix: " << *entry.m_prefix << "\n";
178 os << "In: ";
179 bool first = true;
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700180 BOOST_FOREACH (const NdnPitEntryIncomingFace &face, entry.m_incoming)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800181 {
182 if (!first)
183 os << ",";
184 else
185 first = false;
186
187 os << *face.m_face;
188 }
189
190 os << "\nOut: ";
191 first = true;
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700192 BOOST_FOREACH (const NdnPitEntryOutgoingFace &face, entry.m_outgoing)
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800193 {
194 if (!first)
195 os << ",";
196 else
197 first = false;
198
199 os << *face.m_face;
200 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700201 os << "\nNonces: ";
202 first = true;
203 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
204 {
205 if (!first)
206 os << ",";
207 else
208 first = false;
209
210 os << nonce;
211 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800212
213 return os;
214}
215
216
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800217}