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-2020 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_UTIL_SEGMENT_FETCHER_HPP
23 #define NDN_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 
93 class SegmentFetcher : noncopyable
94 {
95 public:
99  enum ErrorCode {
110  };
111 
112  class Options
113  {
114  public:
116  {
117  }
118 
119  void
120  validate();
121 
122  public:
123  time::milliseconds interestLifetime = 4_s;
124  time::milliseconds maxTimeout = 60_s;
125  bool inOrder = false;
127  bool useConstantCwnd = false;
128  bool disableCwa = false;
129  bool resetCwndToInit = false;
130  bool ignoreCongMarks = false;
131  double initCwnd = 1.0;
132  double initSsthresh = std::numeric_limits<double>::max();
133  double aiStep = 1.0;
134  double mdCoef = 0.5;
136  size_t flowControlWindow = 25000;
137  };
138 
161  static shared_ptr<SegmentFetcher>
162  start(Face& face,
163  const Interest& baseInterest,
164  security::v2::Validator& validator,
165  const Options& options = Options());
166 
172  void
173  stop();
174 
175 private:
176  class PendingSegment;
177 
178  SegmentFetcher(Face& face, security::v2::Validator& validator, const Options& options);
179 
180  static bool
181  shouldStop(const weak_ptr<SegmentFetcher>& weakSelf);
182 
183  void
184  fetchFirstSegment(const Interest& baseInterest, bool isRetransmission);
185 
186  void
187  fetchSegmentsInWindow(const Interest& origInterest);
188 
189  void
190  sendInterest(uint64_t segNum, const Interest& interest, bool isRetransmission);
191 
192  void
193  afterSegmentReceivedCb(const Interest& origInterest, const Data& data,
194  const weak_ptr<SegmentFetcher>& weakSelf);
195 
196  void
197  afterValidationSuccess(const Data& data, const Interest& origInterest,
198  std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt,
199  const weak_ptr<SegmentFetcher>& weakSelf);
200 
201  void
202  afterValidationFailure(const Data& data,
203  const security::v2::ValidationError& error,
204  const weak_ptr<SegmentFetcher>& weakSelf);
205 
206  void
207  afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack,
208  const weak_ptr<SegmentFetcher>& weakSelf);
209 
210  void
211  afterTimeoutCb(const Interest& origInterest,
212  const weak_ptr<SegmentFetcher>& weakSelf);
213 
214  void
215  afterNackOrTimeout(const Interest& origInterest);
216 
217  void
218  finalizeFetch();
219 
220  void
221  windowIncrease();
222 
223  void
224  windowDecrease();
225 
226  void
227  signalError(uint32_t code, const std::string& msg);
228 
229  void
230  updateRetransmittedSegment(uint64_t segmentNum,
231  const PendingInterestHandle& pendingInterest,
232  scheduler::EventId timeoutEvent);
233 
234  void
235  cancelExcessInFlightSegments();
236 
237  bool
238  checkAllSegmentsReceived();
239 
240  time::milliseconds
241  getEstimatedRto();
242 
243 public:
249 
256 
261 
266 
271 
276 
282 
288 
289 private:
290  enum class SegmentState {
291  FirstInterest,
292  InRetxQueue,
293  Retransmitted,
294  };
295 
296  class PendingSegment
297  {
298  public:
299  SegmentState state;
302  scheduler::ScopedEventId timeoutEvent;
303  };
304 
306  static constexpr double MIN_SSTHRESH = 2.0;
307 
308  shared_ptr<SegmentFetcher> m_this;
309 
310  Options m_options;
311  Face& m_face;
312  Scheduler m_scheduler;
313  security::v2::Validator& m_validator;
314  RttEstimator m_rttEstimator;
315 
316  time::steady_clock::TimePoint m_timeLastSegmentReceived;
317  std::queue<uint64_t> m_retxQueue;
318  Name m_versionedDataName;
319  uint64_t m_nextSegmentNum = 0;
320  double m_cwnd;
321  double m_ssthresh;
322  int64_t m_nSegmentsInFlight = 0;
323  int64_t m_nSegments = 0;
324  uint64_t m_highInterest = 0;
325  uint64_t m_highData = 0;
326  uint64_t m_recPoint = 0;
327  int64_t m_nReceived = 0;
328  int64_t m_nBytesReceived = 0;
329  uint64_t m_nextSegmentInOrder = 0;
330 
331  std::map<uint64_t, Buffer> m_segmentBuffer;
332  std::map<uint64_t, PendingSegment> m_pendingSegments;
333  std::set<uint64_t> m_receivedSegments;
334 };
335 
336 } // namespace util
337 } // namespace ndn
338 
339 #endif // NDN_UTIL_SEGMENT_FETCHER_HPP
bool inOrder
true for &#39;in order&#39; mode, false for &#39;block&#39; mode
time_point TimePoint
Definition: time.hpp:225
Definition: data.cpp:26
time::milliseconds interestLifetime
lifetime of sent Interests - independent of Interest timeout
RTT/RTO estimator.
An unrecoverable Nack was received during retrieval.
double mdCoef
multiplicative decrease coefficient
Signal< SegmentFetcher, ConstBufferPtr > onComplete
Emitted upon successful retrieval of the complete object (all segments).
time::milliseconds maxTimeout
maximum allowed time between successful receipt of segments
bool useConstantInterestTimeout
if true, Interest timeout is kept at maxTimeout
Utility class to fetch the latest version of a segmented object.
Represents an Interest packet.
Definition: interest.hpp:50
provides a lightweight signal / event system
Definition: signal.hpp:52
Signal< SegmentFetcher > onInOrderComplete
Emitted on successful retrieval of all segments in &#39;in order&#39; mode.
void stop()
Stops fetching.
A handle for a scheduled event.
Definition: scheduler.hpp:60
represents a Network Nack
Definition: nack.hpp:38
double aiStep
additive increase step (in segments)
size_t flowControlWindow
maximum number of segments stored in the reorder buffer
bool disableCwa
disable Conservative Window Adaptation
One of the retrieved segments failed user-provided validation.
ErrorCode
Error codes passed to onError.
Handle for a pending Interest.
Definition: face.hpp:490
static shared_ptr< SegmentFetcher > start(Face &face, const Interest &baseInterest, security::v2::Validator &validator, const Options &options=Options())
Initiates segment fetching.
Signal< SegmentFetcher > afterSegmentNacked
Emitted whenever an Interest for a data segment is nacked.
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
Signal< SegmentFetcher, Data > afterSegmentValidated
Emitted whenever a received data segment has been successfully validated.
Retrieval timed out because the maximum timeout between the successful receipt of segments was exceed...
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
One of the retrieved Data packets lacked a segment number in the last Name component (excl...
Represents an absolute name.
Definition: name.hpp:44
double initSsthresh
initial slow start threshold
Generic time-based scheduler.
Definition: scheduler.hpp:134
double initCwnd
initial congestion window size
bool ignoreCongMarks
disable window decrease after congestion mark received
bool resetCwndToInit
reduce cwnd to initCwnd when loss event occurs
Signal< SegmentFetcher, Data > afterSegmentReceived
Emitted whenever a data segment received.
Signal< SegmentFetcher > afterSegmentTimedOut
Emitted whenever an Interest for a data segment times out.
RttEstimator::Options rttOptions
options for RTT estimator
Validation error code and optional detailed error message.
Signal< SegmentFetcher, uint32_t, std::string > onError
Emitted when the retrieval could not be completed due to an error.
Represents a Data packet.
Definition: data.hpp:39
A received FinalBlockId did not contain a segment component.
bool useConstantCwnd
if true, window size is kept at initCwnd
Interface for validating data and interest packets.
Definition: validator.hpp:61
Signal< SegmentFetcher, ConstBufferPtr > onInOrderData
Emitted after each data segment in segment order has been validated.