Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, Regents of the University of California, |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [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 | #ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP |
| 28 | #define NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP |
| 29 | |
| 30 | #include "core/common.hpp" |
| 31 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 32 | #include <ndn-cxx/face.hpp> |
| 33 | #include <ndn-cxx/util/scheduler.hpp> |
| 34 | |
| 35 | #include <functional> |
| 36 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 37 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 40 | * @brief Fetch data for a given interest and handle timeout or nack error with retries. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 41 | * |
| 42 | * To instantiate a DataFetcher you need to use the static method fetch, this will also express the |
| 43 | * interest. After a timeout or nack is received, the same interest with a different nonce will be |
| 44 | * requested for a maximum number of time specified by the class user. There are separate retry |
| 45 | * counters for timeouts and nacks. |
| 46 | * |
| 47 | * A specified callback is called after the data matching the expressed interest is received. A |
| 48 | * different callback is called in case one of the retries counter reach the maximum. This callback |
| 49 | * can be different for timeout and nack. The data callback must be defined but the others callback |
| 50 | * are optional. |
| 51 | * |
| 52 | */ |
| 53 | class DataFetcher |
| 54 | { |
| 55 | public: |
| 56 | /** |
| 57 | * @brief means that there is no maximum number of retries, |
| 58 | * i.e. fetching must be retried indefinitely |
| 59 | */ |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 60 | static constexpr int MAX_RETRIES_INFINITE = -1; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * @brief ceiling value for backoff time used in congestion handling |
| 64 | */ |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 65 | static constexpr time::milliseconds MAX_CONGESTION_BACKOFF_TIME = 10_s; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 66 | |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 67 | using FailureCallback = std::function<void(const Interest& interest, const std::string& reason)>; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * @brief instantiate a DataFetcher object and start fetching data |
| 71 | * |
| 72 | * @param onData callback for segment correctly received, must not be empty |
| 73 | */ |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 74 | static std::shared_ptr<DataFetcher> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 75 | fetch(Face& face, const Interest& interest, int maxNackRetries, int maxTimeoutRetries, |
| 76 | DataCallback onData, FailureCallback onTimeout, FailureCallback onNack, |
| 77 | bool isVerbose); |
| 78 | |
| 79 | /** |
| 80 | * @brief stop data fetching without error and calling any callback |
| 81 | */ |
| 82 | void |
| 83 | cancel(); |
| 84 | |
| 85 | bool |
| 86 | isRunning() const |
| 87 | { |
| 88 | return !m_isStopped && !m_hasError; |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | hasError() const |
| 93 | { |
| 94 | return m_hasError; |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | DataFetcher(Face& face, int maxNackRetries, int maxTimeoutRetries, |
| 99 | DataCallback onData, FailureCallback onNack, FailureCallback onTimeout, |
| 100 | bool isVerbose); |
| 101 | |
| 102 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 103 | expressInterest(const Interest& interest, const std::shared_ptr<DataFetcher>& self); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 104 | |
| 105 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 106 | handleData(const Interest& interest, const Data& data, const std::shared_ptr<DataFetcher>& self); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 107 | |
| 108 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 109 | handleNack(const Interest& interest, const lp::Nack& nack, const std::shared_ptr<DataFetcher>& self); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 110 | |
| 111 | void |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 112 | handleTimeout(const Interest& interest, const std::shared_ptr<DataFetcher>& self); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | Face& m_face; |
| 116 | Scheduler m_scheduler; |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 117 | PendingInterestHandle m_pendingInterest; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 118 | DataCallback m_onData; |
| 119 | FailureCallback m_onNack; |
| 120 | FailureCallback m_onTimeout; |
| 121 | |
| 122 | int m_maxNackRetries; |
| 123 | int m_maxTimeoutRetries; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 124 | int m_nNacks = 0; |
| 125 | int m_nTimeouts = 0; |
| 126 | uint32_t m_nCongestionRetries = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 127 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 128 | bool m_isVerbose = false; |
| 129 | bool m_isStopped = false; |
| 130 | bool m_hasError = false; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 133 | } // namespace ndn::chunks |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 134 | |
| 135 | #endif // NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP |