NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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:
250 
257 
262 
267 
272 
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
ndn::security::v2::ValidationError
Validation error code and optional detailed error message.
Definition: validation-error.hpp:35
ndn::security::v2::Validator
Interface for validating data and interest packets.
Definition: validator.hpp:62
ndn::util::SegmentFetcher::onError
Signal< SegmentFetcher, uint32_t, std::string > onError
Emits when the retrieval could not be completed due to an error.
Definition: segment-fetcher.hpp:256
ndn::util::SegmentFetcher::NACK_ERROR
@ NACK_ERROR
An unrecoverable Nack was received during retrieval.
Definition: segment-fetcher.hpp:111
ndn::util::SegmentFetcher::DATA_HAS_NO_SEGMENT
@ DATA_HAS_NO_SEGMENT
One of the retrieved Data packets lacked a segment number in the last Name component (excl....
Definition: segment-fetcher.hpp:107
signal.hpp
ndn::util::SegmentFetcher::FINALBLOCKID_NOT_SEGMENT
@ FINALBLOCKID_NOT_SEGMENT
A received FinalBlockId did not contain a segment component.
Definition: segment-fetcher.hpp:113
validator.hpp
ndn::util::SegmentFetcher::Options::resetCwndToInit
bool resetCwndToInit
reduce cwnd to initCwnd when loss event occurs
Definition: segment-fetcher.hpp:136
ndn::PendingInterestHandle
A handle of pending Interest.
Definition: face.hpp:493
rtt-estimator.hpp
ndn::util::SegmentFetcher::INTEREST_TIMEOUT
@ INTEREST_TIMEOUT
Retrieval timed out because the maximum timeout between the successful receipt of segments was exceed...
Definition: segment-fetcher.hpp:105
ndn::util::SegmentFetcher::Options::maxTimeout
time::milliseconds maxTimeout
maximum allowed time between successful receipt of segments
Definition: segment-fetcher.hpp:129
ndn::util::SegmentFetcher::Options::initCwnd
double initCwnd
initial congestion window size
Definition: segment-fetcher.hpp:131
ndn::Face
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
scheduler.hpp
ndn::util::SegmentFetcher::Options::useConstantInterestTimeout
bool useConstantInterestTimeout
if true, Interest timeout is kept at maxTimeout
Definition: segment-fetcher.hpp:128
ndn::util::signal::Signal
provides a lightweight signal / event system
Definition: signal.hpp:52
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::scheduler::EventId
A handle for a scheduled event.
Definition: scheduler.hpp:59
ndn::util::SegmentFetcher::Options::initSsthresh
double initSsthresh
initial slow start threshold
Definition: segment-fetcher.hpp:132
ndn::util::SegmentFetcher::afterSegmentNacked
Signal< SegmentFetcher > afterSegmentNacked
Emits whenever an Interest for a data segment is nacked.
Definition: segment-fetcher.hpp:271
ndn::util::SegmentFetcher::Options::validate
void validate()
Definition: segment-fetcher.cpp:46
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
ndn::util::SegmentFetcher::Options::mdCoef
double mdCoef
multiplicative decrease coefficient
Definition: segment-fetcher.hpp:134
ndn::util::SegmentFetcher::SEGMENT_VALIDATION_FAIL
@ SEGMENT_VALIDATION_FAIL
One of the retrieved segments failed user-provided validation.
Definition: segment-fetcher.hpp:109
ndn::util::SegmentFetcher
Utility class to fetch the latest version of a segmented object.
Definition: segment-fetcher.hpp:98
ndn::util::SegmentFetcher::Options::disableCwa
bool disableCwa
disable Conservative Window Adaptation
Definition: segment-fetcher.hpp:135
ndn::util::SegmentFetcher::afterSegmentTimedOut
Signal< SegmentFetcher > afterSegmentTimedOut
Emits whenever an Interest for a data segment times out.
Definition: segment-fetcher.hpp:276
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::util::SegmentFetcher::afterSegmentValidated
Signal< SegmentFetcher, Data > afterSegmentValidated
Emits whenever a received data segment has been successfully validated.
Definition: segment-fetcher.hpp:266
ndn::time::steady_clock::TimePoint
time_point TimePoint
Definition: time.hpp:225
ndn::util::SegmentFetcher::Options::rttOptions
RttEstimator::Options rttOptions
options for RTT estimator
Definition: segment-fetcher.hpp:138
face.hpp
ndn::util::SegmentFetcher::afterSegmentReceived
Signal< SegmentFetcher, Data > afterSegmentReceived
Emits whenever a data segment received.
Definition: segment-fetcher.hpp:261
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
ndn::scheduler::Scheduler
Generic time-based scheduler.
Definition: scheduler.hpp:133
ndn::util::SegmentFetcher::Options::Options
Options()
Definition: segment-fetcher.hpp:119
RttEstimator
ndn RttEstimator
Definition: ndn-rtt-estimator.cpp:38
ndn::util::RttEstimator::Options
Definition: rtt-estimator.hpp:45
ndn::util::SegmentFetcher::Options::useConstantCwnd
bool useConstantCwnd
if true, window size is kept at initCwnd
Definition: segment-fetcher.hpp:127
ndn::util::SegmentFetcher::ErrorCode
ErrorCode
Error codes passed to onError.
Definition: segment-fetcher.hpp:103
ndn::util::SegmentFetcher::Options::interestLifetime
time::milliseconds interestLifetime
lifetime of sent Interests - independent of Interest timeout
Definition: segment-fetcher.hpp:130
ndn::util::SegmentFetcher::onComplete
Signal< SegmentFetcher, ConstBufferPtr > onComplete
Emits upon successful retrieval of the complete data.
Definition: segment-fetcher.hpp:249
ndn::util::SegmentFetcher::Options::aiStep
double aiStep
additive increase step (in segments)
Definition: segment-fetcher.hpp:133
ndn::util::SegmentFetcher::Options
Definition: segment-fetcher.hpp:117
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::detail::ScopedCancelHandle< PendingInterestHandle >
ndn::util::SegmentFetcher::Options::ignoreCongMarks
bool ignoreCongMarks
disable window decrease after congestion mark received
Definition: segment-fetcher.hpp:137
ndn::util::SegmentFetcher::stop
void stop()
Stops fetching.
Definition: segment-fetcher.cpp:101
ndn::util::SegmentFetcher::start
static shared_ptr< SegmentFetcher > start(Face &face, const Interest &baseInterest, security::v2::Validator &validator, const Options &options=Options())
Initiates segment fetching.
Definition: segment-fetcher.cpp:89