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