Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 25 | |
| 26 | #include "forwarder.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 28 | #include "core/random.hpp" |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 29 | #include "strategy.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 30 | #include <boost/random/uniform_int_distribution.hpp> |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 32 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 33 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 34 | NFD_LOG_INIT("Forwarder"); |
| 35 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 36 | using fw::Strategy; |
| 37 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 38 | const Name Forwarder::LOCALHOST_NAME("ndn:/localhost"); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 40 | Forwarder::Forwarder() |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 41 | : m_faceTable(*this) |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 42 | , m_fib(m_nameTree) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 43 | , m_pit(m_nameTree) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 44 | , m_measurements(m_nameTree) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 45 | , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this)) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 46 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 47 | fw::installStrategies(*this); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 50 | Forwarder::~Forwarder() |
| 51 | { |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 52 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 53 | |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 54 | void |
| 55 | Forwarder::startProcessInterest(Face& face, const Interest& interest) |
| 56 | { |
| 57 | // check fields used by forwarding are well-formed |
| 58 | try { |
| 59 | if (interest.hasLink()) { |
| 60 | interest.getLink(); |
| 61 | } |
| 62 | } |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 63 | catch (const tlv::Error&) { |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 64 | NFD_LOG_DEBUG("startProcessInterest face=" << face.getId() << |
| 65 | " interest=" << interest.getName() << " malformed"); |
| 66 | // It's safe to call interest.getName() because Name has been fully parsed |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | this->onIncomingInterest(face, interest); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | Forwarder::startProcessData(Face& face, const Data& data) |
| 75 | { |
| 76 | // check fields used by forwarding are well-formed |
| 77 | // (none needed) |
| 78 | |
| 79 | this->onIncomingData(face, data); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 82 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 83 | Forwarder::startProcessNack(Face& face, const lp::Nack& nack) |
| 84 | { |
| 85 | // check fields used by forwarding are well-formed |
| 86 | try { |
| 87 | if (nack.getInterest().hasLink()) { |
| 88 | nack.getInterest().getLink(); |
| 89 | } |
| 90 | } |
| 91 | catch (const tlv::Error&) { |
| 92 | NFD_LOG_DEBUG("startProcessNack face=" << face.getId() << |
| 93 | " nack=" << nack.getInterest().getName() << |
| 94 | "~" << nack.getReason() << " malformed"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | this->onIncomingNack(face, nack); |
| 99 | } |
| 100 | |
| 101 | void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 102 | Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) |
| 103 | { |
| 104 | // receive Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 105 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 106 | " interest=" << interest.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 107 | const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 108 | ++m_counters.getNInInterests(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 110 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 111 | bool isViolatingLocalhost = !inFace.isLocal() && |
| 112 | LOCALHOST_NAME.isPrefixOf(interest.getName()); |
| 113 | if (isViolatingLocalhost) { |
| 114 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 115 | " interest=" << interest.getName() << " violates /localhost"); |
| 116 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 119 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 120 | // PIT insert |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 121 | shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 122 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 123 | // detect duplicate Nonce |
| 124 | int dnw = pitEntry->findNonce(interest.getNonce(), inFace); |
| 125 | bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) || |
| 126 | m_deadNonceList.has(interest.getName(), interest.getNonce()); |
| 127 | if (hasDuplicateNonce) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 128 | // goto Interest loop pipeline |
| 129 | this->onInterestLoop(inFace, interest, pitEntry); |
| 130 | return; |
| 131 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 132 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 133 | // cancel unsatisfy & straggler timer |
| 134 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 136 | // is pending? |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 137 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
Junxiao Shi | e17349a | 2014-03-25 00:55:38 -0700 | [diff] [blame] | 138 | bool isPending = inRecords.begin() != inRecords.end(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 139 | if (!isPending) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 140 | m_cs.find(interest, |
| 141 | bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2), |
| 142 | bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 143 | } |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 144 | else { |
| 145 | this->onContentStoreMiss(inFace, pitEntry, interest); |
| 146 | } |
| 147 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 148 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 149 | void |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 150 | Forwarder::onInterestLoop(Face& inFace, const Interest& interest, |
| 151 | shared_ptr<pit::Entry> pitEntry) |
| 152 | { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 153 | // if multi-access face, drop |
| 154 | if (inFace.isMultiAccess()) { |
| 155 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 156 | " interest=" << interest.getName() << |
| 157 | " drop"); |
| 158 | return; |
| 159 | } |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 160 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 161 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 162 | " interest=" << interest.getName() << |
| 163 | " send-Nack-duplicate"); |
| 164 | |
| 165 | // send Nack with reason=DUPLICATE |
| 166 | // note: Don't enter outgoing Nack pipeline because it needs an in-record. |
| 167 | lp::Nack nack(interest); |
| 168 | nack.setReason(lp::NackReason::DUPLICATE); |
| 169 | inFace.sendNack(nack); |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 173 | Forwarder::onContentStoreMiss(const Face& inFace, |
| 174 | shared_ptr<pit::Entry> pitEntry, |
| 175 | const Interest& interest) |
| 176 | { |
| 177 | NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()); |
| 178 | |
| 179 | shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 180 | // insert InRecord |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 181 | pitEntry->insertOrUpdateInRecord(face, interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 183 | // set PIT unsatisfy timer |
| 184 | this->setUnsatisfyTimer(pitEntry); |
| 185 | |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 186 | shared_ptr<fib::Entry> fibEntry; |
| 187 | // has Link object? |
| 188 | if (!interest.hasLink()) { |
| 189 | // FIB lookup with Interest name |
| 190 | fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
| 191 | NFD_LOG_TRACE("onContentStoreMiss noLinkObject"); |
| 192 | } |
| 193 | else { |
| 194 | const Link& link = interest.getLink(); |
| 195 | |
| 196 | // in producer region? |
| 197 | if (m_networkRegionTable.isInProducerRegion(link)) { |
| 198 | // FIB lookup with Interest name |
| 199 | fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
| 200 | NFD_LOG_TRACE("onContentStoreMiss inProducerRegion"); |
| 201 | } |
| 202 | // has SelectedDelegation? |
| 203 | else if (interest.hasSelectedDelegation()) { |
| 204 | // FIB lookup with SelectedDelegation |
| 205 | fibEntry = m_fib.findLongestPrefixMatch(interest.getSelectedDelegation()); |
| 206 | NFD_LOG_TRACE("onContentStoreMiss hasSelectedDelegation=" << interest.getSelectedDelegation()); |
| 207 | } |
| 208 | else { |
| 209 | // FIB lookup with first delegation Name |
| 210 | fibEntry = m_fib.findLongestPrefixMatch(link.getDelegations().begin()->second); |
| 211 | |
| 212 | // in default-free zone? |
| 213 | bool isDefaultFreeZone = !(fibEntry->getPrefix().size() == 0 && fibEntry->hasNextHops()); |
| 214 | if (isDefaultFreeZone) { |
| 215 | // choose and set SelectedDelegation |
| 216 | for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) { |
| 217 | const Name& delegationName = delegation.second; |
| 218 | fibEntry = m_fib.findLongestPrefixMatch(delegationName); |
| 219 | if (fibEntry->hasNextHops()) { |
| 220 | const_cast<Interest&>(interest).setSelectedDelegation(delegationName); |
| 221 | NFD_LOG_TRACE("onContentStoreMiss enterDefaultFreeZone" |
| 222 | << " setSelectedDelegation=" << delegationName); |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | else { |
| 228 | NFD_LOG_TRACE("onContentStoreMiss inConsumerRegion"); |
| 229 | } |
| 230 | } |
| 231 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 232 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 233 | // dispatch to strategy |
Junxiao Shi | 0355e9f | 2015-09-02 07:24:53 -0700 | [diff] [blame] | 234 | BOOST_ASSERT(fibEntry != nullptr); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 235 | this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 236 | cref(inFace), cref(interest), fibEntry, pitEntry)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 240 | Forwarder::onContentStoreHit(const Face& inFace, |
| 241 | shared_ptr<pit::Entry> pitEntry, |
| 242 | const Interest& interest, |
| 243 | const Data& data) |
| 244 | { |
Vince Lehman | faa5c0c | 2015-08-18 12:52:46 -0500 | [diff] [blame] | 245 | NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName()); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 246 | |
| 247 | const_pointer_cast<Data>(data.shared_from_this())->setIncomingFaceId(FACEID_CONTENT_STORE); |
| 248 | // XXX should we lookup PIT for other Interests that also match csMatch? |
| 249 | |
| 250 | // set PIT straggler timer |
| 251 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
| 252 | |
| 253 | // goto outgoing Data pipeline |
| 254 | this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this())); |
| 255 | } |
| 256 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 257 | /** \brief compare two InRecords for picking outgoing Interest |
| 258 | * \return true if b is preferred over a |
| 259 | * |
| 260 | * This function should be passed to std::max_element over InRecordCollection. |
| 261 | * The outgoing Interest picked is the last incoming Interest |
| 262 | * that does not come from outFace. |
| 263 | * If all InRecords come from outFace, it's fine to pick that. This happens when |
| 264 | * there's only one InRecord that comes from outFace. The legit use is for |
| 265 | * vehicular network; otherwise, strategy shouldn't send to the sole inFace. |
| 266 | */ |
| 267 | static inline bool |
| 268 | compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace) |
| 269 | { |
| 270 | bool isOutFaceA = a.getFace().get() == outFace; |
| 271 | bool isOutFaceB = b.getFace().get() == outFace; |
| 272 | |
| 273 | if (!isOutFaceA && isOutFaceB) { |
| 274 | return false; |
| 275 | } |
| 276 | if (isOutFaceA && !isOutFaceB) { |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | return a.getLastRenewed() > b.getLastRenewed(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 284 | Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace, |
| 285 | bool wantNewNonce) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 286 | { |
Junxiao Shi | 223271b | 2014-07-03 22:06:13 -0700 | [diff] [blame] | 287 | if (outFace.getId() == INVALID_FACEID) { |
| 288 | NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName()); |
| 289 | return; |
| 290 | } |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 291 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
| 292 | " interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 293 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 294 | // scope control |
| 295 | if (pitEntry->violatesScope(outFace)) { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 296 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 297 | " interest=" << pitEntry->getName() << " violates scope"); |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 301 | // pick Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 302 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 303 | pit::InRecordCollection::const_iterator pickedInRecord = std::max_element( |
| 304 | inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace)); |
| 305 | BOOST_ASSERT(pickedInRecord != inRecords.end()); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 306 | shared_ptr<Interest> interest = const_pointer_cast<Interest>( |
| 307 | pickedInRecord->getInterest().shared_from_this()); |
| 308 | |
| 309 | if (wantNewNonce) { |
| 310 | interest = make_shared<Interest>(*interest); |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 311 | static boost::random::uniform_int_distribution<uint32_t> dist; |
| 312 | interest->setNonce(dist(getGlobalRng())); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 313 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 314 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 315 | // insert OutRecord |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 316 | pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 317 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 318 | // send Interest |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 319 | outFace.sendInterest(*interest); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 320 | ++m_counters.getNOutInterests(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 324 | Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 325 | { |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 326 | if (pitEntry->hasUnexpiredOutRecords()) { |
| 327 | NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() << |
| 328 | " cannot reject forwarded Interest"); |
| 329 | return; |
| 330 | } |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 331 | NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 332 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 333 | // cancel unsatisfy & straggler timer |
| 334 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
| 335 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 336 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 337 | this->setStragglerTimer(pitEntry, false); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void |
| 341 | Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry) |
| 342 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 343 | NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName()); |
| 344 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 345 | // invoke PIT unsatisfied callback |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 346 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 347 | pitEntry)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 348 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 349 | // goto Interest Finalize pipeline |
| 350 | this->onInterestFinalize(pitEntry, false); |
| 351 | } |
| 352 | |
| 353 | void |
| 354 | Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied, |
| 355 | const time::milliseconds& dataFreshnessPeriod) |
| 356 | { |
| 357 | NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() << |
| 358 | (isSatisfied ? " satisfied" : " unsatisfied")); |
| 359 | |
| 360 | // Dead Nonce List insert if necessary |
| 361 | this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0); |
| 362 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 363 | // PIT delete |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 364 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 365 | m_pit.erase(pitEntry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void |
| 369 | Forwarder::onIncomingData(Face& inFace, const Data& data) |
| 370 | { |
| 371 | // receive Data |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 372 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 373 | const_cast<Data&>(data).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 374 | ++m_counters.getNInDatas(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 375 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 376 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 377 | bool isViolatingLocalhost = !inFace.isLocal() && |
| 378 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 379 | if (isViolatingLocalhost) { |
| 380 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << |
| 381 | " data=" << data.getName() << " violates /localhost"); |
| 382 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 385 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 386 | // PIT match |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 387 | pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data); |
| 388 | if (pitMatches.begin() == pitMatches.end()) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 389 | // goto Data unsolicited pipeline |
| 390 | this->onDataUnsolicited(inFace, data); |
| 391 | return; |
| 392 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 393 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 394 | // CS insert |
| 395 | m_cs.insert(data); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 396 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 397 | std::set<Face*> pendingDownstreams; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 398 | // foreach PitEntry |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 399 | for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 400 | NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 401 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 402 | // cancel unsatisfy & straggler timer |
| 403 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 404 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 405 | // remember pending downstreams |
| 406 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 407 | for (const pit::InRecord& inRecord : inRecords) { |
| 408 | if (inRecord.getExpiry() > time::steady_clock::now()) { |
| 409 | pendingDownstreams.insert(inRecord.getFace().get()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 412 | |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 413 | // invoke PIT satisfy callback |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 414 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1, |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 415 | pitEntry, cref(inFace), cref(data))); |
| 416 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 417 | // Dead Nonce List insert if necessary (for OutRecord of inFace) |
| 418 | this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace); |
| 419 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 420 | // mark PIT satisfied |
| 421 | pitEntry->deleteInRecords(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 422 | pitEntry->deleteOutRecord(inFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 423 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 424 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 425 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 426 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 427 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 428 | // foreach pending downstream |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 429 | for (Face* pendingDownstream : pendingDownstreams) { |
| 430 | if (pendingDownstream == &inFace) { |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 431 | continue; |
| 432 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 433 | // goto outgoing Data pipeline |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 434 | this->onOutgoingData(data, *pendingDownstream); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | void |
| 439 | Forwarder::onDataUnsolicited(Face& inFace, const Data& data) |
| 440 | { |
| 441 | // accept to cache? |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 442 | bool acceptToCache = inFace.isLocal(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 443 | if (acceptToCache) { |
| 444 | // CS insert |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 445 | m_cs.insert(data, true); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 446 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 447 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 448 | NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << |
| 449 | " data=" << data.getName() << |
| 450 | (acceptToCache ? " cached" : " not cached")); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void |
| 454 | Forwarder::onOutgoingData(const Data& data, Face& outFace) |
| 455 | { |
Junxiao Shi | 223271b | 2014-07-03 22:06:13 -0700 | [diff] [blame] | 456 | if (outFace.getId() == INVALID_FACEID) { |
| 457 | NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName()); |
| 458 | return; |
| 459 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 460 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); |
| 461 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 462 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 463 | bool isViolatingLocalhost = !outFace.isLocal() && |
| 464 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 465 | if (isViolatingLocalhost) { |
| 466 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << |
| 467 | " data=" << data.getName() << " violates /localhost"); |
| 468 | // (drop) |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 472 | // TODO traffic manager |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 473 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 474 | // send Data |
| 475 | outFace.sendData(data); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 476 | ++m_counters.getNOutDatas(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame^] | 479 | void |
| 480 | Forwarder::onIncomingNack(Face& inFace, const lp::Nack& nack) |
| 481 | { |
| 482 | // if multi-access face, drop |
| 483 | if (inFace.isMultiAccess()) { |
| 484 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 485 | " nack=" << nack.getInterest().getName() << |
| 486 | "~" << nack.getReason() << " face-is-multi-access"); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | // PIT match |
| 491 | shared_ptr<pit::Entry> pitEntry = m_pit.find(nack.getInterest()); |
| 492 | // if no PIT entry found, drop |
| 493 | if (pitEntry == nullptr) { |
| 494 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 495 | " nack=" << nack.getInterest().getName() << |
| 496 | "~" << nack.getReason() << " no-PIT-entry"); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | // has out-record? |
| 501 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace); |
| 502 | // if no out-record found, drop |
| 503 | if (outRecord == pitEntry->getOutRecords().end()) { |
| 504 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 505 | " nack=" << nack.getInterest().getName() << |
| 506 | "~" << nack.getReason() << " no-out-record"); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | // if out-record has different Nonce, drop |
| 511 | if (nack.getInterest().getNonce() != outRecord->getLastNonce()) { |
| 512 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 513 | " nack=" << nack.getInterest().getName() << |
| 514 | "~" << nack.getReason() << " wrong-Nonce " << |
| 515 | nack.getInterest().getNonce() << "!=" << outRecord->getLastNonce()); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | NFD_LOG_DEBUG("onIncomingNack face=" << inFace.getId() << |
| 520 | " nack=" << nack.getInterest().getName() << |
| 521 | "~" << nack.getReason() << " OK"); |
| 522 | |
| 523 | // record Nack on out-record |
| 524 | outRecord->setIncomingNack(nack); |
| 525 | |
| 526 | // trigger strategy: after receive NACK |
| 527 | shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
| 528 | this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveNack, _1, |
| 529 | cref(inFace), cref(nack), fibEntry, pitEntry)); |
| 530 | } |
| 531 | |
| 532 | void |
| 533 | Forwarder::onOutgoingNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace, |
| 534 | const lp::NackHeader& nack) |
| 535 | { |
| 536 | if (outFace.getId() == INVALID_FACEID) { |
| 537 | NFD_LOG_WARN("onOutgoingNack face=invalid" << |
| 538 | " nack=" << pitEntry->getInterest().getName() << |
| 539 | "~" << nack.getReason() << " no-in-record"); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | // has in-record? |
| 544 | pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(outFace); |
| 545 | |
| 546 | // if no in-record found, drop |
| 547 | if (inRecord == pitEntry->getInRecords().end()) { |
| 548 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 549 | " nack=" << pitEntry->getInterest().getName() << |
| 550 | "~" << nack.getReason() << " no-in-record"); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | // if multi-access face, drop |
| 555 | if (outFace.isMultiAccess()) { |
| 556 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 557 | " nack=" << pitEntry->getInterest().getName() << |
| 558 | "~" << nack.getReason() << " face-is-multi-access"); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | NFD_LOG_DEBUG("onOutgoingNack face=" << outFace.getId() << |
| 563 | " nack=" << pitEntry->getInterest().getName() << |
| 564 | "~" << nack.getReason() << " OK"); |
| 565 | |
| 566 | // create Nack packet with the Interest from in-record |
| 567 | lp::Nack nackPkt(inRecord->getInterest()); |
| 568 | nackPkt.setHeader(nack); |
| 569 | |
| 570 | // erase in-record |
| 571 | pitEntry->deleteInRecord(outFace); |
| 572 | |
| 573 | // send Nack on face |
| 574 | const_cast<Face&>(outFace).sendNack(nackPkt); |
| 575 | } |
| 576 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 577 | static inline bool |
| 578 | compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b) |
| 579 | { |
| 580 | return a.getExpiry() < b.getExpiry(); |
| 581 | } |
| 582 | |
| 583 | void |
| 584 | Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry) |
| 585 | { |
| 586 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 587 | pit::InRecordCollection::const_iterator lastExpiring = |
| 588 | std::max_element(inRecords.begin(), inRecords.end(), |
| 589 | &compare_InRecord_expiry); |
| 590 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 591 | time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry(); |
| 592 | time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 593 | if (lastExpiryFromNow <= time::seconds(0)) { |
| 594 | // TODO all InRecords are already expired; will this happen? |
| 595 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 596 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 597 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 598 | pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow, |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 599 | bind(&Forwarder::onInterestUnsatisfied, this, pitEntry)); |
| 600 | } |
| 601 | |
| 602 | void |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 603 | Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied, |
| 604 | const time::milliseconds& dataFreshnessPeriod) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 605 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 606 | time::nanoseconds stragglerTime = time::milliseconds(100); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 607 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 608 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 609 | pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime, |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 610 | bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void |
| 614 | Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 615 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 616 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
| 617 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 620 | static inline void |
| 621 | insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry, |
| 622 | const pit::OutRecord& outRecord) |
| 623 | { |
| 624 | dnl.add(pitEntry.getName(), outRecord.getLastNonce()); |
| 625 | } |
| 626 | |
| 627 | void |
| 628 | Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied, |
| 629 | const time::milliseconds& dataFreshnessPeriod, |
| 630 | Face* upstream) |
| 631 | { |
| 632 | // need Dead Nonce List insert? |
| 633 | bool needDnl = false; |
| 634 | if (isSatisfied) { |
| 635 | bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero(); |
| 636 | // Data never becomes stale if it doesn't have FreshnessPeriod field |
| 637 | needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) && |
| 638 | (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime()); |
| 639 | } |
| 640 | else { |
| 641 | needDnl = true; |
| 642 | } |
| 643 | |
| 644 | if (!needDnl) { |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | // Dead Nonce List insert |
| 649 | if (upstream == 0) { |
| 650 | // insert all outgoing Nonces |
| 651 | const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords(); |
| 652 | std::for_each(outRecords.begin(), outRecords.end(), |
| 653 | bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1)); |
| 654 | } |
| 655 | else { |
| 656 | // insert outgoing Nonce of a specific face |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 657 | pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 658 | if (outRecord != pitEntry.getOutRecords().end()) { |
| 659 | m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce()); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 664 | } // namespace nfd |