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 | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, 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 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
| 30 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 31 | #include <cmath> |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 32 | #include <iostream> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 33 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 34 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 36 | std::shared_ptr<DataFetcher> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 37 | DataFetcher::fetch(Face& face, const Interest& interest, int maxNackRetries, int maxTimeoutRetries, |
| 38 | DataCallback onData, FailureCallback onNack, FailureCallback onTimeout, |
| 39 | bool isVerbose) |
| 40 | { |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 41 | auto dataFetcher = std::shared_ptr<DataFetcher>(new DataFetcher(face, |
| 42 | maxNackRetries, |
| 43 | maxTimeoutRetries, |
| 44 | std::move(onData), |
| 45 | std::move(onNack), |
| 46 | std::move(onTimeout), |
| 47 | isVerbose)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 48 | dataFetcher->expressInterest(interest, dataFetcher); |
| 49 | return dataFetcher; |
| 50 | } |
| 51 | |
| 52 | DataFetcher::DataFetcher(Face& face, int maxNackRetries, int maxTimeoutRetries, |
| 53 | DataCallback onData, FailureCallback onNack, FailureCallback onTimeout, |
| 54 | bool isVerbose) |
| 55 | : m_face(face) |
Davide Pesavento | 7e9d7e4 | 2023-11-11 15:00:03 -0500 | [diff] [blame] | 56 | , m_scheduler(m_face.getIoContext()) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 57 | , m_onData(std::move(onData)) |
| 58 | , m_onNack(std::move(onNack)) |
| 59 | , m_onTimeout(std::move(onTimeout)) |
| 60 | , m_maxNackRetries(maxNackRetries) |
| 61 | , m_maxTimeoutRetries(maxTimeoutRetries) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 62 | , m_isVerbose(isVerbose) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 63 | { |
| 64 | BOOST_ASSERT(m_onData != nullptr); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | DataFetcher::cancel() |
| 69 | { |
| 70 | if (isRunning()) { |
| 71 | m_isStopped = true; |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 72 | m_pendingInterest.cancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 73 | m_scheduler.cancelAllEvents(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 78 | DataFetcher::expressInterest(const Interest& interest, const std::shared_ptr<DataFetcher>& self) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 79 | { |
| 80 | m_nCongestionRetries = 0; |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 81 | m_pendingInterest = m_face.expressInterest(interest, |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 82 | [this, self] (auto&&... args) { handleData(std::forward<decltype(args)>(args)..., self); }, |
| 83 | [this, self] (auto&&... args) { handleNack(std::forward<decltype(args)>(args)..., self); }, |
| 84 | [this, self] (auto&&... args) { handleTimeout(std::forward<decltype(args)>(args)..., self); }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | DataFetcher::handleData(const Interest& interest, const Data& data, |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 89 | const std::shared_ptr<DataFetcher>& self) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 90 | { |
| 91 | if (!isRunning()) |
| 92 | return; |
| 93 | |
| 94 | m_isStopped = true; |
| 95 | m_onData(interest, data); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | DataFetcher::handleNack(const Interest& interest, const lp::Nack& nack, |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 100 | const std::shared_ptr<DataFetcher>& self) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 101 | { |
| 102 | if (!isRunning()) |
| 103 | return; |
| 104 | |
| 105 | if (m_maxNackRetries != MAX_RETRIES_INFINITE) |
| 106 | ++m_nNacks; |
| 107 | |
| 108 | if (m_isVerbose) |
| 109 | std::cerr << "Received Nack with reason " << nack.getReason() |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 110 | << " for Interest " << interest << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 111 | |
| 112 | if (m_nNacks <= m_maxNackRetries || m_maxNackRetries == MAX_RETRIES_INFINITE) { |
| 113 | Interest newInterest(interest); |
| 114 | newInterest.refreshNonce(); |
| 115 | |
| 116 | switch (nack.getReason()) { |
| 117 | case lp::NackReason::DUPLICATE: { |
| 118 | expressInterest(newInterest, self); |
| 119 | break; |
| 120 | } |
| 121 | case lp::NackReason::CONGESTION: { |
| 122 | time::milliseconds backoffTime(static_cast<uint64_t>(std::pow(2, m_nCongestionRetries))); |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 123 | if (backoffTime > MAX_CONGESTION_BACKOFF_TIME) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 124 | backoffTime = MAX_CONGESTION_BACKOFF_TIME; |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 125 | } |
| 126 | else { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 127 | m_nCongestionRetries++; |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 128 | } |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 129 | m_scheduler.schedule(backoffTime, [this, newInterest, self] { |
| 130 | expressInterest(newInterest, self); |
| 131 | }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | default: { |
| 135 | m_hasError = true; |
| 136 | if (m_onNack) |
| 137 | m_onNack(interest, "Could not retrieve data for " + interest.getName().toUri() + |
| 138 | ", reason: " + boost::lexical_cast<std::string>(nack.getReason())); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | else { |
| 144 | m_hasError = true; |
| 145 | if (m_onNack) |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 146 | m_onNack(interest, "Reached the maximum number of nack retries (" + std::to_string(m_maxNackRetries) + |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 147 | ") while retrieving data for " + interest.getName().toUri()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 152 | DataFetcher::handleTimeout(const Interest& interest, const std::shared_ptr<DataFetcher>& self) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 153 | { |
| 154 | if (!isRunning()) |
| 155 | return; |
| 156 | |
| 157 | if (m_maxTimeoutRetries != MAX_RETRIES_INFINITE) |
| 158 | ++m_nTimeouts; |
| 159 | |
| 160 | if (m_isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 161 | std::cerr << "Timeout for Interest " << interest << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 162 | |
| 163 | if (m_nTimeouts <= m_maxTimeoutRetries || m_maxTimeoutRetries == MAX_RETRIES_INFINITE) { |
| 164 | Interest newInterest(interest); |
| 165 | newInterest.refreshNonce(); |
| 166 | expressInterest(newInterest, self); |
| 167 | } |
| 168 | else { |
| 169 | m_hasError = true; |
| 170 | if (m_onTimeout) |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 171 | m_onTimeout(interest, "Reached the maximum number of timeout retries (" + std::to_string(m_maxTimeoutRetries) + |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 172 | ") while retrieving data for " + interest.getName().toUri()); |
| 173 | } |
| 174 | } |
| 175 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 176 | } // namespace ndn::chunks |