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