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 | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 26 | #include "available-strategies.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 29 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 31 | NFD_LOG_INIT("Forwarder"); |
| 32 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 33 | using fw::Strategy; |
| 34 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 35 | const Name Forwarder::LOCALHOST_NAME("ndn:/localhost"); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 37 | Forwarder::Forwarder() |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 38 | : m_faceTable(*this) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 39 | , m_nameTree(1024) // "1024" could be made as one configurable parameter of the forwarder. |
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, |
| 109 | boost::cref(inFace), boost::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 |
| 149 | Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace) |
| 150 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 151 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
| 152 | " interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 153 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 154 | // scope control |
| 155 | if (pitEntry->violatesScope(outFace)) { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 156 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 157 | " interest=" << pitEntry->getName() << " violates scope"); |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 161 | // pick Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 162 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 163 | pit::InRecordCollection::const_iterator pickedInRecord = std::max_element( |
| 164 | inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace)); |
| 165 | BOOST_ASSERT(pickedInRecord != inRecords.end()); |
| 166 | const Interest& interest = pickedInRecord->getInterest(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 167 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 168 | // insert OutRecord |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 169 | pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 170 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 171 | // set PIT unsatisfy timer |
| 172 | this->setUnsatisfyTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 174 | // send Interest |
| 175 | outFace.sendInterest(interest); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 176 | m_counters.getNOutInterests() ++; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 180 | Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 181 | { |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 182 | NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 183 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 184 | // set PIT straggler timer |
| 185 | this->setStragglerTimer(pitEntry); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry) |
| 190 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 191 | NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName()); |
| 192 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 193 | // invoke PIT unsatisfied callback |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 194 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1, |
| 195 | pitEntry)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 196 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 197 | // PIT delete |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 198 | m_pit.erase(pitEntry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void |
| 202 | Forwarder::onIncomingData(Face& inFace, const Data& data) |
| 203 | { |
| 204 | // receive Data |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 205 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 206 | const_cast<Data&>(data).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 207 | m_counters.getNInDatas() ++; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 208 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 209 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 210 | bool isViolatingLocalhost = !inFace.isLocal() && |
| 211 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 212 | if (isViolatingLocalhost) { |
| 213 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << |
| 214 | " data=" << data.getName() << " violates /localhost"); |
| 215 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 216 | return; |
| 217 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 218 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 219 | // PIT match |
| 220 | shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data); |
| 221 | if (pitMatches->begin() == pitMatches->end()) { |
| 222 | // goto Data unsolicited pipeline |
| 223 | this->onDataUnsolicited(inFace, data); |
| 224 | return; |
| 225 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 226 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 227 | // CS insert |
| 228 | m_cs.insert(data); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 229 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 230 | std::set<shared_ptr<Face> > pendingDownstreams; |
| 231 | // foreach PitEntry |
| 232 | for (pit::DataMatchResult::iterator it = pitMatches->begin(); |
| 233 | it != pitMatches->end(); ++it) { |
| 234 | shared_ptr<pit::Entry> pitEntry = *it; |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 235 | NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 236 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 237 | // cancel unsatisfy & straggler timer |
| 238 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
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 | // remember pending downstreams |
| 241 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 242 | for (pit::InRecordCollection::const_iterator it = inRecords.begin(); |
| 243 | it != inRecords.end(); ++it) { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 244 | if (it->getExpiry() > time::steady_clock::now()) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 245 | pendingDownstreams.insert(it->getFace()); |
| 246 | } |
| 247 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 248 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 249 | // mark PIT satisfied |
| 250 | pitEntry->deleteInRecords(); |
| 251 | pitEntry->deleteOutRecord(inFace.shared_from_this()); |
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 | // set PIT straggler timer |
| 254 | this->setStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 255 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 256 | // invoke PIT satisfy callback |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 257 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyPendingInterest, _1, |
| 258 | pitEntry, boost::cref(inFace), boost::cref(data))); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 259 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 260 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 261 | // foreach pending downstream |
| 262 | for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin(); |
| 263 | it != pendingDownstreams.end(); ++it) { |
| 264 | // goto outgoing Data pipeline |
| 265 | this->onOutgoingData(data, **it); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | Forwarder::onDataUnsolicited(Face& inFace, const Data& data) |
| 271 | { |
| 272 | // accept to cache? |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 273 | bool acceptToCache = inFace.isLocal(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 274 | if (acceptToCache) { |
| 275 | // CS insert |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 276 | m_cs.insert(data, true); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 277 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 278 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 279 | NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << |
| 280 | " data=" << data.getName() << |
| 281 | (acceptToCache ? " cached" : " not cached")); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void |
| 285 | Forwarder::onOutgoingData(const Data& data, Face& outFace) |
| 286 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 287 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); |
| 288 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 289 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 290 | bool isViolatingLocalhost = !outFace.isLocal() && |
| 291 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 292 | if (isViolatingLocalhost) { |
| 293 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << |
| 294 | " data=" << data.getName() << " violates /localhost"); |
| 295 | // (drop) |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 299 | // TODO traffic manager |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 300 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 301 | // send Data |
| 302 | outFace.sendData(data); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 303 | m_counters.getNOutDatas() ++; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static inline bool |
| 307 | compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b) |
| 308 | { |
| 309 | return a.getExpiry() < b.getExpiry(); |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry) |
| 314 | { |
| 315 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 316 | pit::InRecordCollection::const_iterator lastExpiring = |
| 317 | std::max_element(inRecords.begin(), inRecords.end(), |
| 318 | &compare_InRecord_expiry); |
| 319 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 320 | time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry(); |
| 321 | time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 322 | if (lastExpiryFromNow <= time::seconds(0)) { |
| 323 | // TODO all InRecords are already expired; will this happen? |
| 324 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 325 | |
| 326 | pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow, |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 327 | bind(&Forwarder::onInterestUnsatisfied, this, pitEntry)); |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 332 | { |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 333 | if (pitEntry->hasUnexpiredOutRecords()) { |
| 334 | NFD_LOG_DEBUG("setStragglerTimer " << pitEntry->getName() << |
| 335 | " cannot set StragglerTimer when an OutRecord is pending"); |
| 336 | return; |
| 337 | } |
| 338 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 339 | time::nanoseconds stragglerTime = time::milliseconds(100); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 340 | |
| 341 | pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime, |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 342 | bind(&Pit::erase, &m_pit, pitEntry)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
| 346 | Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 347 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 348 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
| 349 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 352 | } // namespace nfd |