blob: 765963d4c4384e034df1e2da5c3bf83f7b689213 [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 Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2016-2024, 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 Pesavento5748e822024-01-26 18:40:22 -050032#include <ndn-cxx/face.hpp>
33#include <ndn-cxx/util/scheduler.hpp>
34
35#include <functional>
36
Davide Pesaventob3570c62022-02-19 19:19:00 -050037namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038
39/**
Davide Pesaventob3570c62022-02-19 19:19:00 -050040 * @brief Fetch data for a given interest and handle timeout or nack error with retries.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041 *
42 * To instantiate a DataFetcher you need to use the static method fetch, this will also express the
43 * interest. After a timeout or nack is received, the same interest with a different nonce will be
44 * requested for a maximum number of time specified by the class user. There are separate retry
45 * counters for timeouts and nacks.
46 *
47 * A specified callback is called after the data matching the expressed interest is received. A
48 * different callback is called in case one of the retries counter reach the maximum. This callback
49 * can be different for timeout and nack. The data callback must be defined but the others callback
50 * are optional.
51 *
52 */
53class DataFetcher
54{
55public:
56 /**
57 * @brief means that there is no maximum number of retries,
58 * i.e. fetching must be retried indefinitely
59 */
Davide Pesaventob3570c62022-02-19 19:19:00 -050060 static constexpr int MAX_RETRIES_INFINITE = -1;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061
62 /**
63 * @brief ceiling value for backoff time used in congestion handling
64 */
Davide Pesaventob3570c62022-02-19 19:19:00 -050065 static constexpr time::milliseconds MAX_CONGESTION_BACKOFF_TIME = 10_s;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010066
Davide Pesavento60f8cc12018-05-10 22:05:21 -040067 using FailureCallback = std::function<void(const Interest& interest, const std::string& reason)>;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010068
69 /**
70 * @brief instantiate a DataFetcher object and start fetching data
71 *
72 * @param onData callback for segment correctly received, must not be empty
73 */
Davide Pesavento5748e822024-01-26 18:40:22 -050074 static std::shared_ptr<DataFetcher>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075 fetch(Face& face, const Interest& interest, int maxNackRetries, int maxTimeoutRetries,
76 DataCallback onData, FailureCallback onTimeout, FailureCallback onNack,
77 bool isVerbose);
78
79 /**
80 * @brief stop data fetching without error and calling any callback
81 */
82 void
83 cancel();
84
85 bool
86 isRunning() const
87 {
88 return !m_isStopped && !m_hasError;
89 }
90
91 bool
92 hasError() const
93 {
94 return m_hasError;
95 }
96
97private:
98 DataFetcher(Face& face, int maxNackRetries, int maxTimeoutRetries,
99 DataCallback onData, FailureCallback onNack, FailureCallback onTimeout,
100 bool isVerbose);
101
102 void
Davide Pesavento5748e822024-01-26 18:40:22 -0500103 expressInterest(const Interest& interest, const std::shared_ptr<DataFetcher>& self);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100104
105 void
Davide Pesavento5748e822024-01-26 18:40:22 -0500106 handleData(const Interest& interest, const Data& data, const std::shared_ptr<DataFetcher>& self);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100107
108 void
Davide Pesavento5748e822024-01-26 18:40:22 -0500109 handleNack(const Interest& interest, const lp::Nack& nack, const std::shared_ptr<DataFetcher>& self);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100110
111 void
Davide Pesavento5748e822024-01-26 18:40:22 -0500112 handleTimeout(const Interest& interest, const std::shared_ptr<DataFetcher>& self);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100113
114private:
115 Face& m_face;
116 Scheduler m_scheduler;
Junxiao Shi06d008c2019-02-04 08:26:59 +0000117 PendingInterestHandle m_pendingInterest;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100118 DataCallback m_onData;
119 FailureCallback m_onNack;
120 FailureCallback m_onTimeout;
121
122 int m_maxNackRetries;
123 int m_maxTimeoutRetries;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500124 int m_nNacks = 0;
125 int m_nTimeouts = 0;
126 uint32_t m_nCongestionRetries = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100127
Davide Pesaventob3570c62022-02-19 19:19:00 -0500128 bool m_isVerbose = false;
129 bool m_isStopped = false;
130 bool m_hasError = false;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100131};
132
Davide Pesaventob3570c62022-02-19 19:19:00 -0500133} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100134
135#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP