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 | /* |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_UTIL_SEGMENT_FETCHER_HPP |
| 23 | #define NDN_UTIL_SEGMENT_FETCHER_HPP |
| 24 | |
| 25 | #include "../common.hpp" |
| 26 | #include "../face.hpp" |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 27 | #include "../encoding/buffer-stream.hpp" |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 28 | #include "../security/v2/validator.hpp" |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 29 | #include "scheduler.hpp" |
| 30 | #include "signal.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 33 | namespace util { |
| 34 | |
| 35 | /** |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 36 | * @brief Utility class to fetch latest version of the segmented data |
| 37 | * |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 38 | * SegmentFetcher assumes that the data is named `/<prefix>/<version>/<segment>`, |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 39 | * where: |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 40 | * - `<prefix>` is the specified prefix, |
| 41 | * - `<version>` is an unknown version that needs to be discovered, and |
| 42 | * - `<segment>` is a segment number (number of segments is unknown and is controlled |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 43 | * by `FinalBlockId` field in at least the last Data packet |
| 44 | * |
| 45 | * The following logic is implemented in SegmentFetcher: |
| 46 | * |
| 47 | * 1. Express first interest to discover version: |
| 48 | * |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 49 | * >> Interest: `/<prefix>?ChildSelector=1&MustBeFresh=yes` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 50 | * |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 51 | * 2. Infer the latest version of Data: `<version> = Data.getName().get(-2)` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 52 | * |
| 53 | * 3. If segment number in the retrieved packet == 0, go to step 5. |
| 54 | * |
| 55 | * 4. Send Interest for segment 0: |
| 56 | * |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 57 | * >> Interest: `/<prefix>/<version>/<segment=0>` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 58 | * |
| 59 | * 5. Keep sending Interests for the next segment while the retrieved Data does not have |
| 60 | * FinalBlockId or FinalBlockId != Data.getName().get(-1). |
| 61 | * |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 62 | * >> Interest: `/<prefix>/<version>/<segment=(N+1))>` |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 63 | * |
| 64 | * 6. Fire onCompletion callback with memory block that combines content part from all |
| 65 | * segmented objects. |
| 66 | * |
| 67 | * If an error occurs during the fetching process, an error callback is fired |
| 68 | * with a proper error code. The following errors are possible: |
| 69 | * |
| 70 | * - `INTEREST_TIMEOUT`: if any of the Interests times out |
| 71 | * - `DATA_HAS_NO_SEGMENT`: if any of the retrieved Data packets don't have segment |
| 72 | * as a last component of the name (not counting implicit digest) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 73 | * - `SEGMENT_VALIDATION_FAIL`: if any retrieved segment fails user-provided validation |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 74 | * |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 75 | * In order to validate individual segments, a Validator instance needs to be specified. |
| 76 | * If the segment validation is successful, afterValidationSuccess callback is fired, otherwise |
| 77 | * afterValidationFailure callback. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 78 | * |
| 79 | * Examples: |
| 80 | * |
| 81 | * void |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 82 | * afterFetchComplete(const ConstBufferPtr& data) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 83 | * { |
| 84 | * ... |
| 85 | * } |
| 86 | * |
| 87 | * void |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 88 | * afterFetchError(uint32_t errorCode, const std::string& errorMsg) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 89 | * { |
| 90 | * ... |
| 91 | * } |
| 92 | * |
| 93 | * ... |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 94 | * SegmentFetcher::fetch(face, Interest("/data/prefix", 30_s), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 95 | * validator, |
| 96 | * bind(&afterFetchComplete, this, _1), |
| 97 | * bind(&afterFetchError, this, _1, _2)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 98 | * |
| 99 | */ |
| 100 | class SegmentFetcher : noncopyable |
| 101 | { |
| 102 | public: |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 103 | /** |
| 104 | * @brief Maximum number of times an interest will be reexpressed incase of NackCallback |
| 105 | */ |
| 106 | static const uint32_t MAX_INTEREST_REEXPRESS; |
| 107 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 108 | typedef function<void (ConstBufferPtr data)> CompleteCallback; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 109 | typedef function<void (uint32_t code, const std::string& msg)> ErrorCallback; |
| 110 | |
| 111 | /** |
| 112 | * @brief Error codes that can be passed to ErrorCallback |
| 113 | */ |
| 114 | enum ErrorCode { |
| 115 | INTEREST_TIMEOUT = 1, |
| 116 | DATA_HAS_NO_SEGMENT = 2, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 117 | SEGMENT_VALIDATION_FAIL = 3, |
| 118 | NACK_ERROR = 4 |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /** |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 122 | * @brief Initiates segment fetching |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 123 | * |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 124 | * @param face Reference to the Face that should be used to fetch data |
| 125 | * @param baseInterest An Interest for the initial segment of requested data. |
| 126 | * This interest may include a custom InterestLifetime and selectors that will |
| 127 | * propagate to all subsequent Interests. The only exception is that the |
| 128 | * initial Interest will be forced to include the "ChildSelector=1" and |
| 129 | * "MustBeFresh=true" selectors, which will be turned off in subsequent |
| 130 | * Interests. |
| 131 | * @param validator Reference to the Validator that should be used to validate data. Caller |
| 132 | * must ensure validator is valid until either onComplete or onError has been |
| 133 | * signaled. |
| 134 | * |
| 135 | * @return A shared_ptr to the constructed SegmentFetcher |
| 136 | * |
| 137 | * Transfer completion, failure, and progress are indicated via signals. |
| 138 | */ |
| 139 | static |
| 140 | shared_ptr<SegmentFetcher> |
| 141 | start(Face& face, |
| 142 | const Interest& baseInterest, |
| 143 | security::v2::Validator& validator); |
| 144 | |
| 145 | /** |
| 146 | * @brief Initiates segment fetching |
| 147 | * |
| 148 | * @deprecated Use @c start |
| 149 | * |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 150 | * @param face Reference to the Face that should be used to fetch data |
| 151 | * @param baseInterest An Interest for the initial segment of requested data. |
| 152 | * This interest may include custom InterestLifetime and selectors that |
| 153 | * will propagate to all subsequent Interests. The only exception is that |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 154 | * the initial Interest will be forced to include "ChildSelector=rightmost" and |
| 155 | * "MustBeFresh=true" selectors, which will be turned off in subsequent |
| 156 | * Interests. |
| 157 | * @param validator Reference to the Validator that should be used to validate data. Caller |
| 158 | * must ensure validator is valid until either completeCallback or errorCallback |
| 159 | * is invoked. |
| 160 | * |
| 161 | * @param completeCallback Callback to be fired when all segments are fetched |
| 162 | * @param errorCallback Callback to be fired when an error occurs (@see Errors) |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 163 | * @return A shared_ptr to the constructed SegmentFetcher |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 164 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 165 | [[deprecated("use SegmentFetcher::start instead")]] |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 166 | static |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 167 | shared_ptr<SegmentFetcher> |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 168 | fetch(Face& face, |
| 169 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 170 | security::v2::Validator& validator, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 171 | const CompleteCallback& completeCallback, |
| 172 | const ErrorCallback& errorCallback); |
| 173 | |
| 174 | /** |
| 175 | * @brief Initiate segment fetching |
| 176 | * |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 177 | * @deprecated Use @c start |
| 178 | * |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 179 | * @param face Reference to the Face that should be used to fetch data |
| 180 | * @param baseInterest An Interest for the initial segment of requested data. |
| 181 | * This interest may include custom InterestLifetime and selectors that |
| 182 | * will propagate to all subsequent Interests. The only exception is that |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 183 | * the initial Interest will be forced to include "ChildSelector=1" and |
| 184 | * "MustBeFresh=true" selectors, which will be turned off in subsequent |
| 185 | * Interests. |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 186 | * @param validator A shared_ptr to the Validator that should be used to validate data. |
| 187 | * |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 188 | * @param completeCallback Callback to be fired when all segments are fetched |
| 189 | * @param errorCallback Callback to be fired when an error occurs (@see Errors) |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 190 | * @return A shared_ptr to the constructed SegmentFetcher |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 191 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 192 | [[deprecated("use SegmentFetcher::start instead")]] |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 193 | static |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 194 | shared_ptr<SegmentFetcher> |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 195 | fetch(Face& face, |
| 196 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 197 | shared_ptr<security::v2::Validator> validator, |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 198 | const CompleteCallback& completeCallback, |
| 199 | const ErrorCallback& errorCallback); |
| 200 | |
| 201 | private: |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 202 | SegmentFetcher(Face& face, security::v2::Validator& validator); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 203 | |
| 204 | void |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 205 | fetchFirstSegment(const Interest& baseInterest, shared_ptr<SegmentFetcher> self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 206 | |
| 207 | void |
| 208 | fetchNextSegment(const Interest& origInterest, const Name& dataName, uint64_t segmentNo, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 209 | shared_ptr<SegmentFetcher> self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 210 | |
| 211 | void |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 212 | afterSegmentReceivedCb(const Interest& origInterest, |
| 213 | const Data& data, bool isSegmentZeroExpected, |
| 214 | shared_ptr<SegmentFetcher> self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 215 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 216 | afterValidationSuccess(const Data& data, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 217 | bool isSegmentZeroExpected, |
| 218 | const Interest& origInterest, |
| 219 | shared_ptr<SegmentFetcher> self); |
| 220 | |
| 221 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 222 | afterValidationFailure(const Data& data, const security::v2::ValidationError& error); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 223 | |
| 224 | void |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 225 | afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack, |
| 226 | uint32_t reExpressCount, shared_ptr<SegmentFetcher> self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 227 | |
| 228 | void |
| 229 | reExpressInterest(Interest interest, uint32_t reExpressCount, |
| 230 | shared_ptr<SegmentFetcher> self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 231 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 232 | public: |
| 233 | /** |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 234 | * @brief Emits upon successful retrieval of the complete data |
| 235 | */ |
| 236 | Signal<SegmentFetcher, ConstBufferPtr> onComplete; |
| 237 | |
| 238 | /** |
| 239 | * @brief Emits when the retrieval could not be completed due to an error |
| 240 | * |
| 241 | * Handler(s) are provided with an error code and a string error message. |
| 242 | */ |
| 243 | Signal<SegmentFetcher, uint32_t, std::string> onError; |
| 244 | |
| 245 | /** |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 246 | * @brief Emits whenever a data segment received |
| 247 | */ |
| 248 | Signal<SegmentFetcher, Data> afterSegmentReceived; |
| 249 | |
| 250 | /** |
| 251 | * @brief Emits whenever a received data segment has been successfully validated |
| 252 | */ |
| 253 | Signal<SegmentFetcher, Data> afterSegmentValidated; |
| 254 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 255 | private: |
| 256 | Face& m_face; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 257 | Scheduler m_scheduler; |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 258 | security::v2::Validator& m_validator; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 259 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame] | 260 | OBufferStream m_buffer; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 263 | } // namespace util |
| 264 | } // namespace ndn |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 265 | |
| 266 | #endif // NDN_UTIL_SEGMENT_FETCHER_HPP |