Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2016-2017, Regents of the University of California, |
| 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 |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP |
| 31 | #define NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP |
| 32 | |
| 33 | #include "core/common.hpp" |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace chunks { |
| 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 | */ |
| 48 | class PipelineInterests |
| 49 | { |
| 50 | public: |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 51 | typedef function<void(const std::string& reason)> FailureCallback; |
| 52 | |
| 53 | public: |
| 54 | /** |
| 55 | * @brief create a PipelineInterests service |
| 56 | * |
| 57 | * Configures the pipelining service without specifying the retrieval namespace. After this |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 58 | * configuration the method run must be called to start the Pipeline. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 59 | */ |
| 60 | explicit |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 61 | PipelineInterests(Face& face); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 62 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 63 | virtual |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 64 | ~PipelineInterests(); |
| 65 | |
| 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 | * |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 69 | * @param data a segment of the segmented Data to fetch; the Data name must end with a segment 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 |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 74 | run(const Data& data, 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 | |
| 95 | void |
| 96 | onData(const Interest& interest, const Data& data) const |
| 97 | { |
| 98 | m_onData(interest, data); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @brief subclasses can call this method to signal an unrecoverable failure |
| 103 | */ |
| 104 | void |
| 105 | onFailure(const std::string& reason); |
| 106 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 107 | private: |
| 108 | /** |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 109 | * @brief perform subclass-specific operations to fetch all the segments |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 110 | * |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 111 | * When overriding this function, at a minimum, the subclass should implement the retrieving |
| 112 | * of all the segments. Segment m_excludedSegmentNo can be skipped. Subclass must guarantee |
| 113 | * that onData is called at least once for every segment that is fetched successfully. |
| 114 | * |
| 115 | * @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] | 116 | */ |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 117 | virtual void |
| 118 | doRun() = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 119 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 120 | virtual void |
| 121 | doCancel() = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 122 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 123 | protected: |
| 124 | Face& m_face; |
| 125 | Name m_prefix; |
| 126 | uint64_t m_lastSegmentNo; |
| 127 | uint64_t m_excludedSegmentNo; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 128 | |
| 129 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 130 | bool m_hasFinalBlockId; ///< true if the last segment number is known |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 131 | |
| 132 | private: |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 133 | DataCallback m_onData; |
| 134 | FailureCallback m_onFailure; |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame^] | 135 | time::steady_clock::TimePoint m_startTime; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 136 | bool m_isStopping; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame^] | 139 | template<typename Packet> |
| 140 | uint64_t |
| 141 | getSegmentFromPacket(const Packet& packet) |
| 142 | { |
| 143 | return packet.getName().at(-1).toSegment(); |
| 144 | } |
| 145 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 146 | } // namespace chunks |
| 147 | } // namespace ndn |
| 148 | |
| 149 | #endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP |