blob: 3afdc0021fa4e96785bdbac78c6602caa8820e44 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifaf3eb02015-02-16 10:50:36 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shiaf6569a2014-06-14 00:01:34 -070024 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025
26#include "forwarder.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070028#include "core/random.hpp"
Junxiao Shifaf3eb02015-02-16 10:50:36 -070029#include "strategy.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070030#include <boost/random/uniform_int_distribution.hpp>
Alexander Afanasyev33b72772014-01-26 23:22:58 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080033
Junxiao Shi8c8d2182014-01-30 22:33:00 -070034NFD_LOG_INIT("Forwarder");
35
Junxiao Shif3c07812014-03-11 21:48:49 -070036using fw::Strategy;
37
Junxiao Shif3c07812014-03-11 21:48:49 -070038const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
Junxiao Shi88884492014-02-15 15:57:43 -070039
Junxiao Shic041ca32014-02-25 20:01:15 -070040Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070041 : m_faceTable(*this)
HangZhangad4afd12014-03-01 11:03:08 +080042 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060043 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080044 , m_measurements(m_nameTree)
Junxiao Shif3c07812014-03-11 21:48:49 -070045 , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
Alexander Afanasyev33b72772014-01-26 23:22:58 -080046{
Junxiao Shif3c07812014-03-11 21:48:49 -070047 fw::installStrategies(*this);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080048}
49
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060050Forwarder::~Forwarder()
51{
52
53}
54
Alexander Afanasyev33b72772014-01-26 23:22:58 -080055void
Junxiao Shid3c792f2014-01-30 00:46:13 -070056Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
57{
58 // receive Interest
Junxiao Shif3c07812014-03-11 21:48:49 -070059 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
60 " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070061 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -070062 ++m_counters.getNInInterests();
Junxiao Shic041ca32014-02-25 20:01:15 -070063
Junxiao Shi88884492014-02-15 15:57:43 -070064 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -070065 bool isViolatingLocalhost = !inFace.isLocal() &&
66 LOCALHOST_NAME.isPrefixOf(interest.getName());
67 if (isViolatingLocalhost) {
68 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
69 " interest=" << interest.getName() << " violates /localhost");
70 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -070071 return;
72 }
Junxiao Shic041ca32014-02-25 20:01:15 -070073
Junxiao Shid3c792f2014-01-30 00:46:13 -070074 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -070075 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -070076
Junxiao Shia110f262014-10-12 12:35:20 -070077 // detect duplicate Nonce
78 int dnw = pitEntry->findNonce(interest.getNonce(), inFace);
79 bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) ||
80 m_deadNonceList.has(interest.getName(), interest.getNonce());
81 if (hasDuplicateNonce) {
Junxiao Shid3c792f2014-01-30 00:46:13 -070082 // goto Interest loop pipeline
83 this->onInterestLoop(inFace, interest, pitEntry);
84 return;
85 }
Junxiao Shic041ca32014-02-25 20:01:15 -070086
Junxiao Shid3c792f2014-01-30 00:46:13 -070087 // cancel unsatisfy & straggler timer
88 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shif3c07812014-03-11 21:48:49 -070090 // is pending?
Junxiao Shid3c792f2014-01-30 00:46:13 -070091 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
Junxiao Shie17349a2014-03-25 00:55:38 -070092 bool isPending = inRecords.begin() != inRecords.end();
Junxiao Shid3c792f2014-01-30 00:46:13 -070093 if (!isPending) {
mzhang4eab72492015-02-25 11:16:09 -060094 m_cs.find(interest,
95 bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
96 bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
Junxiao Shid3c792f2014-01-30 00:46:13 -070097 }
mzhang4eab72492015-02-25 11:16:09 -060098 else {
99 this->onContentStoreMiss(inFace, pitEntry, interest);
100 }
101}
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
mzhang4eab72492015-02-25 11:16:09 -0600103void
104Forwarder::onContentStoreMiss(const Face& inFace,
105 shared_ptr<pit::Entry> pitEntry,
106 const Interest& interest)
107{
108 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
109
110 shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700111 // insert InRecord
mzhang4eab72492015-02-25 11:16:09 -0600112 pitEntry->insertOrUpdateInRecord(face, interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700113
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700114 // set PIT unsatisfy timer
115 this->setUnsatisfyTimer(pitEntry);
116
Junxiao Shid3c792f2014-01-30 00:46:13 -0700117 // FIB lookup
Junxiao Shi40631842014-03-01 13:52:37 -0700118 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
Junxiao Shid3c792f2014-01-30 00:46:13 -0700120 // dispatch to strategy
Junxiao Shif3c07812014-03-11 21:48:49 -0700121 this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700122 cref(inFace), cref(interest), fibEntry, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700123}
124
125void
mzhang4eab72492015-02-25 11:16:09 -0600126Forwarder::onContentStoreHit(const Face& inFace,
127 shared_ptr<pit::Entry> pitEntry,
128 const Interest& interest,
129 const Data& data)
130{
131 NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
132
133 const_pointer_cast<Data>(data.shared_from_this())->setIncomingFaceId(FACEID_CONTENT_STORE);
134 // XXX should we lookup PIT for other Interests that also match csMatch?
135
136 // set PIT straggler timer
137 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
138
139 // goto outgoing Data pipeline
140 this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this()));
141}
142
143void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700144Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
145 shared_ptr<pit::Entry> pitEntry)
146{
Junxiao Shif3c07812014-03-11 21:48:49 -0700147 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() <<
148 " interest=" << interest.getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700149
Junxiao Shif3c07812014-03-11 21:48:49 -0700150 // (drop)
151}
152
153/** \brief compare two InRecords for picking outgoing Interest
154 * \return true if b is preferred over a
155 *
156 * This function should be passed to std::max_element over InRecordCollection.
157 * The outgoing Interest picked is the last incoming Interest
158 * that does not come from outFace.
159 * If all InRecords come from outFace, it's fine to pick that. This happens when
160 * there's only one InRecord that comes from outFace. The legit use is for
161 * vehicular network; otherwise, strategy shouldn't send to the sole inFace.
162 */
163static inline bool
164compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace)
165{
166 bool isOutFaceA = a.getFace().get() == outFace;
167 bool isOutFaceB = b.getFace().get() == outFace;
168
169 if (!isOutFaceA && isOutFaceB) {
170 return false;
171 }
172 if (isOutFaceA && !isOutFaceB) {
173 return true;
174 }
175
176 return a.getLastRenewed() > b.getLastRenewed();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700177}
178
179void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700180Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
181 bool wantNewNonce)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700182{
Junxiao Shi223271b2014-07-03 22:06:13 -0700183 if (outFace.getId() == INVALID_FACEID) {
184 NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName());
185 return;
186 }
Junxiao Shif3c07812014-03-11 21:48:49 -0700187 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
188 " interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700189
Junxiao Shi57f0f312014-03-16 11:52:20 -0700190 // scope control
191 if (pitEntry->violatesScope(outFace)) {
Junxiao Shif3c07812014-03-11 21:48:49 -0700192 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
Junxiao Shi57f0f312014-03-16 11:52:20 -0700193 " interest=" << pitEntry->getName() << " violates scope");
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700194 return;
195 }
196
Junxiao Shid3c792f2014-01-30 00:46:13 -0700197 // pick Interest
Junxiao Shif3c07812014-03-11 21:48:49 -0700198 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
199 pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
200 inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace));
201 BOOST_ASSERT(pickedInRecord != inRecords.end());
Junxiao Shid938a6b2014-05-11 23:40:29 -0700202 shared_ptr<Interest> interest = const_pointer_cast<Interest>(
203 pickedInRecord->getInterest().shared_from_this());
204
205 if (wantNewNonce) {
206 interest = make_shared<Interest>(*interest);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700207 static boost::random::uniform_int_distribution<uint32_t> dist;
208 interest->setNonce(dist(getGlobalRng()));
Junxiao Shid938a6b2014-05-11 23:40:29 -0700209 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700210
Junxiao Shid3c792f2014-01-30 00:46:13 -0700211 // insert OutRecord
Junxiao Shid938a6b2014-05-11 23:40:29 -0700212 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700213
Junxiao Shid3c792f2014-01-30 00:46:13 -0700214 // send Interest
Junxiao Shid938a6b2014-05-11 23:40:29 -0700215 outFace.sendInterest(*interest);
Junxiao Shi33152f12014-07-16 19:54:32 -0700216 ++m_counters.getNOutInterests();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700217}
218
219void
Junxiao Shi09498f02014-02-26 19:41:08 -0700220Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700221{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700222 if (pitEntry->hasUnexpiredOutRecords()) {
223 NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() <<
224 " cannot reject forwarded Interest");
225 return;
226 }
Junxiao Shi09498f02014-02-26 19:41:08 -0700227 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700228
Alexander Afanasyeva57f8b42014-07-10 20:11:32 -0700229 // cancel unsatisfy & straggler timer
230 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
231
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700233 this->setStragglerTimer(pitEntry, false);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700234}
235
236void
237Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
238{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700239 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
240
Junxiao Shid3c792f2014-01-30 00:46:13 -0700241 // invoke PIT unsatisfied callback
Junxiao Shif3c07812014-03-11 21:48:49 -0700242 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1,
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700243 pitEntry));
Junxiao Shic041ca32014-02-25 20:01:15 -0700244
Junxiao Shia110f262014-10-12 12:35:20 -0700245 // goto Interest Finalize pipeline
246 this->onInterestFinalize(pitEntry, false);
247}
248
249void
250Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
251 const time::milliseconds& dataFreshnessPeriod)
252{
253 NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() <<
254 (isSatisfied ? " satisfied" : " unsatisfied"));
255
256 // Dead Nonce List insert if necessary
257 this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0);
258
Junxiao Shif3c07812014-03-11 21:48:49 -0700259 // PIT delete
Junxiao Shid938a6b2014-05-11 23:40:29 -0700260 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600261 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700262}
263
264void
265Forwarder::onIncomingData(Face& inFace, const Data& data)
266{
267 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700268 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700269 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shi33152f12014-07-16 19:54:32 -0700270 ++m_counters.getNInDatas();
Junxiao Shic041ca32014-02-25 20:01:15 -0700271
Junxiao Shi88884492014-02-15 15:57:43 -0700272 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700273 bool isViolatingLocalhost = !inFace.isLocal() &&
274 LOCALHOST_NAME.isPrefixOf(data.getName());
275 if (isViolatingLocalhost) {
276 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() <<
277 " data=" << data.getName() << " violates /localhost");
278 // (drop)
Junxiao Shi88884492014-02-15 15:57:43 -0700279 return;
280 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700281
Junxiao Shid3c792f2014-01-30 00:46:13 -0700282 // PIT match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700283 pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data);
284 if (pitMatches.begin() == pitMatches.end()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700285 // goto Data unsolicited pipeline
286 this->onDataUnsolicited(inFace, data);
287 return;
288 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700289
Junxiao Shid3c792f2014-01-30 00:46:13 -0700290 // CS insert
291 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Junxiao Shid3c792f2014-01-30 00:46:13 -0700293 std::set<shared_ptr<Face> > pendingDownstreams;
294 // foreach PitEntry
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700295 for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) {
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700296 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700297
Junxiao Shid3c792f2014-01-30 00:46:13 -0700298 // cancel unsatisfy & straggler timer
299 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700300
Junxiao Shid3c792f2014-01-30 00:46:13 -0700301 // remember pending downstreams
302 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
303 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
304 it != inRecords.end(); ++it) {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700305 if (it->getExpiry() > time::steady_clock::now()) {
Junxiao Shid3c792f2014-01-30 00:46:13 -0700306 pendingDownstreams.insert(it->getFace());
307 }
308 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700309
Junxiao Shid938a6b2014-05-11 23:40:29 -0700310 // invoke PIT satisfy callback
Junxiao Shi82e7f582014-09-07 15:15:40 -0700311 this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700312 pitEntry, cref(inFace), cref(data)));
313
Junxiao Shia110f262014-10-12 12:35:20 -0700314 // Dead Nonce List insert if necessary (for OutRecord of inFace)
315 this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace);
316
Junxiao Shid3c792f2014-01-30 00:46:13 -0700317 // mark PIT satisfied
318 pitEntry->deleteInRecords();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700319 pitEntry->deleteOutRecord(inFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700320
Junxiao Shid3c792f2014-01-30 00:46:13 -0700321 // set PIT straggler timer
Junxiao Shia110f262014-10-12 12:35:20 -0700322 this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700323 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700324
Junxiao Shid3c792f2014-01-30 00:46:13 -0700325 // foreach pending downstream
326 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
327 it != pendingDownstreams.end(); ++it) {
Junxiao Shida006f52014-05-16 11:18:00 -0700328 shared_ptr<Face> pendingDownstream = *it;
329 if (pendingDownstream.get() == &inFace) {
330 continue;
331 }
Junxiao Shid3c792f2014-01-30 00:46:13 -0700332 // goto outgoing Data pipeline
Junxiao Shida006f52014-05-16 11:18:00 -0700333 this->onOutgoingData(data, *pendingDownstream);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700334 }
335}
336
337void
338Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
339{
340 // accept to cache?
Junxiao Shif3c07812014-03-11 21:48:49 -0700341 bool acceptToCache = inFace.isLocal();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700342 if (acceptToCache) {
343 // CS insert
Junxiao Shif3c07812014-03-11 21:48:49 -0700344 m_cs.insert(data, true);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700345 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700346
Junxiao Shif3c07812014-03-11 21:48:49 -0700347 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
348 " data=" << data.getName() <<
349 (acceptToCache ? " cached" : " not cached"));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700350}
351
352void
353Forwarder::onOutgoingData(const Data& data, Face& outFace)
354{
Junxiao Shi223271b2014-07-03 22:06:13 -0700355 if (outFace.getId() == INVALID_FACEID) {
356 NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName());
357 return;
358 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700359 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
360
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700361 // /localhost scope control
Junxiao Shif3c07812014-03-11 21:48:49 -0700362 bool isViolatingLocalhost = !outFace.isLocal() &&
363 LOCALHOST_NAME.isPrefixOf(data.getName());
364 if (isViolatingLocalhost) {
365 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() <<
366 " data=" << data.getName() << " violates /localhost");
367 // (drop)
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700368 return;
369 }
370
Junxiao Shif3c07812014-03-11 21:48:49 -0700371 // TODO traffic manager
Junxiao Shic041ca32014-02-25 20:01:15 -0700372
Junxiao Shid3c792f2014-01-30 00:46:13 -0700373 // send Data
374 outFace.sendData(data);
Junxiao Shi33152f12014-07-16 19:54:32 -0700375 ++m_counters.getNOutDatas();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700376}
377
378static inline bool
379compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
380{
381 return a.getExpiry() < b.getExpiry();
382}
383
384void
385Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
386{
387 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
388 pit::InRecordCollection::const_iterator lastExpiring =
389 std::max_element(inRecords.begin(), inRecords.end(),
390 &compare_InRecord_expiry);
391
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700392 time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry();
393 time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now();
Junxiao Shid3c792f2014-01-30 00:46:13 -0700394 if (lastExpiryFromNow <= time::seconds(0)) {
395 // TODO all InRecords are already expired; will this happen?
396 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700397
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700398 scheduler::cancel(pitEntry->m_unsatisfyTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700399 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700400 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
401}
402
403void
Junxiao Shia110f262014-10-12 12:35:20 -0700404Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
405 const time::milliseconds& dataFreshnessPeriod)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700406{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700407 time::nanoseconds stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700408
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700409 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shic041ca32014-02-25 20:01:15 -0700410 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Junxiao Shia110f262014-10-12 12:35:20 -0700411 bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700412}
413
414void
415Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
416{
Junxiao Shic041ca32014-02-25 20:01:15 -0700417 scheduler::cancel(pitEntry->m_unsatisfyTimer);
418 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700419}
420
Junxiao Shia110f262014-10-12 12:35:20 -0700421static inline void
422insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
423 const pit::OutRecord& outRecord)
424{
425 dnl.add(pitEntry.getName(), outRecord.getLastNonce());
426}
427
428void
429Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
430 const time::milliseconds& dataFreshnessPeriod,
431 Face* upstream)
432{
433 // need Dead Nonce List insert?
434 bool needDnl = false;
435 if (isSatisfied) {
436 bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero();
437 // Data never becomes stale if it doesn't have FreshnessPeriod field
438 needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) &&
439 (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime());
440 }
441 else {
442 needDnl = true;
443 }
444
445 if (!needDnl) {
446 return;
447 }
448
449 // Dead Nonce List insert
450 if (upstream == 0) {
451 // insert all outgoing Nonces
452 const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
453 std::for_each(outRecords.begin(), outRecords.end(),
454 bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
455 }
456 else {
457 // insert outgoing Nonce of a specific face
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700458 pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream);
Junxiao Shia110f262014-10-12 12:35:20 -0700459 if (outRecord != pitEntry.getOutRecords().end()) {
460 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
461 }
462 }
463}
464
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800465} // namespace nfd