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-2019 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 "ndn-cxx/face.hpp"
35 #include "ndn-cxx/util/signal.hpp"
36 
37 #include <queue>
38 
39 namespace ndn {
40 namespace util {
41 
97 class SegmentFetcher : noncopyable
98 {
99 public:
103  enum ErrorCode {
114  };
115 
116  class Options
117  {
118  public:
120  {
121  }
122 
123  void
124  validate();
125 
126  public:
127  bool useConstantCwnd = false;
129  time::milliseconds maxTimeout = 60_s;
130  time::milliseconds interestLifetime = 4_s;
131  double initCwnd = 1.0;
132  double initSsthresh = std::numeric_limits<double>::max();
133  double aiStep = 1.0;
134  double mdCoef = 0.5;
135  bool disableCwa = false;
136  bool resetCwndToInit = false;
137  bool ignoreCongMarks = false;
139  };
140 
163  static shared_ptr<SegmentFetcher>
164  start(Face& face,
165  const Interest& baseInterest,
166  security::v2::Validator& validator,
167  const Options& options = Options());
168 
174  void
175  stop();
176 
177 private:
178  class PendingSegment;
179 
180  SegmentFetcher(Face& face, security::v2::Validator& validator, const Options& options);
181 
182  static bool
183  shouldStop(const weak_ptr<SegmentFetcher>& weakSelf);
184 
185  void
186  fetchFirstSegment(const Interest& baseInterest, bool isRetransmission);
187 
188  void
189  fetchSegmentsInWindow(const Interest& origInterest);
190 
191  void
192  sendInterest(uint64_t segNum, const Interest& interest, bool isRetransmission);
193 
194  void
195  afterSegmentReceivedCb(const Interest& origInterest, const Data& data,
196  const weak_ptr<SegmentFetcher>& weakSelf);
197 
198  void
199  afterValidationSuccess(const Data& data, const Interest& origInterest,
200  std::map<uint64_t, PendingSegment>::iterator pendingSegmentIt,
201  const weak_ptr<SegmentFetcher>& weakSelf);
202 
203  void
204  afterValidationFailure(const Data& data,
205  const security::v2::ValidationError& error,
206  const weak_ptr<SegmentFetcher>& weakSelf);
207 
208  void
209  afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack,
210  const weak_ptr<SegmentFetcher>& weakSelf);
211 
212  void
213  afterTimeoutCb(const Interest& origInterest,
214  const weak_ptr<SegmentFetcher>& weakSelf);
215 
216  void
217  afterNackOrTimeout(const Interest& origInterest);
218 
219  void
220  finalizeFetch();
221 
222  void
223  windowIncrease();
224 
225  void
226  windowDecrease();
227 
228  void
229  signalError(uint32_t code, const std::string& msg);
230 
231  void
232  updateRetransmittedSegment(uint64_t segmentNum,
233  const PendingInterestHandle& pendingInterest,
234  scheduler::EventId timeoutEvent);
235 
236  void
237  cancelExcessInFlightSegments();
238 
239  bool
240  checkAllSegmentsReceived();
241 
242  time::milliseconds
243  getEstimatedRto();
244 
245 public:
249  Signal<SegmentFetcher, ConstBufferPtr> onComplete;
250 
256  Signal<SegmentFetcher, uint32_t, std::string> onError;
257 
261  Signal<SegmentFetcher, Data> afterSegmentReceived;
262 
266  Signal<SegmentFetcher, Data> afterSegmentValidated;
267 
271  Signal<SegmentFetcher> afterSegmentNacked;
272 
276  Signal<SegmentFetcher> afterSegmentTimedOut;
277 
278 private:
279  enum class SegmentState {
280  FirstInterest,
281  InRetxQueue,
282  Retransmitted,
283  };
284 
285  class PendingSegment
286  {
287  public:
288  SegmentState state;
291  scheduler::ScopedEventId timeoutEvent;
292  };
293 
295  static constexpr double MIN_SSTHRESH = 2.0;
296 
297  shared_ptr<SegmentFetcher> m_this;
298 
299  Options m_options;
300  Face& m_face;
301  Scheduler m_scheduler;
302  security::v2::Validator& m_validator;
303  RttEstimator m_rttEstimator;
304  time::milliseconds m_timeout;
305 
306  time::steady_clock::TimePoint m_timeLastSegmentReceived;
307  std::queue<uint64_t> m_retxQueue;
308  Name m_versionedDataName;
309  uint64_t m_nextSegmentNum;
310  double m_cwnd;
311  double m_ssthresh;
312  int64_t m_nSegmentsInFlight;
313  int64_t m_nSegments;
314  uint64_t m_highInterest;
315  uint64_t m_highData;
316  uint64_t m_recPoint;
317  int64_t m_nReceived;
318  int64_t m_nBytesReceived;
319 
320  std::map<uint64_t, Buffer> m_receivedSegments;
321  std::map<uint64_t, PendingSegment> m_pendingSegments;
322 };
323 
324 } // namespace util
325 } // namespace ndn
326 
327 #endif // NDN_UTIL_SEGMENT_FETCHER_HPP
time_point TimePoint
Definition: time.hpp:225
Definition: data.cpp:26
time::milliseconds interestLifetime
lifetime of sent Interests - independent of Interest timeout
An unrecoverable Nack was received during retrieval.
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:44
void stop()
Stops fetching.
represents a Network Nack
Definition: nack.hpp:38
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.
A handle of pending Interest.
Definition: face.hpp:548
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:93
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...
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
One of the retrieved Data packets lacked a segment number in the last Name component (excl...
Represents an absolute name.
Definition: name.hpp:43
double initSsthresh
initial slow start threshold
A scoped handle of scheduled event.
Definition: scheduler.hpp:127
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.
A handle of scheduled event.
Definition: scheduler.hpp:59
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
Cancels an operation automatically upon destruction.
Interface for validating data and interest packets.
Definition: validator.hpp:61