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 | 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 |
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 | |
| 34 | #include "core/common.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 | typedef function<void(const std::string& reason)> FailureCallback; |
| 53 | |
| 54 | public: |
| 55 | /** |
| 56 | * @brief create a PipelineInterests service |
| 57 | * |
| 58 | * Configures the pipelining service without specifying the retrieval namespace. After this |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 59 | * configuration the method run must be called to start the Pipeline. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 60 | */ |
| 61 | explicit |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 62 | PipelineInterests(Face& face); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 63 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 64 | virtual |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 65 | ~PipelineInterests(); |
| 66 | |
| 67 | /** |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 68 | * @brief start fetching all the segments of the specified prefix |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | * |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 70 | * @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] | 71 | * @param onData callback for every segment correctly received, must not be empty |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 72 | * @param onFailure callback if an error occurs, may be empty |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 73 | */ |
| 74 | void |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 75 | run(const Data& data, DataCallback onData, FailureCallback onFailure); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * @brief stop all fetch operations |
| 79 | */ |
| 80 | void |
| 81 | cancel(); |
| 82 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 83 | protected: |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 84 | time::steady_clock::TimePoint |
| 85 | getStartTime() const |
| 86 | { |
| 87 | return m_startTime; |
| 88 | } |
| 89 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 90 | bool |
| 91 | isStopping() const |
| 92 | { |
| 93 | return m_isStopping; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | onData(const Interest& interest, const Data& data) const |
| 98 | { |
| 99 | m_onData(interest, data); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @brief subclasses can call this method to signal an unrecoverable failure |
| 104 | */ |
| 105 | void |
| 106 | onFailure(const std::string& reason); |
| 107 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame^] | 108 | /** |
| 109 | * @param throughput The throughput in bits/s |
| 110 | */ |
| 111 | static std::string |
| 112 | formatThroughput(double throughput); |
| 113 | |
| 114 | /** |
| 115 | * @brief print statistics about quality of packet delivery |
| 116 | * |
| 117 | * This method should be overriden by each pipeline (e.g. AIMD) |
| 118 | */ |
| 119 | virtual void |
| 120 | printSummary() const; |
| 121 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 122 | private: |
| 123 | /** |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 124 | * @brief perform subclass-specific operations to fetch all the segments |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 125 | * |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 126 | * When overriding this function, at a minimum, the subclass should implement the retrieving |
| 127 | * of all the segments. Segment m_excludedSegmentNo can be skipped. Subclass must guarantee |
| 128 | * that onData is called at least once for every segment that is fetched successfully. |
| 129 | * |
| 130 | * @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] | 131 | */ |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 132 | virtual void |
| 133 | doRun() = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 134 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 135 | virtual void |
| 136 | doCancel() = 0; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 137 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 138 | protected: |
| 139 | Face& m_face; |
| 140 | Name m_prefix; |
| 141 | uint64_t m_lastSegmentNo; |
| 142 | uint64_t m_excludedSegmentNo; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 143 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame^] | 144 | size_t m_receivedSize; ///< size of received data in bytes |
| 145 | int64_t m_nReceived; ///< number of segments received |
| 146 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 147 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 148 | bool m_hasFinalBlockId; ///< true if the last segment number is known |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 149 | |
| 150 | private: |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 151 | DataCallback m_onData; |
| 152 | FailureCallback m_onFailure; |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 153 | time::steady_clock::TimePoint m_startTime; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 154 | bool m_isStopping; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 157 | template<typename Packet> |
| 158 | uint64_t |
| 159 | getSegmentFromPacket(const Packet& packet) |
| 160 | { |
| 161 | return packet.getName().at(-1).toSegment(); |
| 162 | } |
| 163 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 164 | } // namespace chunks |
| 165 | } // namespace ndn |
| 166 | |
| 167 | #endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP |