blob: dd8b5de73a01b5f527a8611f6216ddb63ffb6e9b [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/*
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -08003 * Copyright (c) 2016-2019, 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
36namespace ndn {
37namespace chunks {
38
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039/**
40 * @brief Service for retrieving Data via an Interest pipeline
41 *
Weiwei Liue4765012016-06-01 00:10:29 -070042 * Retrieves all segments of Data under a given prefix by maintaining a (variable or fixed-size)
43 * window of N Interests in flight. A user-specified callback function is used to notify
44 * the arrival of each segment of Data.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045 *
Weiwei Liue4765012016-06-01 00:10:29 -070046 * No guarantees are made as to the order in which segments are fetched or callbacks are invoked,
47 * i.e. out-of-order delivery is possible.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048 */
49class PipelineInterests
50{
51public:
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052 /**
Davide Pesavento97a33b22019-10-17 22:10:47 -040053 * @brief Constructor.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054 *
Davide Pesaventoe9c69852017-11-04 18:08:37 -040055 * Configures the pipelining service without specifying the retrieval namespace.
56 * After construction, the method run() must be called in order to start the pipeline.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057 */
Davide Pesavento97a33b22019-10-17 22:10:47 -040058 PipelineInterests(Face& face, const Options& opts);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010059
Weiwei Liue4765012016-06-01 00:10:29 -070060 virtual
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061 ~PipelineInterests();
62
Davide Pesavento97a33b22019-10-17 22:10:47 -040063 using DataCallback = std::function<void(const Data&)>;
64 using FailureCallback = std::function<void(const std::string& reason)>;
65
Andrea Tosatto672b9a72016-01-05 16:18:20 +010066 /**
Weiwei Liue4765012016-06-01 00:10:29 -070067 * @brief start fetching all the segments of the specified prefix
Andrea Tosatto672b9a72016-01-05 16:18:20 +010068 *
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080069 * @param versionedName the name of the segmented Data ending with a version number
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070 * @param onData callback for every segment correctly received, must not be empty
Weiwei Liue4765012016-06-01 00:10:29 -070071 * @param onFailure callback if an error occurs, may be empty
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072 */
73 void
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080074 run(const Name& versionedName, DataCallback onData, FailureCallback onFailure);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075
76 /**
77 * @brief stop all fetch operations
78 */
79 void
80 cancel();
81
Weiwei Liue4765012016-06-01 00:10:29 -070082protected:
Davide Pesaventobf1c0692017-01-15 19:15:09 -050083 time::steady_clock::TimePoint
84 getStartTime() const
85 {
86 return m_startTime;
87 }
88
Weiwei Liue4765012016-06-01 00:10:29 -070089 bool
90 isStopping() const
91 {
92 return m_isStopping;
93 }
94
Davide Pesaventob587cfd2017-11-04 16:29:50 -040095 /**
Ryan Wickman2c9933c2018-06-12 11:51:51 -050096 * @brief check if the transfer is complete
97 * @return true if all segments have been received, false otherwise
98 */
99 bool
100 allSegmentsReceived() const;
101
102 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400103 * @return next segment number to retrieve
104 * @post m_nextSegmentNo == return-value + 1
105 */
106 uint64_t
107 getNextSegmentNo();
108
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400109 /**
110 * @brief subclasses must call this method to notify successful retrieval of a segment
111 */
Weiwei Liue4765012016-06-01 00:10:29 -0700112 void
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400113 onData(const Data& data);
Weiwei Liue4765012016-06-01 00:10:29 -0700114
115 /**
116 * @brief subclasses can call this method to signal an unrecoverable failure
117 */
118 void
119 onFailure(const std::string& reason);
120
Davide Pesavento97a33b22019-10-17 22:10:47 -0400121 void
122 printOptions() const;
123
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000124 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400125 * @brief print statistics about this fetching session
126 *
127 * Subclasses can override this method to print additional stats or change the summary format
128 */
129 virtual void
130 printSummary() const;
131
132 /**
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000133 * @param throughput The throughput in bits/s
134 */
135 static std::string
136 formatThroughput(double throughput);
137
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100138private:
139 /**
Weiwei Liue4765012016-06-01 00:10:29 -0700140 * @brief perform subclass-specific operations to fetch all the segments
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141 *
Weiwei Liue4765012016-06-01 00:10:29 -0700142 * When overriding this function, at a minimum, the subclass should implement the retrieving
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400143 * of all the segments. Subclass must guarantee that `onData` is called once for every
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400144 * segment that is fetched successfully.
Weiwei Liue4765012016-06-01 00:10:29 -0700145 *
146 * @note m_lastSegmentNo contains a valid value only if m_hasFinalBlockId is true.
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100147 */
Weiwei Liue4765012016-06-01 00:10:29 -0700148 virtual void
149 doRun() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100150
Weiwei Liue4765012016-06-01 00:10:29 -0700151 virtual void
152 doCancel() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100153
Weiwei Liue4765012016-06-01 00:10:29 -0700154protected:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400155 const Options& m_options;
Weiwei Liue4765012016-06-01 00:10:29 -0700156 Face& m_face;
157 Name m_prefix;
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000158
Weiwei Liu245d7912016-07-28 00:04:25 -0700159PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400160 bool m_hasFinalBlockId; ///< true if the last segment number is known
161 uint64_t m_lastSegmentNo; ///< valid only if m_hasFinalBlockId == true
162 int64_t m_nReceived; ///< number of segments received
163 size_t m_receivedSize; ///< size of received data in bytes
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100164
165private:
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100166 DataCallback m_onData;
167 FailureCallback m_onFailure;
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400168 uint64_t m_nextSegmentNo;
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500169 time::steady_clock::TimePoint m_startTime;
Weiwei Liue4765012016-06-01 00:10:29 -0700170 bool m_isStopping;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100171};
172
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500173template<typename Packet>
174uint64_t
175getSegmentFromPacket(const Packet& packet)
176{
177 return packet.getName().at(-1).toSegment();
178}
179
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100180} // namespace chunks
181} // namespace ndn
182
183#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP