Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2022, Regents of the University of California, |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 8 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 9 | * |
| 10 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 22 | * |
| 23 | * @author Andrea Tosatto |
| 24 | * @author Davide Pesavento |
| 25 | */ |
| 26 | |
| 27 | #include "data-fetcher.hpp" |
| 28 | |
| 29 | #include <cmath> |
| 30 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 31 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 32 | |
| 33 | shared_ptr<DataFetcher> |
| 34 | DataFetcher::fetch(Face& face, const Interest& interest, int maxNackRetries, int maxTimeoutRetries, |
| 35 | DataCallback onData, FailureCallback onNack, FailureCallback onTimeout, |
| 36 | bool isVerbose) |
| 37 | { |
| 38 | auto dataFetcher = shared_ptr<DataFetcher>(new DataFetcher(face, |
| 39 | maxNackRetries, |
| 40 | maxTimeoutRetries, |
| 41 | std::move(onData), |
| 42 | std::move(onNack), |
| 43 | std::move(onTimeout), |
| 44 | isVerbose)); |
| 45 | dataFetcher->expressInterest(interest, dataFetcher); |
| 46 | return dataFetcher; |
| 47 | } |
| 48 | |
| 49 | DataFetcher::DataFetcher(Face& face, int maxNackRetries, int maxTimeoutRetries, |
| 50 | DataCallback onData, FailureCallback onNack, FailureCallback onTimeout, |
| 51 | bool isVerbose) |
| 52 | : m_face(face) |
| 53 | , m_scheduler(m_face.getIoService()) |
| 54 | , m_onData(std::move(onData)) |
| 55 | , m_onNack(std::move(onNack)) |
| 56 | , m_onTimeout(std::move(onTimeout)) |
| 57 | , m_maxNackRetries(maxNackRetries) |
| 58 | , m_maxTimeoutRetries(maxTimeoutRetries) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 59 | , m_isVerbose(isVerbose) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 60 | { |
| 61 | BOOST_ASSERT(m_onData != nullptr); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | DataFetcher::cancel() |
| 66 | { |
| 67 | if (isRunning()) { |
| 68 | m_isStopped = true; |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 69 | m_pendingInterest.cancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 70 | m_scheduler.cancelAllEvents(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | DataFetcher::expressInterest(const Interest& interest, const shared_ptr<DataFetcher>& self) |
| 76 | { |
| 77 | m_nCongestionRetries = 0; |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 78 | m_pendingInterest = m_face.expressInterest(interest, |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 79 | [=] (auto&&... args) { handleData(std::forward<decltype(args)>(args)..., self); }, |
| 80 | [=] (auto&&... args) { handleNack(std::forward<decltype(args)>(args)..., self); }, |
| 81 | [=] (auto&&... args) { handleTimeout(std::forward<decltype(args)>(args)..., self); }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | DataFetcher::handleData(const Interest& interest, const Data& data, |
| 86 | const shared_ptr<DataFetcher>& self) |
| 87 | { |
| 88 | if (!isRunning()) |
| 89 | return; |
| 90 | |
| 91 | m_isStopped = true; |
| 92 | m_onData(interest, data); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | DataFetcher::handleNack(const Interest& interest, const lp::Nack& nack, |
| 97 | const shared_ptr<DataFetcher>& self) |
| 98 | { |
| 99 | if (!isRunning()) |
| 100 | return; |
| 101 | |
| 102 | if (m_maxNackRetries != MAX_RETRIES_INFINITE) |
| 103 | ++m_nNacks; |
| 104 | |
| 105 | if (m_isVerbose) |
| 106 | std::cerr << "Received Nack with reason " << nack.getReason() |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 107 | << " for Interest " << interest << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 108 | |
| 109 | if (m_nNacks <= m_maxNackRetries || m_maxNackRetries == MAX_RETRIES_INFINITE) { |
| 110 | Interest newInterest(interest); |
| 111 | newInterest.refreshNonce(); |
| 112 | |
| 113 | switch (nack.getReason()) { |
| 114 | case lp::NackReason::DUPLICATE: { |
| 115 | expressInterest(newInterest, self); |
| 116 | break; |
| 117 | } |
| 118 | case lp::NackReason::CONGESTION: { |
| 119 | time::milliseconds backoffTime(static_cast<uint64_t>(std::pow(2, m_nCongestionRetries))); |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 120 | if (backoffTime > MAX_CONGESTION_BACKOFF_TIME) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 121 | backoffTime = MAX_CONGESTION_BACKOFF_TIME; |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 122 | } |
| 123 | else { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 124 | m_nCongestionRetries++; |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 125 | } |
| 126 | m_scheduler.schedule(backoffTime, [=] { expressInterest(newInterest, self); }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 127 | break; |
| 128 | } |
| 129 | default: { |
| 130 | m_hasError = true; |
| 131 | if (m_onNack) |
| 132 | m_onNack(interest, "Could not retrieve data for " + interest.getName().toUri() + |
| 133 | ", reason: " + boost::lexical_cast<std::string>(nack.getReason())); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | else { |
| 139 | m_hasError = true; |
| 140 | if (m_onNack) |
| 141 | m_onNack(interest, "Reached the maximum number of nack retries (" + to_string(m_maxNackRetries) + |
| 142 | ") while retrieving data for " + interest.getName().toUri()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | DataFetcher::handleTimeout(const Interest& interest, const shared_ptr<DataFetcher>& self) |
| 148 | { |
| 149 | if (!isRunning()) |
| 150 | return; |
| 151 | |
| 152 | if (m_maxTimeoutRetries != MAX_RETRIES_INFINITE) |
| 153 | ++m_nTimeouts; |
| 154 | |
| 155 | if (m_isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 156 | std::cerr << "Timeout for Interest " << interest << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 157 | |
| 158 | if (m_nTimeouts <= m_maxTimeoutRetries || m_maxTimeoutRetries == MAX_RETRIES_INFINITE) { |
| 159 | Interest newInterest(interest); |
| 160 | newInterest.refreshNonce(); |
| 161 | expressInterest(newInterest, self); |
| 162 | } |
| 163 | else { |
| 164 | m_hasError = true; |
| 165 | if (m_onTimeout) |
| 166 | m_onTimeout(interest, "Reached the maximum number of timeout retries (" + to_string(m_maxTimeoutRetries) + |
| 167 | ") while retrieving data for " + interest.getName().toUri()); |
| 168 | } |
| 169 | } |
| 170 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 171 | } // namespace ndn::chunks |