segment-fetcher.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 Regents of the University of California.
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_CXX_UTIL_SEGMENT_FETCHER_HPP
23 #define NDN_CXX_UTIL_SEGMENT_FETCHER_HPP
24 
25 #include "ndn-cxx/face.hpp"
29 #include "ndn-cxx/util/signal.hpp"
30 
31 #include <queue>
32 #include <set>
33 
34 namespace ndn {
35 namespace util {
36 
80 class SegmentFetcher : noncopyable
81 {
82 public:
86  enum ErrorCode {
97  };
98 
99  class Options
100  {
101  public:
103  {
104  }
105 
106  void
107  validate();
108 
109  public:
112  bool inOrder = false;
114  bool useConstantCwnd = false;
115  bool disableCwa = false;
116  bool resetCwndToInit = false;
117  bool ignoreCongMarks = false;
118  double initCwnd = 1.0;
119  double initSsthresh = std::numeric_limits<double>::max();
120  double aiStep = 1.0;
121  double mdCoef = 0.5;
123  size_t flowControlWindow = 25000;
124  };
125 
148  static shared_ptr<SegmentFetcher>
149  start(Face& face,
150  const Interest& baseInterest,
151  security::Validator& validator,
152  const Options& options = Options());
153 
159  void
160  stop();
161 
162 private:
163  class PendingSegment;
164 
165  SegmentFetcher(Face& face, security::Validator& validator, const Options& options);
166 
167  static bool
168  shouldStop(const weak_ptr<SegmentFetcher>& weakSelf);
169 
170  void
171  fetchFirstSegment(const Interest& baseInterest, bool isRetransmission);
172 
173  void
174  fetchSegmentsInWindow(const Interest& origInterest);
175 
176  void
177  sendInterest(uint64_t segNum, const Interest& interest, bool isRetransmission);
178 
179  void
180  afterSegmentReceivedCb(const Interest& origInterest, const Data& data,
181  const weak_ptr<SegmentFetcher>& weakSelf);
182 
183  void
184  afterValidationSuccess(const Data& data, const Interest& origInterest,
185  std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt,
186  const weak_ptr<SegmentFetcher>& weakSelf);
187 
188  void
189  afterValidationFailure(const Data& data,
190  const security::ValidationError& error,
191  const weak_ptr<SegmentFetcher>& weakSelf);
192 
193  void
194  afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack,
195  const weak_ptr<SegmentFetcher>& weakSelf);
196 
197  void
198  afterTimeoutCb(const Interest& origInterest,
199  const weak_ptr<SegmentFetcher>& weakSelf);
200 
201  void
202  afterNackOrTimeout(const Interest& origInterest);
203 
204  void
205  finalizeFetch();
206 
207  void
208  windowIncrease();
209 
210  void
211  windowDecrease();
212 
213  void
214  signalError(uint32_t code, const std::string& msg);
215 
216  void
217  updateRetransmittedSegment(uint64_t segmentNum,
218  const PendingInterestHandle& pendingInterest,
219  scheduler::EventId timeoutEvent);
220 
221  void
222  cancelExcessInFlightSegments();
223 
224  bool
225  checkAllSegmentsReceived();
226 
228  getEstimatedRto();
229 
230 public:
236 
243 
248 
253 
258 
263 
269 
275 
276 private:
277  enum class SegmentState {
278  FirstInterest,
279  InRetxQueue,
280  Retransmitted,
281  };
282 
283  class PendingSegment
284  {
285  public:
286  SegmentState state;
289  scheduler::ScopedEventId timeoutEvent;
290  };
291 
293  static constexpr double MIN_SSTHRESH = 2.0;
294 
295  shared_ptr<SegmentFetcher> m_this;
296 
297  Options m_options;
298  Face& m_face;
299  Scheduler m_scheduler;
300  security::Validator& m_validator;
301  RttEstimator m_rttEstimator;
302 
303  time::steady_clock::TimePoint m_timeLastSegmentReceived;
304  std::queue<uint64_t> m_retxQueue;
305  Name m_versionedDataName;
306  uint64_t m_nextSegmentNum = 0;
307  double m_cwnd;
308  double m_ssthresh;
309  int64_t m_nSegmentsInFlight = 0;
310  int64_t m_nSegments = 0;
311  uint64_t m_highInterest = 0;
312  uint64_t m_highData = 0;
313  uint64_t m_recPoint = 0;
314  int64_t m_nReceived = 0;
315  int64_t m_nBytesReceived = 0;
316  uint64_t m_nextSegmentInOrder = 0;
317 
318  std::map<uint64_t, Buffer> m_segmentBuffer;
319  std::map<uint64_t, PendingSegment> m_pendingSegments;
320  std::set<uint64_t> m_receivedSegments;
321 };
322 
323 } // namespace util
324 } // namespace ndn
325 
326 #endif // NDN_CXX_UTIL_SEGMENT_FETCHER_HPP
Represents a Data packet.
Definition: data.hpp:39
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
Represents an Interest packet.
Definition: interest.hpp:50
Represents an absolute name.
Definition: name.hpp:44
Handle for a pending Interest.
Definition: face.hpp:491
Represents a Network Nack.
Definition: nack.hpp:40
A handle for a scheduled event.
Definition: scheduler.hpp:61
Generic time-based scheduler.
Definition: scheduler.hpp:135
Validation error code and optional detailed error message.
Interface for validating data and interest packets.
Definition: validator.hpp:62
time_point TimePoint
Definition: time.hpp:233
bool useConstantInterestTimeout
if true, Interest timeout is kept at maxTimeout
double mdCoef
multiplicative decrease coefficient
double initSsthresh
initial slow start threshold
RttEstimator::Options rttOptions
options for RTT estimator
double aiStep
additive increase step (in segments)
double initCwnd
initial congestion window size
bool disableCwa
disable Conservative Window Adaptation
bool resetCwndToInit
reduce cwnd to initCwnd when loss event occurs
time::milliseconds interestLifetime
lifetime of sent Interests - independent of Interest timeout
bool useConstantCwnd
if true, window size is kept at initCwnd
bool inOrder
true for 'in order' mode, false for 'block' mode
size_t flowControlWindow
maximum number of segments stored in the reorder buffer
bool ignoreCongMarks
disable window decrease after congestion mark received
time::milliseconds maxTimeout
maximum allowed time between successful receipt of segments
Utility class to fetch the latest version of a segmented object.
Signal< SegmentFetcher > onInOrderComplete
Emitted on successful retrieval of all segments in 'in order' mode.
static shared_ptr< SegmentFetcher > start(Face &face, const Interest &baseInterest, security::Validator &validator, const Options &options=Options())
Initiates segment fetching.
Signal< SegmentFetcher, ConstBufferPtr > onComplete
Emitted upon successful retrieval of the complete object (all segments).
Signal< SegmentFetcher, ConstBufferPtr > onInOrderData
Emitted after each data segment in segment order has been validated.
Signal< SegmentFetcher, Data > afterSegmentValidated
Emitted whenever a received data segment has been successfully validated.
ErrorCode
Error codes passed to onError.
@ INTEREST_TIMEOUT
Retrieval timed out because the maximum timeout between the successful receipt of segments was exceed...
@ DATA_HAS_NO_SEGMENT
One of the retrieved Data packets lacked a segment number in the last Name component (excl....
@ FINALBLOCKID_NOT_SEGMENT
A received FinalBlockId did not contain a segment component.
@ SEGMENT_VALIDATION_FAIL
One of the retrieved segments failed user-provided validation.
@ NACK_ERROR
An unrecoverable Nack was received during retrieval.
Signal< SegmentFetcher > afterSegmentNacked
Emitted whenever an Interest for a data segment is nacked.
void stop()
Stops fetching.
Signal< SegmentFetcher, uint32_t, std::string > onError
Emitted when the retrieval could not be completed due to an error.
Signal< SegmentFetcher, Data > afterSegmentReceived
Emitted whenever a data segment received.
Signal< SegmentFetcher > afterSegmentTimedOut
Emitted whenever an Interest for a data segment times out.
Provides a lightweight signal / event system.
Definition: signal.hpp:53
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
Definition: data.cpp:25