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-2018, Regents of the University of California,
4  * Colorado State University,
5  * University Pierre & Marie Curie, Sorbonne University.
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.
22  *
23  * @author Shuo Yang
24  * @author Weiwei Liu
25  * @author Chavoosh Ghasemi
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"
33 #include "../security/v2/validator.hpp"
34 #include "rtt-estimator.hpp"
35 #include "scheduler.hpp"
36 #include "signal.hpp"
37 
38 #include <queue>
39 
40 namespace ndn {
41 namespace util {
42 
102 class SegmentFetcher : noncopyable
103 {
104 public:
105  // Deprecated: will be removed when deprecated fetch() API is removed - use start() instead
106  typedef function<void (ConstBufferPtr data)> CompleteCallback;
107  typedef function<void (uint32_t code, const std::string& msg)> ErrorCallback;
108 
112  enum ErrorCode {
123  };
124 
125  class Options
126  {
127  public:
129  {
130  }
131 
132  void
133  validate();
134 
135  public:
136  bool useConstantCwnd = false;
138  time::milliseconds maxTimeout = 60_s;
139  time::milliseconds interestLifetime = 4_s;
140  double initCwnd = 1.0;
141  double initSsthresh = std::numeric_limits<double>::max();
142  double aiStep = 1.0;
143  double mdCoef = 0.5;
144  bool disableCwa = false;
145  bool resetCwndToInit = false;
146  bool ignoreCongMarks = false;
148  };
149 
172  static shared_ptr<SegmentFetcher>
173  start(Face& face,
174  const Interest& baseInterest,
175  security::v2::Validator& validator,
176  const Options& options = Options());
177 
201  [[deprecated("use SegmentFetcher::start instead")]]
202  static shared_ptr<SegmentFetcher>
203  fetch(Face& face,
204  const Interest& baseInterest,
205  security::v2::Validator& validator,
206  const CompleteCallback& completeCallback,
207  const ErrorCallback& errorCallback);
208 
231  [[deprecated("use SegmentFetcher::start instead")]]
232  static shared_ptr<SegmentFetcher>
233  fetch(Face& face,
234  const Interest& baseInterest,
235  shared_ptr<security::v2::Validator> validator,
236  const CompleteCallback& completeCallback,
237  const ErrorCallback& errorCallback);
238 
239 private:
240  class PendingSegment;
241 
242  SegmentFetcher(Face& face, security::v2::Validator& validator, const Options& options);
243 
244  void
245  fetchFirstSegment(const Interest& baseInterest,
246  bool isRetransmission,
247  shared_ptr<SegmentFetcher> self);
248 
249  void
250  fetchSegmentsInWindow(const Interest& origInterest, shared_ptr<SegmentFetcher> self);
251 
252  void
253  afterSegmentReceivedCb(const Interest& origInterest,
254  const Data& data,
255  shared_ptr<SegmentFetcher> self);
256  void
257  afterValidationSuccess(const Data& data,
258  const Interest& origInterest,
259  std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt,
260  shared_ptr<SegmentFetcher> self);
261 
262  void
263  afterValidationFailure(const Data& data,
264  const security::v2::ValidationError& error,
265  shared_ptr<SegmentFetcher> self);
266 
267  void
268  afterNackReceivedCb(const Interest& origInterest,
269  const lp::Nack& nack,
270  shared_ptr<SegmentFetcher> self);
271 
272  void
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();
305 
306 public:
310  Signal<SegmentFetcher, ConstBufferPtr> onComplete;
311 
317  Signal<SegmentFetcher, uint32_t, std::string> onError;
318 
322  Signal<SegmentFetcher, Data> afterSegmentReceived;
323 
327  Signal<SegmentFetcher, Data> afterSegmentValidated;
328 
332  Signal<SegmentFetcher> afterSegmentNacked;
333 
337  Signal<SegmentFetcher> afterSegmentTimedOut;
338 
339 private:
340  enum class SegmentState {
341  FirstInterest,
342  InRetxQueue,
343  Retransmitted,
344  };
345 
346  class PendingSegment
347  {
348  public:
349  SegmentState state;
351  const PendingInterestId* id;
352  scheduler::EventId timeoutEvent;
353  };
354 
356  static constexpr double MIN_SSTHRESH = 2.0;
357 
358  Options m_options;
359  Face& m_face;
360  Scheduler m_scheduler;
361  security::v2::Validator& m_validator;
362  RttEstimator m_rttEstimator;
363  time::milliseconds m_timeout;
364 
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;
381 };
382 
383 } // namespace util
384 } // namespace ndn
385 
386 #endif // NDN_UTIL_SEGMENT_FETCHER_HPP
time_point TimePoint
Definition: time.hpp:225
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
time::milliseconds interestLifetime
lifetime of sent Interests - independent of Interest timeout
function< void(uint32_t code, const std::string &msg)> ErrorCallback
an unrecoverable Nack was received during retrieval
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
static shared_ptr< SegmentFetcher > fetch(Face &face, const Interest &baseInterest, security::v2::Validator &validator, const CompleteCallback &completeCallback, const ErrorCallback &errorCallback)
Initiates segment fetching.
double mdCoef
multiplicative decrease coefficient
Signal< SegmentFetcher, ConstBufferPtr > onComplete
Emits upon successful retrieval of the complete data.
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:43
represents a Network Nack
Definition: nack.hpp:40
double aiStep
additive increase step (in segments)
bool disableCwa
disable Conservative Window Adaptation
one of the retrieved segments failed user-provided validation
ErrorCode
Error codes passed to onError
static shared_ptr< SegmentFetcher > start(Face &face, const Interest &baseInterest, security::v2::Validator &validator, const Options &options=Options())
Initiates segment fetching.
Signal< SegmentFetcher > afterSegmentNacked
Emits 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
Emits whenever a received data segment has been successfully validated.
retrieval timed out because the maximum timeout between the successful receipt of segments was exceed...
function< void(ConstBufferPtr data)> CompleteCallback
one of the retrieved Data packets lacked a segment number in the last Name component (excl...
Represents an absolute name.
Definition: name.hpp:42
double initSsthresh
initial slow start threshold
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
Emits whenever a data segment received.
Signal< SegmentFetcher > afterSegmentTimedOut
Emits 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
Emits when the retrieval could not be completed due to an error.
Identifies a scheduled event.
Definition: scheduler.hpp:53
Represents a Data packet.
Definition: data.hpp:35
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