blob: 081b1557fa8facb827f8df83ab6a1ccda201084f [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
21#include "ccnx-pit-entry.h"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070022
23#include "ns3/ccnx-fib.h"
24#include "ns3/ccnx-name-components.h"
25#include "ns3/ccnx-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
35NS_LOG_COMPONENT_DEFINE ("CcnxPitEntry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070036
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070037namespace ns3
38{
39
Alexander Afanasyev36b45772012-07-10 16:57:42 -070040CcnxPitEntry::CcnxPitEntry (CcnxPit &container,
41 Ptr<const CcnxInterestHeader> header,
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070042 Ptr<CcnxFibEntry> 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
55CcnxPitEntry::UpdateLifetime (const Time &offsetTime)
56{
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 Afanasyevf034cbd2012-06-29 14:28:31 -070066CcnxPitEntry::in_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080067CcnxPitEntry::AddIncoming (Ptr<CcnxFace> face)
68{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070069 std::pair<in_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080070 m_incoming.insert (CcnxPitEntryIncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080071
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080072 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
78CcnxPitEntry::RemoveIncoming (Ptr<CcnxFace> face)
79{
80 m_incoming.erase (face);
81}
82
83
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070084CcnxPitEntry::out_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080085CcnxPitEntry::AddOutgoing (Ptr<CcnxFace> face)
86{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070087 std::pair<out_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080088 m_outgoing.insert (CcnxPitEntryOutgoingFace (face));
89
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080090 if (!ret.second)
91 { // outgoing face already exists
92 m_outgoing.modify (ret.first,
93 ll::bind (&CcnxPitEntryOutgoingFace::UpdateOnRetransmit, ll::_1));
94 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080095
96 return ret.first;
97}
98
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099void
100CcnxPitEntry::RemoveAllReferencesToFace (Ptr<CcnxFace> face)
101{
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 Afanasyev5a595072011-11-25 14:49:07 -0800114void
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700115CcnxPitEntry::SetWaitingInVain (CcnxPitEntry::out_iterator face)
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800116{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800117 NS_LOG_DEBUG (boost::cref (*face->m_face));
118
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800119 m_outgoing.modify (face,
120 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain = true);
121}
122
123bool
124CcnxPitEntry::AreAllOutgoingInVain () const
125{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800126 NS_LOG_DEBUG (m_outgoing.size ());
127
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800128 bool inVain = true;
129 std::for_each (m_outgoing.begin (), m_outgoing.end (),
130 ll::var(inVain) &= (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain);
131
132 NS_LOG_DEBUG ("inVain " << inVain);
133 return inVain;
134}
135
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800136bool
137CcnxPitEntry::AreTherePromisingOutgoingFacesExcept (Ptr<CcnxFace> face) const
138{
139 bool inVain = true;
140 std::for_each (m_outgoing.begin (), m_outgoing.end (),
141 ll::var(inVain) &=
142 ((&ll::_1)->*&CcnxPitEntryOutgoingFace::m_face == face ||
143 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain));
144
145 return !inVain;
146}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800147
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800148void
149CcnxPitEntry::IncreaseAllowedRetxCount ()
150{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800151 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700152 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700153 {
154 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700155 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700156 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700157 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700158 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800159}
160
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800161std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry)
162{
163 os << "Prefix: " << *entry.m_prefix << "\n";
164 os << "In: ";
165 bool first = true;
166 BOOST_FOREACH (const CcnxPitEntryIncomingFace &face, entry.m_incoming)
167 {
168 if (!first)
169 os << ",";
170 else
171 first = false;
172
173 os << *face.m_face;
174 }
175
176 os << "\nOut: ";
177 first = true;
178 BOOST_FOREACH (const CcnxPitEntryOutgoingFace &face, entry.m_outgoing)
179 {
180 if (!first)
181 os << ",";
182 else
183 first = false;
184
185 os << *face.m_face;
186 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700187 os << "\nNonces: ";
188 first = true;
189 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
190 {
191 if (!first)
192 os << ",";
193 else
194 first = false;
195
196 os << nonce;
197 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800198
199 return os;
200}
201
202
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800203}