blob: 53e32c0c9e0ccf29fb98127e96cdf0b47489047b [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/*
Junxiao Shi06d008c2019-02-04 08:26:59 +00003 * Copyright (c) 2016-2019, 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
32namespace ndn {
33namespace chunks {
34
35/**
36 * @brief fetch data for a given interest and handle timeout or nack error with retries
37 *
38 * To instantiate a DataFetcher you need to use the static method fetch, this will also express the
39 * interest. After a timeout or nack is received, the same interest with a different nonce will be
40 * requested for a maximum number of time specified by the class user. There are separate retry
41 * counters for timeouts and nacks.
42 *
43 * A specified callback is called after the data matching the expressed interest is received. A
44 * different callback is called in case one of the retries counter reach the maximum. This callback
45 * can be different for timeout and nack. The data callback must be defined but the others callback
46 * are optional.
47 *
48 */
49class DataFetcher
50{
51public:
52 /**
53 * @brief means that there is no maximum number of retries,
54 * i.e. fetching must be retried indefinitely
55 */
56 static const int MAX_RETRIES_INFINITE;
57
58 /**
59 * @brief ceiling value for backoff time used in congestion handling
60 */
61 static const time::milliseconds MAX_CONGESTION_BACKOFF_TIME;
62
Davide Pesavento60f8cc12018-05-10 22:05:21 -040063 using FailureCallback = std::function<void(const Interest& interest, const std::string& reason)>;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010064
65 /**
66 * @brief instantiate a DataFetcher object and start fetching data
67 *
68 * @param onData callback for segment correctly received, must not be empty
69 */
70 static shared_ptr<DataFetcher>
71 fetch(Face& face, const Interest& interest, int maxNackRetries, int maxTimeoutRetries,
72 DataCallback onData, FailureCallback onTimeout, FailureCallback onNack,
73 bool isVerbose);
74
75 /**
76 * @brief stop data fetching without error and calling any callback
77 */
78 void
79 cancel();
80
81 bool
82 isRunning() const
83 {
84 return !m_isStopped && !m_hasError;
85 }
86
87 bool
88 hasError() const
89 {
90 return m_hasError;
91 }
92
93private:
94 DataFetcher(Face& face, int maxNackRetries, int maxTimeoutRetries,
95 DataCallback onData, FailureCallback onNack, FailureCallback onTimeout,
96 bool isVerbose);
97
98 void
99 expressInterest(const Interest& interest, const shared_ptr<DataFetcher>& self);
100
101 void
102 handleData(const Interest& interest, const Data& data, const shared_ptr<DataFetcher>& self);
103
104 void
105 handleNack(const Interest& interest, const lp::Nack& nack, const shared_ptr<DataFetcher>& self);
106
107 void
108 handleTimeout(const Interest& interest, const shared_ptr<DataFetcher>& self);
109
110private:
111 Face& m_face;
112 Scheduler m_scheduler;
Junxiao Shi06d008c2019-02-04 08:26:59 +0000113 PendingInterestHandle m_pendingInterest;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100114 DataCallback m_onData;
115 FailureCallback m_onNack;
116 FailureCallback m_onTimeout;
117
118 int m_maxNackRetries;
119 int m_maxTimeoutRetries;
120 int m_nNacks;
121 int m_nTimeouts;
122 uint32_t m_nCongestionRetries;
123
124 bool m_isVerbose;
125 bool m_isStopped;
126 bool m_hasError;
127};
128
129} // namespace chunks
130} // namespace ndn
131
132#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_DATA_FETCHER_HPP