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-2024 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"
30 
31 #include <queue>
32 #include <set>
33 
34 namespace ndn {
35 
40 {
46  bool probeLatestVersion = true;
48  bool inOrder = false;
52  bool useConstantCwnd = false;
54  bool disableCwa = false;
56  bool resetCwndToInit = false;
58  bool ignoreCongMarks = false;
60  double initCwnd = 1.0;
62  double initSsthresh = std::numeric_limits<double>::max();
64  double aiStep = 1.0;
66  double mdCoef = 0.5;
70  size_t flowControlWindow = 25000;
71 
72  void
73  validate();
74 };
75 
126 class SegmentFetcher : noncopyable
127 {
128 public:
132  enum ErrorCode {
143  };
144 
146 
171  static shared_ptr<SegmentFetcher>
172  start(Face& face, const Interest& baseInterest, security::Validator& validator,
173  const Options& options = {});
174 
180  void
181  stop();
182 
183 private:
184  class PendingSegment;
185 
186  SegmentFetcher(Face& face, security::Validator& validator, const Options& options);
187 
188  static bool
189  shouldStop(const weak_ptr<SegmentFetcher>& weakSelf);
190 
191  void
192  fetchFirstSegment(const Interest& baseInterest, bool isRetransmission);
193 
194  void
195  fetchSegmentsInWindow(const Interest& origInterest);
196 
197  void
198  sendInterest(uint64_t segNum, const Interest& interest, bool isRetransmission);
199 
200  void
201  afterSegmentReceivedCb(const Interest& origInterest, const Data& data,
202  const weak_ptr<SegmentFetcher>& weakSelf);
203 
204  void
205  afterValidationSuccess(const Data& data, const Interest& origInterest,
206  std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt,
207  const weak_ptr<SegmentFetcher>& weakSelf);
208 
209  void
210  afterValidationFailure(const Data& data,
211  const security::ValidationError& error,
212  const weak_ptr<SegmentFetcher>& weakSelf);
213 
214  void
215  afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack,
216  const weak_ptr<SegmentFetcher>& weakSelf);
217 
218  void
219  afterTimeoutCb(const Interest& origInterest,
220  const weak_ptr<SegmentFetcher>& weakSelf);
221 
222  void
223  afterNackOrTimeout(const Interest& origInterest);
224 
225  void
226  finalizeFetch();
227 
228  void
229  windowIncrease();
230 
231  void
232  windowDecrease();
233 
234  void
235  signalError(uint32_t code, const std::string& msg);
236 
237  void
238  updateRetransmittedSegment(uint64_t segmentNum,
239  const PendingInterestHandle& pendingInterest,
240  scheduler::EventId timeoutEvent);
241 
242  void
243  cancelExcessInFlightSegments();
244 
245  bool
246  checkAllSegmentsReceived();
247 
249  getEstimatedRto();
250 
251 public:
257 
264 
269 
274 
279 
284 
290 
296 
297 private:
298  enum class SegmentState {
299  FirstInterest,
300  InRetxQueue,
301  Retransmitted,
302  };
303 
304  class PendingSegment
305  {
306  public:
307  SegmentState state;
310  scheduler::ScopedEventId timeoutEvent;
311  };
312 
314  static constexpr double MIN_SSTHRESH = 2.0;
315 
316  shared_ptr<SegmentFetcher> m_this;
317 
318  Options m_options;
319  Face& m_face;
320  Scheduler m_scheduler;
321  security::Validator& m_validator;
322  util::RttEstimator m_rttEstimator;
323 
324  time::steady_clock::time_point m_timeLastSegmentReceived;
325  std::queue<uint64_t> m_retxQueue;
326  Name m_versionedDataName;
327  uint64_t m_nextSegmentNum = 0;
328  double m_cwnd;
329  double m_ssthresh;
330  int64_t m_nSegmentsInFlight = 0;
331  int64_t m_nSegments = 0;
332  uint64_t m_highInterest = 0;
333  uint64_t m_highData = 0;
334  uint64_t m_recPoint = 0;
335  int64_t m_nReceived = 0;
336  int64_t m_nBytesReceived = 0;
337  uint64_t m_nextSegmentInOrder = 0;
338 
339  std::map<uint64_t, Buffer> m_segmentBuffer;
340  std::map<uint64_t, PendingSegment> m_pendingSegments;
341  std::set<uint64_t> m_receivedSegments;
342 };
343 
344 } // namespace ndn
345 
346 #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
Handle for a pending Interest.
Definition: face.hpp:494
Utility class to fetch a versioned and segmented object.
signal::Signal< SegmentFetcher, Data > afterSegmentValidated
Emitted whenever a received data segment has been successfully validated.
signal::Signal< SegmentFetcher > afterSegmentNacked
Emitted whenever an Interest for a data segment is nacked.
static shared_ptr< SegmentFetcher > start(Face &face, const Interest &baseInterest, security::Validator &validator, const Options &options={})
Initiates segment fetching.
void stop()
Stops fetching.
signal::Signal< SegmentFetcher, ConstBufferPtr > onInOrderData
Emitted after each data segment in segment order has been validated.
signal::Signal< SegmentFetcher, ConstBufferPtr > onComplete
Emitted upon successful retrieval of the complete object (all segments).
signal::Signal< SegmentFetcher > onInOrderComplete
Emitted on successful retrieval of all segments in 'in order' mode.
signal::Signal< SegmentFetcher, Data > afterSegmentReceived
Emitted whenever a data segment received.
ErrorCode
Error codes passed to onError.
@ INTEREST_TIMEOUT
Retrieval timed out because the maximum timeout between the successful receipt of segments was exceed...
@ FINALBLOCKID_NOT_SEGMENT
A received FinalBlockId did not contain a segment component.
@ NACK_ERROR
An unrecoverable Nack was received during retrieval.
@ SEGMENT_VALIDATION_FAIL
One of the retrieved segments failed user-provided validation.
@ DATA_HAS_NO_SEGMENT
One of the retrieved Data packets lacked a segment number in the last Name component (excl....
signal::Signal< SegmentFetcher, uint32_t, std::string > onError
Emitted when the retrieval could not be completed due to an error.
signal::Signal< SegmentFetcher > afterSegmentTimedOut
Emitted whenever an Interest for a data segment times out.
SegmentFetcherOptions Options
Represents a Network Nack.
Definition: nack.hpp:39
A handle for a scheduled event.
Definition: scheduler.hpp:62
Validation error code and optional detailed error message.
Interface for validating data and interest packets.
Definition: validator.hpp:61
Provides a lightweight signal / event system.
Definition: signal.hpp:51
::boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:232
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:49
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
@ Name
Definition: tlv.hpp:71
Definition: data.cpp:25
Options for SegmentFetcher.
size_t flowControlWindow
Maximum number of segments stored in the reorder buffer.
double initCwnd
Initial congestion window size.
bool resetCwndToInit
Reduce cwnd to initCwnd when a loss event occurs.
bool probeLatestVersion
Use the first Interest to probe the latest version of the object.
double mdCoef
Multiplicative decrease coefficient.
time::milliseconds maxTimeout
Maximum allowed time between successful receipt of segments.
bool disableCwa
Disable Conservative Window Adaptation.
bool inOrder
Set to true for 'in order' mode, false for 'block' mode.
bool useConstantInterestTimeout
If true, Interest timeout is kept fixed at maxTimeout.
double initSsthresh
Initial slow start threshold.
time::milliseconds interestLifetime
Lifetime of sent Interests (independent of Interest timeout)
double aiStep
Additive increase step (in segments)
bool useConstantCwnd
If true, window size is kept fixed at initCwnd.
util::RttEstimator::Options rttOptions
Options for the RTT estimator.
bool ignoreCongMarks
Disable window decrease after a congestion mark is received.