blob: 2757b21414bf97272a7cc02fa5b915ab957968cd [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 Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2016-2024, 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 Pesavento5748e822024-01-26 18:40:22 -050034#include "core/common.hpp"
Davide Pesavento97a33b22019-10-17 22:10:47 -040035#include "options.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036
Davide Pesavento5748e822024-01-26 18:40:22 -050037#include <ndn-cxx/face.hpp>
38
39#include <functional>
40
Davide Pesaventob3570c62022-02-19 19:19:00 -050041namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043/**
44 * @brief Service for retrieving Data via an Interest pipeline
45 *
Weiwei Liue4765012016-06-01 00:10:29 -070046 * Retrieves all segments of Data under a given prefix by maintaining a (variable or fixed-size)
47 * window of N Interests in flight. A user-specified callback function is used to notify
48 * the arrival of each segment of Data.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049 *
Weiwei Liue4765012016-06-01 00:10:29 -070050 * No guarantees are made as to the order in which segments are fetched or callbacks are invoked,
51 * i.e. out-of-order delivery is possible.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052 */
Davide Pesaventob3570c62022-02-19 19:19:00 -050053class PipelineInterests : noncopyable
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054{
55public:
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056 /**
Davide Pesavento97a33b22019-10-17 22:10:47 -040057 * @brief Constructor.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058 *
Davide Pesaventoe9c69852017-11-04 18:08:37 -040059 * Configures the pipelining service without specifying the retrieval namespace.
60 * After construction, the method run() must be called in order to start the pipeline.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061 */
Davide Pesavento97a33b22019-10-17 22:10:47 -040062 PipelineInterests(Face& face, const Options& opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010063
Weiwei Liue4765012016-06-01 00:10:29 -070064 virtual
Andrea Tosatto672b9a72016-01-05 16:18:20 +010065 ~PipelineInterests();
66
Davide Pesavento97a33b22019-10-17 22:10:47 -040067 using DataCallback = std::function<void(const Data&)>;
68 using FailureCallback = std::function<void(const std::string& reason)>;
69
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070 /**
Weiwei Liue4765012016-06-01 00:10:29 -070071 * @brief start fetching all the segments of the specified prefix
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072 *
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080073 * @param versionedName the name of the segmented Data ending with a version number
Andrea Tosatto672b9a72016-01-05 16:18:20 +010074 * @param onData callback for every segment correctly received, must not be empty
Weiwei Liue4765012016-06-01 00:10:29 -070075 * @param onFailure callback if an error occurs, may be empty
Andrea Tosatto672b9a72016-01-05 16:18:20 +010076 */
77 void
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080078 run(const Name& versionedName, DataCallback onData, FailureCallback onFailure);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010079
80 /**
81 * @brief stop all fetch operations
82 */
83 void
84 cancel();
85
Weiwei Liue4765012016-06-01 00:10:29 -070086protected:
Davide Pesaventof8d9a532021-07-03 16:04:12 -040087 time::steady_clock::time_point
Davide Pesaventobf1c0692017-01-15 19:15:09 -050088 getStartTime() const
89 {
90 return m_startTime;
91 }
92
Weiwei Liue4765012016-06-01 00:10:29 -070093 bool
94 isStopping() const
95 {
96 return m_isStopping;
97 }
98
Davide Pesaventob587cfd2017-11-04 16:29:50 -040099 /**
Ryan Wickman2c9933c2018-06-12 11:51:51 -0500100 * @brief check if the transfer is complete
101 * @return true if all segments have been received, false otherwise
102 */
Davide Pesaventob3570c62022-02-19 19:19:00 -0500103 [[nodiscard]] bool
Ryan Wickman2c9933c2018-06-12 11:51:51 -0500104 allSegmentsReceived() const;
105
106 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400107 * @return next segment number to retrieve
108 * @post m_nextSegmentNo == return-value + 1
109 */
110 uint64_t
111 getNextSegmentNo();
112
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400113 /**
114 * @brief subclasses must call this method to notify successful retrieval of a segment
115 */
Weiwei Liue4765012016-06-01 00:10:29 -0700116 void
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400117 onData(const Data& data);
Weiwei Liue4765012016-06-01 00:10:29 -0700118
119 /**
120 * @brief subclasses can call this method to signal an unrecoverable failure
121 */
122 void
123 onFailure(const std::string& reason);
124
Davide Pesavento97a33b22019-10-17 22:10:47 -0400125 void
126 printOptions() const;
127
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000128 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400129 * @brief print statistics about this fetching session
130 *
131 * Subclasses can override this method to print additional stats or change the summary format
132 */
133 virtual void
134 printSummary() const;
135
136 /**
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000137 * @param throughput The throughput in bits/s
138 */
139 static std::string
140 formatThroughput(double throughput);
141
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100142private:
143 /**
Weiwei Liue4765012016-06-01 00:10:29 -0700144 * @brief perform subclass-specific operations to fetch all the segments
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100145 *
Weiwei Liue4765012016-06-01 00:10:29 -0700146 * When overriding this function, at a minimum, the subclass should implement the retrieving
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400147 * of all the segments. Subclass must guarantee that `onData` is called once for every
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400148 * segment that is fetched successfully.
Weiwei Liue4765012016-06-01 00:10:29 -0700149 *
150 * @note m_lastSegmentNo contains a valid value only if m_hasFinalBlockId is true.
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100151 */
Weiwei Liue4765012016-06-01 00:10:29 -0700152 virtual void
153 doRun() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100154
Weiwei Liue4765012016-06-01 00:10:29 -0700155 virtual void
156 doCancel() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100157
Weiwei Liue4765012016-06-01 00:10:29 -0700158protected:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400159 const Options& m_options;
Weiwei Liue4765012016-06-01 00:10:29 -0700160 Face& m_face;
161 Name m_prefix;
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000162
Weiwei Liu245d7912016-07-28 00:04:25 -0700163PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Davide Pesaventob3570c62022-02-19 19:19:00 -0500164 bool m_hasFinalBlockId = false; ///< true if the last segment number is known
165 uint64_t m_lastSegmentNo = 0; ///< valid only if m_hasFinalBlockId == true
166 int64_t m_nReceived = 0; ///< number of segments received
167 size_t m_receivedSize = 0; ///< size of received data in bytes
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100168
169private:
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100170 DataCallback m_onData;
171 FailureCallback m_onFailure;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500172 uint64_t m_nextSegmentNo = 0;
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400173 time::steady_clock::time_point m_startTime;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500174 bool m_isStopping = false;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100175};
176
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500177template<typename Packet>
178uint64_t
179getSegmentFromPacket(const Packet& packet)
180{
181 return packet.getName().at(-1).toSegment();
182}
183
Davide Pesaventob3570c62022-02-19 19:19:00 -0500184} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100185
186#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP