blob: 491896ee365a867836bf1632ef4749868abef040 [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"
22#include "ccnx-name-components.h"
23#include "ccnx-fib.h"
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070024#include "ccnx-interest-header.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070025
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070026#include "ns3/simulator.h"
Alexander Afanasyev5a595072011-11-25 14:49:07 -080027#include "ns3/log.h"
28
29#include <boost/lambda/lambda.hpp>
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080030#include <boost/lambda/bind.hpp>
Alexander Afanasyev78057c32012-07-06 15:18:46 -070031#include <boost/foreach.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080032namespace ll = boost::lambda;
33
34NS_LOG_COMPONENT_DEFINE ("CcnxPitEntry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070035
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070036namespace ns3
37{
38
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070039CcnxPitEntry::CcnxPitEntry (Ptr<const CcnxInterestHeader> header,
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070040 Ptr<CcnxFibEntry> fibEntry)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070041 : m_prefix (header->GetNamePtr ())
42 , m_expireTime (Simulator::Now () + header->GetInterestLifetime ())
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070043 , m_fibEntry (fibEntry)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080044 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070045{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070046 // note that if interest lifetime is not set, the behavior is undefined
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070047}
48
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080049void
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070050CcnxPitEntry::SetExpireTime (const Time &expireTime)
51{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070052 NS_LOG_FUNCTION (expireTime.ToDouble (Time::S));
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070053 m_expireTime = expireTime;
54}
55
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070056void
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080057CcnxPitEntry::UpdateLifetime (const Time &offsetTime)
58{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070059 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
60
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080061 Time newExpireTime = Simulator::Now () + offsetTime;
62 if (newExpireTime > m_expireTime)
63 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070064
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070065 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080066}
67
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070068CcnxPitEntry::in_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080069CcnxPitEntry::AddIncoming (Ptr<CcnxFace> face)
70{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070071 std::pair<in_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080072 m_incoming.insert (CcnxPitEntryIncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080073
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080074 NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080075
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080076 return ret.first;
77}
78
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080079void
80CcnxPitEntry::RemoveIncoming (Ptr<CcnxFace> face)
81{
82 m_incoming.erase (face);
83}
84
85
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070086CcnxPitEntry::out_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080087CcnxPitEntry::AddOutgoing (Ptr<CcnxFace> face)
88{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070089 std::pair<out_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080090 m_outgoing.insert (CcnxPitEntryOutgoingFace (face));
91
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080092 if (!ret.second)
93 { // outgoing face already exists
94 m_outgoing.modify (ret.first,
95 ll::bind (&CcnxPitEntryOutgoingFace::UpdateOnRetransmit, ll::_1));
96 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080097
98 return ret.first;
99}
100
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800101void
102CcnxPitEntry::RemoveAllReferencesToFace (Ptr<CcnxFace> face)
103{
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 Afanasyev5a595072011-11-25 14:49:07 -0800116void
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700117CcnxPitEntry::SetWaitingInVain (CcnxPitEntry::out_iterator face)
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800118{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800119 NS_LOG_DEBUG (boost::cref (*face->m_face));
120
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800121 m_outgoing.modify (face,
122 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain = true);
123}
124
125bool
126CcnxPitEntry::AreAllOutgoingInVain () const
127{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800128 NS_LOG_DEBUG (m_outgoing.size ());
129
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800130 bool inVain = true;
131 std::for_each (m_outgoing.begin (), m_outgoing.end (),
132 ll::var(inVain) &= (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain);
133
134 NS_LOG_DEBUG ("inVain " << inVain);
135 return inVain;
136}
137
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800138bool
139CcnxPitEntry::AreTherePromisingOutgoingFacesExcept (Ptr<CcnxFace> face) const
140{
141 bool inVain = true;
142 std::for_each (m_outgoing.begin (), m_outgoing.end (),
143 ll::var(inVain) &=
144 ((&ll::_1)->*&CcnxPitEntryOutgoingFace::m_face == face ||
145 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain));
146
147 return !inVain;
148}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800149
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800150void
151CcnxPitEntry::IncreaseAllowedRetxCount ()
152{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800153 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700154 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700155 {
156 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700157 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700158 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700159 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700160 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800161}
162
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800163std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry)
164{
165 os << "Prefix: " << *entry.m_prefix << "\n";
166 os << "In: ";
167 bool first = true;
168 BOOST_FOREACH (const CcnxPitEntryIncomingFace &face, entry.m_incoming)
169 {
170 if (!first)
171 os << ",";
172 else
173 first = false;
174
175 os << *face.m_face;
176 }
177
178 os << "\nOut: ";
179 first = true;
180 BOOST_FOREACH (const CcnxPitEntryOutgoingFace &face, entry.m_outgoing)
181 {
182 if (!first)
183 os << ",";
184 else
185 first = false;
186
187 os << *face.m_face;
188 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700189 os << "\nNonces: ";
190 first = true;
191 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
192 {
193 if (!first)
194 os << ",";
195 else
196 first = false;
197
198 os << nonce;
199 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800200
201 return os;
202}
203
204
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800205}