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