Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 2 | /* |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2018, Regents of the University of California, |
| 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 8 | * |
| 9 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 10 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 11 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 14 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 15 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 18 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 19 | * <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 22 | * |
| 23 | * @author Shuo Yang |
| 24 | * @author Weiwei Liu |
| 25 | * @author Chavoosh Ghasemi |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #ifndef NDN_UTIL_SEGMENT_FETCHER_HPP |
| 29 | #define NDN_UTIL_SEGMENT_FETCHER_HPP |
| 30 | |
| 31 | #include "../common.hpp" |
| 32 | #include "../face.hpp" |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 33 | #include "../security/v2/validator.hpp" |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 34 | #include "rtt-estimator.hpp" |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 35 | #include "scheduler.hpp" |
| 36 | #include "signal.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 37 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 38 | #include <queue> |
| 39 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 40 | namespace ndn { |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 41 | namespace util { |
| 42 | |
| 43 | /** |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 44 | * @brief Utility class to fetch the latest version of a segmented object. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 45 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 46 | * SegmentFetcher assumes that segments in the object are named `/<prefix>/<version>/<segment>`, |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 47 | * where: |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 48 | * - `<prefix>` is the specified prefix, |
| 49 | * - `<version>` is an unknown version that needs to be discovered, and |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 50 | * - `<segment>` is a segment number (The number of segments in the object is unknown until a Data |
| 51 | * packet containing the `FinalBlockId` field is receieved.) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 52 | * |
| 53 | * The following logic is implemented in SegmentFetcher: |
| 54 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 55 | * 1. Express an Interest to discover the latest version of the object: |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 56 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 57 | * >> Interest: `/<prefix>?ndn.CanBePrefix=true&ndn.MustBeFresh=true` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 58 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 59 | * 2. Infer the latest version of the object: `<version> = Data.getName().get(-2)` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 60 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 61 | * 3. Keep sending Interests for future segments until an error occurs or the number of segments |
| 62 | * indicated by the FinalBlockId in a received Data packet is reached. This retrieval will start |
| 63 | * at segment 1 if segment 0 was received in response to the Interest expressed in step 2; |
| 64 | * otherwise, retrieval will start at segment 0. By default, congestion control will be used to |
| 65 | * manage the Interest window size. Interests expressed in this step will follow this Name |
| 66 | * format: |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 67 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 68 | * >> Interest: `/<prefix>/<version>/<segment=(N)>` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 69 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 70 | * 4. Signal `onComplete` with a memory block that combines the content of all segments in the |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 71 | * object. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 72 | * |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 73 | * If an error occurs during the fetching process, `onError` is signaled with one of the error codes |
| 74 | * from `SegmentFetcher::ErrorCode`. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 75 | * |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 76 | * A Validator instance must be specified to validate individual segments. Every time a segment has |
| 77 | * been successfully validated, `afterValidationSuccess` will be signaled. If a segment fails |
| 78 | * validation, `afterValidationFailure` will be signaled. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 79 | * |
| 80 | * Examples: |
| 81 | * |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 82 | * @code |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 83 | * void |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 84 | * afterFetchComplete(ConstBufferPtr data) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 85 | * { |
| 86 | * ... |
| 87 | * } |
| 88 | * |
| 89 | * void |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 90 | * afterFetchError(uint32_t errorCode, const std::string& errorMsg) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 91 | * { |
| 92 | * ... |
| 93 | * } |
| 94 | * |
| 95 | * ... |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 96 | * auto fetcher = SegmentFetcher::start(face, Interest("/data/prefix"), validator); |
| 97 | * fetcher->onComplete.connect(bind(&afterFetchComplete, this, _1)); |
| 98 | * fetcher->onError.connect(bind(&afterFetchError, this, _1, _2)); |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 99 | * @endcode |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 100 | * |
| 101 | */ |
| 102 | class SegmentFetcher : noncopyable |
| 103 | { |
| 104 | public: |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 105 | // Deprecated: will be removed when deprecated fetch() API is removed - use start() instead |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 106 | typedef function<void (ConstBufferPtr data)> CompleteCallback; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 107 | typedef function<void (uint32_t code, const std::string& msg)> ErrorCallback; |
| 108 | |
| 109 | /** |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 110 | * @brief Error codes passed to `onError` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 111 | */ |
| 112 | enum ErrorCode { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 113 | /// retrieval timed out because the maximum timeout between the successful receipt of segments was exceeded |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 114 | INTEREST_TIMEOUT = 1, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 115 | /// one of the retrieved Data packets lacked a segment number in the last Name component (excl. implicit digest) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 116 | DATA_HAS_NO_SEGMENT = 2, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 117 | /// one of the retrieved segments failed user-provided validation |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 118 | SEGMENT_VALIDATION_FAIL = 3, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 119 | /// an unrecoverable Nack was received during retrieval |
| 120 | NACK_ERROR = 4, |
| 121 | /// a received FinalBlockId did not contain a segment component |
| 122 | FINALBLOCKID_NOT_SEGMENT = 5 |
| 123 | }; |
| 124 | |
| 125 | class Options |
| 126 | { |
| 127 | public: |
| 128 | Options() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | validate(); |
| 134 | |
| 135 | public: |
| 136 | bool useConstantCwnd = false; ///< if true, window size is kept at `initCwnd` |
| 137 | bool useConstantInterestTimeout = false; ///< if true, Interest timeout is kept at `maxTimeout` |
| 138 | time::milliseconds maxTimeout = 60_s; ///< maximum allowed time between successful receipt of segments |
| 139 | time::milliseconds interestLifetime = 4_s; ///< lifetime of sent Interests - independent of Interest timeout |
| 140 | double initCwnd = 1.0; ///< initial congestion window size |
| 141 | double initSsthresh = std::numeric_limits<double>::max(); ///< initial slow start threshold |
| 142 | double aiStep = 1.0; ///< additive increase step (in segments) |
| 143 | double mdCoef = 0.5; ///< multiplicative decrease coefficient |
| 144 | bool disableCwa = false; ///< disable Conservative Window Adaptation |
| 145 | bool resetCwndToInit = false; ///< reduce cwnd to initCwnd when loss event occurs |
| 146 | bool ignoreCongMarks = false; ///< disable window decrease after congestion mark received |
| 147 | RttEstimator::Options rttOptions; ///< options for RTT estimator |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /** |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 151 | * @brief Initiates segment fetching |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 152 | * |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 153 | * @param face Reference to the Face that should be used to fetch data |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 154 | * @param baseInterest Interest for the initial segment of requested data. |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 155 | * This interest may include a custom InterestLifetime and parameters that |
| 156 | * will propagate to all subsequent Interests. The only exception is that the |
| 157 | * initial Interest will be forced to include the "CanBePrefix=true" and |
| 158 | * "MustBeFresh=true" parameters, which will not be included in subsequent |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 159 | * Interests. |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 160 | * @param validator Reference to the Validator the fetcher will use to validate data. |
| 161 | * The caller must ensure the validator is valid until either `onComplete` or |
| 162 | * `onError` has been signaled. |
| 163 | * @param options Options controlling the transfer |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 164 | * |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 165 | * @return A shared_ptr to the constructed SegmentFetcher. |
| 166 | * This shared_ptr is kept internally for the lifetime of the transfer. |
| 167 | * Therefore, it does not need to be saved and is provided here so that the |
| 168 | * SegmentFetcher's signals can be connected to. |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 169 | * |
| 170 | * Transfer completion, failure, and progress are indicated via signals. |
| 171 | */ |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 172 | static shared_ptr<SegmentFetcher> |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 173 | start(Face& face, |
| 174 | const Interest& baseInterest, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 175 | security::v2::Validator& validator, |
| 176 | const Options& options = Options()); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * @brief Initiates segment fetching |
| 180 | * |
| 181 | * @deprecated Use @c start |
| 182 | * |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 183 | * @param face Reference to the Face that should be used to fetch data |
| 184 | * @param baseInterest An Interest for the initial segment of requested data. |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 185 | * This interest may include a custom InterestLifetime and parameters that |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 186 | * will propagate to all subsequent Interests. The only exception is that |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 187 | * the initial Interest will be forced to include the parameters |
| 188 | * "CanBePrefix=true" and "MustBeFresh=true", which will be turned off in |
| 189 | * subsequent Interests. |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 190 | * @param validator Reference to the Validator that should be used to validate data. Caller |
| 191 | * must ensure validator is valid until either completeCallback or |
| 192 | * errorCallback is invoked. |
| 193 | * @param completeCallback Callback to be fired when all segments are fetched |
| 194 | * @param errorCallback Callback to be fired when an error occurs (@see Errors) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 195 | * |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 196 | * @return A shared_ptr to the constructed SegmentFetcher. |
| 197 | * This shared_ptr is kept internally for the lifetime of the transfer. |
| 198 | * Therefore, it does not need to be saved and is provided here so that |
| 199 | * the SegmentFetcher's signals can be connected to. |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 200 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 201 | [[deprecated("use SegmentFetcher::start instead")]] |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 202 | static shared_ptr<SegmentFetcher> |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 203 | fetch(Face& face, |
| 204 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 205 | security::v2::Validator& validator, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 206 | const CompleteCallback& completeCallback, |
| 207 | const ErrorCallback& errorCallback); |
| 208 | |
| 209 | /** |
| 210 | * @brief Initiate segment fetching |
| 211 | * |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 212 | * @deprecated Use @c start |
| 213 | * |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 214 | * @param face Reference to the Face that should be used to fetch data |
| 215 | * @param baseInterest An Interest for the initial segment of requested data. |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 216 | * This interest may include a custom InterestLifetime and parameters that |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 217 | * will propagate to all subsequent Interests. The only exception is that |
Eric Newberry | 2b765f8 | 2018-06-25 14:51:13 -0700 | [diff] [blame^] | 218 | * the initial Interest will be forced to include the parameters |
| 219 | * "CanBePrefix=true" and "MustBeFresh=true", which will both be set to |
| 220 | * false in subsequent Interests. |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 221 | * @param validator A shared_ptr to the Validator that should be used to validate data. |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 222 | * |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 223 | * @param completeCallback Callback to be fired when all segments are fetched |
| 224 | * @param errorCallback Callback to be fired when an error occurs (@see Errors) |
| 225 | * |
| 226 | * @return A shared_ptr to the constructed SegmentFetcher. |
| 227 | * This shared_ptr is kept internally for the lifetime of the transfer. |
| 228 | * Therefore, it does not need to be saved and is provided here so that |
| 229 | * the SegmentFetcher's signals can be connected to. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 230 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 231 | [[deprecated("use SegmentFetcher::start instead")]] |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 232 | static shared_ptr<SegmentFetcher> |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 233 | fetch(Face& face, |
| 234 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 235 | shared_ptr<security::v2::Validator> validator, |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 236 | const CompleteCallback& completeCallback, |
| 237 | const ErrorCallback& errorCallback); |
| 238 | |
| 239 | private: |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 240 | class PendingSegment; |
| 241 | |
| 242 | SegmentFetcher(Face& face, security::v2::Validator& validator, const Options& options); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 243 | |
| 244 | void |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 245 | fetchFirstSegment(const Interest& baseInterest, |
| 246 | bool isRetransmission, |
| 247 | shared_ptr<SegmentFetcher> self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 248 | |
| 249 | void |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 250 | fetchSegmentsInWindow(const Interest& origInterest, shared_ptr<SegmentFetcher> self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 251 | |
| 252 | void |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 253 | afterSegmentReceivedCb(const Interest& origInterest, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 254 | const Data& data, |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 255 | shared_ptr<SegmentFetcher> self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 256 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 257 | afterValidationSuccess(const Data& data, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 258 | const Interest& origInterest, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 259 | std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 260 | shared_ptr<SegmentFetcher> self); |
| 261 | |
| 262 | void |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 263 | afterValidationFailure(const Data& data, |
| 264 | const security::v2::ValidationError& error, |
| 265 | shared_ptr<SegmentFetcher> self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 266 | |
| 267 | void |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 268 | afterNackReceivedCb(const Interest& origInterest, |
| 269 | const lp::Nack& nack, |
| 270 | shared_ptr<SegmentFetcher> self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 271 | |
| 272 | void |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 273 | afterTimeoutCb(const Interest& origInterest, |
| 274 | shared_ptr<SegmentFetcher> self); |
| 275 | |
| 276 | void |
| 277 | afterNackOrTimeout(const Interest& origInterest, |
| 278 | shared_ptr<SegmentFetcher> self); |
| 279 | |
| 280 | void |
| 281 | finalizeFetch(shared_ptr<SegmentFetcher> self); |
| 282 | |
| 283 | void |
| 284 | windowIncrease(); |
| 285 | |
| 286 | void |
| 287 | windowDecrease(); |
| 288 | |
| 289 | void |
| 290 | signalError(uint32_t code, const std::string& msg); |
| 291 | |
| 292 | void |
| 293 | updateRetransmittedSegment(uint64_t segmentNum, |
| 294 | const PendingInterestId* pendingInterest, |
| 295 | scheduler::EventId timeoutEvent); |
| 296 | |
| 297 | void |
| 298 | cancelExcessInFlightSegments(); |
| 299 | |
| 300 | bool |
| 301 | checkAllSegmentsReceived(); |
| 302 | |
| 303 | time::milliseconds |
| 304 | getEstimatedRto(); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 305 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 306 | public: |
| 307 | /** |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 308 | * @brief Emits upon successful retrieval of the complete data |
| 309 | */ |
| 310 | Signal<SegmentFetcher, ConstBufferPtr> onComplete; |
| 311 | |
| 312 | /** |
| 313 | * @brief Emits when the retrieval could not be completed due to an error |
| 314 | * |
| 315 | * Handler(s) are provided with an error code and a string error message. |
| 316 | */ |
| 317 | Signal<SegmentFetcher, uint32_t, std::string> onError; |
| 318 | |
| 319 | /** |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 320 | * @brief Emits whenever a data segment received |
| 321 | */ |
| 322 | Signal<SegmentFetcher, Data> afterSegmentReceived; |
| 323 | |
| 324 | /** |
| 325 | * @brief Emits whenever a received data segment has been successfully validated |
| 326 | */ |
| 327 | Signal<SegmentFetcher, Data> afterSegmentValidated; |
| 328 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 329 | /** |
| 330 | * @brief Emits whenever an Interest for a data segment is nacked |
| 331 | */ |
| 332 | Signal<SegmentFetcher> afterSegmentNacked; |
| 333 | |
| 334 | /** |
| 335 | * @brief Emits whenever an Interest for a data segment times out |
| 336 | */ |
| 337 | Signal<SegmentFetcher> afterSegmentTimedOut; |
| 338 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 339 | private: |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 340 | enum class SegmentState { |
| 341 | FirstInterest, ///< the first Interest for this segment has been sent |
| 342 | InRetxQueue, ///< the segment is awaiting Interest retransmission |
| 343 | Retransmitted, ///< one or more retransmitted Interests have been sent for this segment |
| 344 | }; |
| 345 | |
| 346 | class PendingSegment |
| 347 | { |
| 348 | public: |
| 349 | SegmentState state; |
| 350 | time::steady_clock::TimePoint sendTime; |
| 351 | const PendingInterestId* id; |
| 352 | scheduler::EventId timeoutEvent; |
| 353 | }; |
| 354 | |
| 355 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 356 | static constexpr double MIN_SSTHRESH = 2.0; |
| 357 | |
| 358 | Options m_options; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 359 | Face& m_face; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 360 | Scheduler m_scheduler; |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 361 | security::v2::Validator& m_validator; |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 362 | RttEstimator m_rttEstimator; |
| 363 | time::milliseconds m_timeout; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 364 | |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 365 | time::steady_clock::TimePoint m_timeLastSegmentReceived; |
| 366 | std::queue<uint64_t> m_retxQueue; |
| 367 | Name m_versionedDataName; |
| 368 | uint64_t m_nextSegmentNum; |
| 369 | double m_cwnd; |
| 370 | double m_ssthresh; |
| 371 | int64_t m_nSegmentsInFlight; |
| 372 | int64_t m_nSegments; |
| 373 | uint64_t m_highInterest; |
| 374 | uint64_t m_highData; |
| 375 | uint64_t m_recPoint; |
| 376 | int64_t m_nReceived; |
| 377 | int64_t m_nBytesReceived; |
| 378 | |
| 379 | std::map<uint64_t, Buffer> m_receivedSegments; |
| 380 | std::map<uint64_t, PendingSegment> m_pendingSegments; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 383 | } // namespace util |
| 384 | } // namespace ndn |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 385 | |
| 386 | #endif // NDN_UTIL_SEGMENT_FETCHER_HPP |