Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 24 | |
| 25 | #include "forwarder.hpp" |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/util/random.hpp> |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 28 | #include "available-strategies.hpp" |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 30 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 31 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 32 | NFD_LOG_INIT("Forwarder"); |
| 33 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 34 | using fw::Strategy; |
| 35 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 36 | const Name Forwarder::LOCALHOST_NAME("ndn:/localhost"); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 38 | Forwarder::Forwarder() |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 39 | : m_faceTable(*this) |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 40 | , m_fib(m_nameTree) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 41 | , m_pit(m_nameTree) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 42 | , m_measurements(m_nameTree) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 43 | , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this)) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 44 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 45 | fw::installStrategies(*this); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 48 | Forwarder::~Forwarder() |
| 49 | { |
| 50 | |
| 51 | } |
| 52 | |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 53 | void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 54 | Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) |
| 55 | { |
| 56 | // receive Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 57 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 58 | " interest=" << interest.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 59 | const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 60 | m_counters.getNInInterests() ++; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 62 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 63 | bool isViolatingLocalhost = !inFace.isLocal() && |
| 64 | LOCALHOST_NAME.isPrefixOf(interest.getName()); |
| 65 | if (isViolatingLocalhost) { |
| 66 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 67 | " interest=" << interest.getName() << " violates /localhost"); |
| 68 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 69 | return; |
| 70 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 72 | // PIT insert |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 73 | shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 75 | // detect loop and record Nonce |
| 76 | bool isLoop = ! pitEntry->addNonce(interest.getNonce()); |
| 77 | if (isLoop) { |
| 78 | // goto Interest loop pipeline |
| 79 | this->onInterestLoop(inFace, interest, pitEntry); |
| 80 | return; |
| 81 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 83 | // cancel unsatisfy & straggler timer |
| 84 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 86 | // is pending? |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 87 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
Junxiao Shi | e17349a | 2014-03-25 00:55:38 -0700 | [diff] [blame] | 88 | bool isPending = inRecords.begin() != inRecords.end(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 89 | if (!isPending) { |
| 90 | // CS lookup |
| 91 | const Data* csMatch = m_cs.find(interest); |
| 92 | if (csMatch != 0) { |
| 93 | // XXX should we lookup PIT for other Interests that also match csMatch? |
| 94 | |
| 95 | // goto outgoing Data pipeline |
| 96 | this->onOutgoingData(*csMatch, inFace); |
| 97 | return; |
| 98 | } |
| 99 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 101 | // insert InRecord |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 102 | pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 103 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 104 | // FIB lookup |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 105 | shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 106 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 107 | // dispatch to strategy |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 108 | this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 109 | cref(inFace), cref(interest), fibEntry, pitEntry)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
| 113 | Forwarder::onInterestLoop(Face& inFace, const Interest& interest, |
| 114 | shared_ptr<pit::Entry> pitEntry) |
| 115 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 116 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 117 | " interest=" << interest.getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 119 | // (drop) |
| 120 | } |
| 121 | |
| 122 | /** \brief compare two InRecords for picking outgoing Interest |
| 123 | * \return true if b is preferred over a |
| 124 | * |
| 125 | * This function should be passed to std::max_element over InRecordCollection. |
| 126 | * The outgoing Interest picked is the last incoming Interest |
| 127 | * that does not come from outFace. |
| 128 | * If all InRecords come from outFace, it's fine to pick that. This happens when |
| 129 | * there's only one InRecord that comes from outFace. The legit use is for |
| 130 | * vehicular network; otherwise, strategy shouldn't send to the sole inFace. |
| 131 | */ |
| 132 | static inline bool |
| 133 | compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace) |
| 134 | { |
| 135 | bool isOutFaceA = a.getFace().get() == outFace; |
| 136 | bool isOutFaceB = b.getFace().get() == outFace; |
| 137 | |
| 138 | if (!isOutFaceA && isOutFaceB) { |
| 139 | return false; |
| 140 | } |
| 141 | if (isOutFaceA && !isOutFaceB) { |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | return a.getLastRenewed() > b.getLastRenewed(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 149 | Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace, |
| 150 | bool wantNewNonce) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 151 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 152 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
| 153 | " interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 154 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 155 | // scope control |
| 156 | if (pitEntry->violatesScope(outFace)) { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 157 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 158 | " interest=" << pitEntry->getName() << " violates scope"); |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 162 | // pick Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 163 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 164 | pit::InRecordCollection::const_iterator pickedInRecord = std::max_element( |
| 165 | inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace)); |
| 166 | BOOST_ASSERT(pickedInRecord != inRecords.end()); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 167 | shared_ptr<Interest> interest = const_pointer_cast<Interest>( |
| 168 | pickedInRecord->getInterest().shared_from_this()); |
| 169 | |
| 170 | if (wantNewNonce) { |
| 171 | interest = make_shared<Interest>(*interest); |
| 172 | interest->setNonce(ndn::random::generateWord32()); |
| 173 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 174 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 175 | // insert OutRecord |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 176 | pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 178 | // set PIT unsatisfy timer |
| 179 | this->setUnsatisfyTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 180 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 181 | // send Interest |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 182 | outFace.sendInterest(*interest); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 183 | m_counters.getNOutInterests() ++; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 187 | Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 188 | { |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 189 | if (pitEntry->hasUnexpiredOutRecords()) { |
| 190 | NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() << |
| 191 | " cannot reject forwarded Interest"); |
| 192 | return; |
| 193 | } |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 194 | NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 195 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 196 | // set PIT straggler timer |
| 197 | this->setStragglerTimer(pitEntry); |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry) |
| 202 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 203 | NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName()); |
| 204 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 205 | // invoke PIT unsatisfied callback |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 206 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 207 | pitEntry)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 208 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 209 | // PIT delete |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 210 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 211 | m_pit.erase(pitEntry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
| 215 | Forwarder::onIncomingData(Face& inFace, const Data& data) |
| 216 | { |
| 217 | // receive Data |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 218 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 219 | const_cast<Data&>(data).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 220 | m_counters.getNInDatas() ++; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 221 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 222 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 223 | bool isViolatingLocalhost = !inFace.isLocal() && |
| 224 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 225 | if (isViolatingLocalhost) { |
| 226 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << |
| 227 | " data=" << data.getName() << " violates /localhost"); |
| 228 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 229 | return; |
| 230 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 231 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 232 | // PIT match |
| 233 | shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data); |
| 234 | if (pitMatches->begin() == pitMatches->end()) { |
| 235 | // goto Data unsolicited pipeline |
| 236 | this->onDataUnsolicited(inFace, data); |
| 237 | return; |
| 238 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 239 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 240 | // CS insert |
| 241 | m_cs.insert(data); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 242 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 243 | std::set<shared_ptr<Face> > pendingDownstreams; |
| 244 | // foreach PitEntry |
| 245 | for (pit::DataMatchResult::iterator it = pitMatches->begin(); |
| 246 | it != pitMatches->end(); ++it) { |
| 247 | shared_ptr<pit::Entry> pitEntry = *it; |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 248 | NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 249 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 250 | // cancel unsatisfy & straggler timer |
| 251 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 252 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 253 | // remember pending downstreams |
| 254 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 255 | for (pit::InRecordCollection::const_iterator it = inRecords.begin(); |
| 256 | it != inRecords.end(); ++it) { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 257 | if (it->getExpiry() > time::steady_clock::now()) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 258 | pendingDownstreams.insert(it->getFace()); |
| 259 | } |
| 260 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 261 | |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 262 | // invoke PIT satisfy callback |
| 263 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyPendingInterest, _1, |
| 264 | pitEntry, cref(inFace), cref(data))); |
| 265 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 266 | // mark PIT satisfied |
| 267 | pitEntry->deleteInRecords(); |
| 268 | pitEntry->deleteOutRecord(inFace.shared_from_this()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 269 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 270 | // set PIT straggler timer |
| 271 | this->setStragglerTimer(pitEntry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 272 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 273 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 274 | // foreach pending downstream |
| 275 | for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin(); |
| 276 | it != pendingDownstreams.end(); ++it) { |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 277 | shared_ptr<Face> pendingDownstream = *it; |
| 278 | if (pendingDownstream.get() == &inFace) { |
| 279 | continue; |
| 280 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 281 | // goto outgoing Data pipeline |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 282 | this->onOutgoingData(data, *pendingDownstream); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | Forwarder::onDataUnsolicited(Face& inFace, const Data& data) |
| 288 | { |
| 289 | // accept to cache? |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 290 | bool acceptToCache = inFace.isLocal(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 291 | if (acceptToCache) { |
| 292 | // CS insert |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 293 | m_cs.insert(data, true); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 294 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 295 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 296 | NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << |
| 297 | " data=" << data.getName() << |
| 298 | (acceptToCache ? " cached" : " not cached")); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void |
| 302 | Forwarder::onOutgoingData(const Data& data, Face& outFace) |
| 303 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 304 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); |
| 305 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 306 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 307 | bool isViolatingLocalhost = !outFace.isLocal() && |
| 308 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 309 | if (isViolatingLocalhost) { |
| 310 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << |
| 311 | " data=" << data.getName() << " violates /localhost"); |
| 312 | // (drop) |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 316 | // TODO traffic manager |
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 Data |
| 319 | outFace.sendData(data); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 320 | m_counters.getNOutDatas() ++; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static inline bool |
| 324 | compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b) |
| 325 | { |
| 326 | return a.getExpiry() < b.getExpiry(); |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry) |
| 331 | { |
| 332 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 333 | pit::InRecordCollection::const_iterator lastExpiring = |
| 334 | std::max_element(inRecords.begin(), inRecords.end(), |
| 335 | &compare_InRecord_expiry); |
| 336 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 337 | time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry(); |
| 338 | time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 339 | if (lastExpiryFromNow <= time::seconds(0)) { |
| 340 | // TODO all InRecords are already expired; will this happen? |
| 341 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 342 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 343 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 344 | pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow, |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 345 | bind(&Forwarder::onInterestUnsatisfied, this, pitEntry)); |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 350 | { |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 351 | if (pitEntry->hasUnexpiredOutRecords()) { |
| 352 | NFD_LOG_DEBUG("setStragglerTimer " << pitEntry->getName() << |
| 353 | " cannot set StragglerTimer when an OutRecord is pending"); |
| 354 | return; |
| 355 | } |
| 356 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 357 | time::nanoseconds stragglerTime = time::milliseconds(100); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 358 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 359 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 360 | pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime, |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 361 | bind(&Pit::erase, &m_pit, pitEntry)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void |
| 365 | Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 366 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 367 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
| 368 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 371 | } // namespace nfd |