Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +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, |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [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 Wentao Shang |
| 24 | * @author Steve DiBenedetto |
| 25 | * @author Andrea Tosatto |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 26 | * @author Davide Pesavento |
| 27 | * @author Weiwei Liu |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 28 | * @author Chavoosh Ghasemi |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP |
| 32 | #define NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP |
| 33 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 34 | #include "options.hpp" |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 36 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 37 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 38 | /** |
| 39 | * @brief Service for retrieving Data via an Interest pipeline |
| 40 | * |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 41 | * Retrieves all segments of Data under a given prefix by maintaining a (variable or fixed-size) |
| 42 | * window of N Interests in flight. A user-specified callback function is used to notify |
| 43 | * the arrival of each segment of Data. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 44 | * |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 45 | * No guarantees are made as to the order in which segments are fetched or callbacks are invoked, |
| 46 | * i.e. out-of-order delivery is possible. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 47 | */ |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 48 | class PipelineInterests : noncopyable |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 49 | { |
| 50 | public: |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 51 | /** |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 52 | * @brief Constructor. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 53 | * |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 54 | * Configures the pipelining service without specifying the retrieval namespace. |
| 55 | * After construction, the method run() must be called in order to start the pipeline. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 56 | */ |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 57 | PipelineInterests(Face& face, const Options& opts); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 58 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 59 | virtual |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 60 | ~PipelineInterests(); |
| 61 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 62 | using DataCallback = std::function<void(const Data&)>; |
| 63 | using FailureCallback = std::function<void(const std::string& reason)>; |
| 64 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 65 | /** |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 66 | * @brief start fetching all the segments of the specified prefix |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 67 | * |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 68 | * @param versionedName the name of the segmented Data ending with a version number |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | * @param onData callback for every segment correctly received, must not be empty |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 70 | * @param onFailure callback if an error occurs, may be empty |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 71 | */ |
| 72 | void |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 73 | run(const Name& versionedName, DataCallback onData, FailureCallback onFailure); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * @brief stop all fetch operations |
| 77 | */ |
| 78 | void |
| 79 | cancel(); |
| 80 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 81 | protected: |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 82 | time::steady_clock::time_point |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 83 | getStartTime() const |
| 84 | { |
| 85 | return m_startTime; |
| 86 | } |
| 87 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 88 | bool |
| 89 | isStopping() const |
| 90 | { |
| 91 | return m_isStopping; |
| 92 | } |
| 93 | |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 94 | /** |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 95 | * @brief check if the transfer is complete |
| 96 | * @return true if all segments have been received, false otherwise |
| 97 | */ |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 98 | [[nodiscard]] bool |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 99 | allSegmentsReceived() const; |
| 100 | |
| 101 | /** |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 102 | * @return next segment number to retrieve |
| 103 | * @post m_nextSegmentNo == return-value + 1 |
| 104 | */ |
| 105 | uint64_t |
| 106 | getNextSegmentNo(); |
| 107 | |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 108 | /** |
| 109 | * @brief subclasses must call this method to notify successful retrieval of a segment |
| 110 | */ |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 111 | void |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 112 | onData(const Data& data); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * @brief subclasses can call this method to signal an unrecoverable failure |
| 116 | */ |
| 117 | void |
| 118 | onFailure(const std::string& reason); |
| 119 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 120 | void |
| 121 | printOptions() const; |
| 122 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 123 | /** |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 124 | * @brief print statistics about this fetching session |
| 125 | * |
| 126 | * Subclasses can override this method to print additional stats or change the summary format |
| 127 | */ |
| 128 | virtual void |
| 129 | printSummary() const; |
| 130 | |
| 131 | /** |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 132 | * @param throughput The throughput in bits/s |
| 133 | */ |
| 134 | static std::string |
| 135 | formatThroughput(double throughput); |
| 136 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 137 | private: |
| 138 | /** |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 139 | * @brief perform subclass-specific operations to fetch all the segments |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 140 | * |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 141 | * When overriding this function, at a minimum, the subclass should implement the retrieving |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 142 | * of all the segments. Subclass must guarantee that `onData` is called once for every |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 143 | * segment that is fetched successfully. |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 144 | * |
| 145 | * @note m_lastSegmentNo contains a valid value only if m_hasFinalBlockId is true. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 146 | */ |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 147 | virtual void |
| 148 | doRun() = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 149 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 150 | virtual void |
| 151 | doCancel() = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 152 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 153 | protected: |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 154 | const Options& m_options; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 155 | Face& m_face; |
| 156 | Name m_prefix; |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 157 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 158 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 159 | bool m_hasFinalBlockId = false; ///< true if the last segment number is known |
| 160 | uint64_t m_lastSegmentNo = 0; ///< valid only if m_hasFinalBlockId == true |
| 161 | int64_t m_nReceived = 0; ///< number of segments received |
| 162 | size_t m_receivedSize = 0; ///< size of received data in bytes |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 163 | |
| 164 | private: |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 165 | DataCallback m_onData; |
| 166 | FailureCallback m_onFailure; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 167 | uint64_t m_nextSegmentNo = 0; |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 168 | time::steady_clock::time_point m_startTime; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 169 | bool m_isStopping = false; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 170 | }; |
| 171 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 172 | template<typename Packet> |
| 173 | uint64_t |
| 174 | getSegmentFromPacket(const Packet& packet) |
| 175 | { |
| 176 | return packet.getName().at(-1).toSegment(); |
| 177 | } |
| 178 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 179 | } // namespace ndn::chunks |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 180 | |
| 181 | #endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP |