blob: ea52d2f69fc5b8273896cee655a55b6ce2b10667 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +00002/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, Regents of the University of California,
Davide Pesaventobf1c0692017-01-15 19:15:09 -05004 * 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 Wentao Shang
24 * @author Steve DiBenedetto
25 * @author Andrea Tosatto
Weiwei Liue4765012016-06-01 00:10:29 -070026 * @author Davide Pesavento
27 * @author Weiwei Liu
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000028 * @author Chavoosh Ghasemi
Andrea Tosatto672b9a72016-01-05 16:18:20 +010029 */
30
31#ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP
32#define NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP
33
Davide Pesavento97a33b22019-10-17 22:10:47 -040034#include "options.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035
Davide Pesaventob3570c62022-02-19 19:19:00 -050036namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010037
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038/**
39 * @brief Service for retrieving Data via an Interest pipeline
40 *
Weiwei Liue4765012016-06-01 00:10:29 -070041 * Retrieves all segments of Data under a given prefix by maintaining a (variable or fixed-size)
42 * window of N Interests in flight. A user-specified callback function is used to notify
43 * the arrival of each segment of Data.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010044 *
Weiwei Liue4765012016-06-01 00:10:29 -070045 * No guarantees are made as to the order in which segments are fetched or callbacks are invoked,
46 * i.e. out-of-order delivery is possible.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010047 */
Davide Pesaventob3570c62022-02-19 19:19:00 -050048class PipelineInterests : noncopyable
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049{
50public:
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051 /**
Davide Pesavento97a33b22019-10-17 22:10:47 -040052 * @brief Constructor.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053 *
Davide Pesaventoe9c69852017-11-04 18:08:37 -040054 * Configures the pipelining service without specifying the retrieval namespace.
55 * After construction, the method run() must be called in order to start the pipeline.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056 */
Davide Pesavento97a33b22019-10-17 22:10:47 -040057 PipelineInterests(Face& face, const Options& opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058
Weiwei Liue4765012016-06-01 00:10:29 -070059 virtual
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060 ~PipelineInterests();
61
Davide Pesavento97a33b22019-10-17 22:10:47 -040062 using DataCallback = std::function<void(const Data&)>;
63 using FailureCallback = std::function<void(const std::string& reason)>;
64
Andrea Tosatto672b9a72016-01-05 16:18:20 +010065 /**
Weiwei Liue4765012016-06-01 00:10:29 -070066 * @brief start fetching all the segments of the specified prefix
Andrea Tosatto672b9a72016-01-05 16:18:20 +010067 *
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080068 * @param versionedName the name of the segmented Data ending with a version number
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069 * @param onData callback for every segment correctly received, must not be empty
Weiwei Liue4765012016-06-01 00:10:29 -070070 * @param onFailure callback if an error occurs, may be empty
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071 */
72 void
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080073 run(const Name& versionedName, DataCallback onData, FailureCallback onFailure);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010074
75 /**
76 * @brief stop all fetch operations
77 */
78 void
79 cancel();
80
Weiwei Liue4765012016-06-01 00:10:29 -070081protected:
Davide Pesaventof8d9a532021-07-03 16:04:12 -040082 time::steady_clock::time_point
Davide Pesaventobf1c0692017-01-15 19:15:09 -050083 getStartTime() const
84 {
85 return m_startTime;
86 }
87
Weiwei Liue4765012016-06-01 00:10:29 -070088 bool
89 isStopping() const
90 {
91 return m_isStopping;
92 }
93
Davide Pesaventob587cfd2017-11-04 16:29:50 -040094 /**
Ryan Wickman2c9933c2018-06-12 11:51:51 -050095 * @brief check if the transfer is complete
96 * @return true if all segments have been received, false otherwise
97 */
Davide Pesaventob3570c62022-02-19 19:19:00 -050098 [[nodiscard]] bool
Ryan Wickman2c9933c2018-06-12 11:51:51 -050099 allSegmentsReceived() const;
100
101 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400102 * @return next segment number to retrieve
103 * @post m_nextSegmentNo == return-value + 1
104 */
105 uint64_t
106 getNextSegmentNo();
107
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400108 /**
109 * @brief subclasses must call this method to notify successful retrieval of a segment
110 */
Weiwei Liue4765012016-06-01 00:10:29 -0700111 void
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400112 onData(const Data& data);
Weiwei Liue4765012016-06-01 00:10:29 -0700113
114 /**
115 * @brief subclasses can call this method to signal an unrecoverable failure
116 */
117 void
118 onFailure(const std::string& reason);
119
Davide Pesavento97a33b22019-10-17 22:10:47 -0400120 void
121 printOptions() const;
122
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000123 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400124 * @brief print statistics about this fetching session
125 *
126 * Subclasses can override this method to print additional stats or change the summary format
127 */
128 virtual void
129 printSummary() const;
130
131 /**
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000132 * @param throughput The throughput in bits/s
133 */
134 static std::string
135 formatThroughput(double throughput);
136
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100137private:
138 /**
Weiwei Liue4765012016-06-01 00:10:29 -0700139 * @brief perform subclass-specific operations to fetch all the segments
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100140 *
Weiwei Liue4765012016-06-01 00:10:29 -0700141 * When overriding this function, at a minimum, the subclass should implement the retrieving
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400142 * of all the segments. Subclass must guarantee that `onData` is called once for every
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400143 * segment that is fetched successfully.
Weiwei Liue4765012016-06-01 00:10:29 -0700144 *
145 * @note m_lastSegmentNo contains a valid value only if m_hasFinalBlockId is true.
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100146 */
Weiwei Liue4765012016-06-01 00:10:29 -0700147 virtual void
148 doRun() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100149
Weiwei Liue4765012016-06-01 00:10:29 -0700150 virtual void
151 doCancel() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100152
Weiwei Liue4765012016-06-01 00:10:29 -0700153protected:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400154 const Options& m_options;
Weiwei Liue4765012016-06-01 00:10:29 -0700155 Face& m_face;
156 Name m_prefix;
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000157
Weiwei Liu245d7912016-07-28 00:04:25 -0700158PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Davide Pesaventob3570c62022-02-19 19:19:00 -0500159 bool m_hasFinalBlockId = false; ///< true if the last segment number is known
160 uint64_t m_lastSegmentNo = 0; ///< valid only if m_hasFinalBlockId == true
161 int64_t m_nReceived = 0; ///< number of segments received
162 size_t m_receivedSize = 0; ///< size of received data in bytes
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100163
164private:
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100165 DataCallback m_onData;
166 FailureCallback m_onFailure;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500167 uint64_t m_nextSegmentNo = 0;
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400168 time::steady_clock::time_point m_startTime;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500169 bool m_isStopping = false;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100170};
171
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500172template<typename Packet>
173uint64_t
174getSegmentFromPacket(const Packet& packet)
175{
176 return packet.getName().at(-1).toSegment();
177}
178
Davide Pesaventob3570c62022-02-19 19:19:00 -0500179} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100180
181#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP