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