blob: c51d577654b5f089ea83307142dbf652689979e7 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento60f8cc12018-05-10 22:05:21 -04002/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, Regents of the University of California,
Davide Pesavento60f8cc12018-05-10 22:05:21 -04004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Andrea Tosatto
24 * @author Davide Pesavento
25 */
26
27#ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP
28#define NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP
29
30#include "core/common.hpp"
31
Davide Pesaventob3570c62022-02-19 19:19:00 -050032namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010033
34/**
Davide Pesaventob3570c62022-02-19 19:19:00 -050035 * @brief Fetch data for a given interest and handle timeout or nack error with retries.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036 *
37 * To instantiate a DataFetcher you need to use the static method fetch, this will also express the
38 * interest. After a timeout or nack is received, the same interest with a different nonce will be
39 * requested for a maximum number of time specified by the class user. There are separate retry
40 * counters for timeouts and nacks.
41 *
42 * A specified callback is called after the data matching the expressed interest is received. A
43 * different callback is called in case one of the retries counter reach the maximum. This callback
44 * can be different for timeout and nack. The data callback must be defined but the others callback
45 * are optional.
46 *
47 */
48class DataFetcher
49{
50public:
51 /**
52 * @brief means that there is no maximum number of retries,
53 * i.e. fetching must be retried indefinitely
54 */
Davide Pesaventob3570c62022-02-19 19:19:00 -050055 static constexpr int MAX_RETRIES_INFINITE = -1;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056
57 /**
58 * @brief ceiling value for backoff time used in congestion handling
59 */
Davide Pesaventob3570c62022-02-19 19:19:00 -050060 static constexpr time::milliseconds MAX_CONGESTION_BACKOFF_TIME = 10_s;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061
Davide Pesavento60f8cc12018-05-10 22:05:21 -040062 using FailureCallback = std::function<void(const Interest& interest, const std::string& reason)>;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010063
64 /**
65 * @brief instantiate a DataFetcher object and start fetching data
66 *
67 * @param onData callback for segment correctly received, must not be empty
68 */
69 static shared_ptr<DataFetcher>
70 fetch(Face& face, const Interest& interest, int maxNackRetries, int maxTimeoutRetries,
71 DataCallback onData, FailureCallback onTimeout, FailureCallback onNack,
72 bool isVerbose);
73
74 /**
75 * @brief stop data fetching without error and calling any callback
76 */
77 void
78 cancel();
79
80 bool
81 isRunning() const
82 {
83 return !m_isStopped && !m_hasError;
84 }
85
86 bool
87 hasError() const
88 {
89 return m_hasError;
90 }
91
92private:
93 DataFetcher(Face& face, int maxNackRetries, int maxTimeoutRetries,
94 DataCallback onData, FailureCallback onNack, FailureCallback onTimeout,
95 bool isVerbose);
96
97 void
98 expressInterest(const Interest& interest, const shared_ptr<DataFetcher>& self);
99
100 void
101 handleData(const Interest& interest, const Data& data, const shared_ptr<DataFetcher>& self);
102
103 void
104 handleNack(const Interest& interest, const lp::Nack& nack, const shared_ptr<DataFetcher>& self);
105
106 void
107 handleTimeout(const Interest& interest, const shared_ptr<DataFetcher>& self);
108
109private:
110 Face& m_face;
111 Scheduler m_scheduler;
Junxiao Shi06d008c2019-02-04 08:26:59 +0000112 PendingInterestHandle m_pendingInterest;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100113 DataCallback m_onData;
114 FailureCallback m_onNack;
115 FailureCallback m_onTimeout;
116
117 int m_maxNackRetries;
118 int m_maxTimeoutRetries;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500119 int m_nNacks = 0;
120 int m_nTimeouts = 0;
121 uint32_t m_nCongestionRetries = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100122
Davide Pesaventob3570c62022-02-19 19:19:00 -0500123 bool m_isVerbose = false;
124 bool m_isStopped = false;
125 bool m_hasError = false;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100126};
127
Davide Pesaventob3570c62022-02-19 19:19:00 -0500128} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100129
130#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP