blob: 20fec041194bc4e5c6c1f818242b5e5327e9314b [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 Pesavento60f8cc12018-05-10 22:05:21 -04003 * Copyright (c) 2016-2018, 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
34#include "core/common.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:
Davide Pesavento60f8cc12018-05-10 22:05:21 -040052 using DataCallback = std::function<void(const Data&)>;
53 using FailureCallback = std::function<void(const std::string& reason)>;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055 /**
56 * @brief create a PipelineInterests service
57 *
Davide Pesaventoe9c69852017-11-04 18:08:37 -040058 * Configures the pipelining service without specifying the retrieval namespace.
59 * After construction, the method run() must be called in order to start the pipeline.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060 */
61 explicit
Weiwei Liue4765012016-06-01 00:10:29 -070062 PipelineInterests(Face& face);
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
67 /**
Weiwei Liue4765012016-06-01 00:10:29 -070068 * @brief start fetching all the segments of the specified prefix
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069 *
Weiwei Liue4765012016-06-01 00:10:29 -070070 * @param data a segment of the segmented Data to fetch; the Data name must end with a segment number
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071 * @param onData callback for every segment correctly received, must not be empty
Weiwei Liue4765012016-06-01 00:10:29 -070072 * @param onFailure callback if an error occurs, may be empty
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073 */
74 void
Weiwei Liue4765012016-06-01 00:10:29 -070075 run(const Data& data, DataCallback onData, FailureCallback onFailure);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010076
77 /**
78 * @brief stop all fetch operations
79 */
80 void
81 cancel();
82
Weiwei Liue4765012016-06-01 00:10:29 -070083protected:
Davide Pesaventobf1c0692017-01-15 19:15:09 -050084 time::steady_clock::TimePoint
85 getStartTime() const
86 {
87 return m_startTime;
88 }
89
Weiwei Liue4765012016-06-01 00:10:29 -070090 bool
91 isStopping() const
92 {
93 return m_isStopping;
94 }
95
Davide Pesaventob587cfd2017-11-04 16:29:50 -040096 /**
Ryan Wickman2c9933c2018-06-12 11:51:51 -050097 * @brief check if the transfer is complete
98 * @return true if all segments have been received, false otherwise
99 */
100 bool
101 allSegmentsReceived() const;
102
103 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400104 * @return next segment number to retrieve
105 * @post m_nextSegmentNo == return-value + 1
106 */
107 uint64_t
108 getNextSegmentNo();
109
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400110 /**
111 * @brief subclasses must call this method to notify successful retrieval of a segment
112 */
Weiwei Liue4765012016-06-01 00:10:29 -0700113 void
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400114 onData(const Data& data);
Weiwei Liue4765012016-06-01 00:10:29 -0700115
116 /**
117 * @brief subclasses can call this method to signal an unrecoverable failure
118 */
119 void
120 onFailure(const std::string& reason);
121
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000122 /**
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400123 * @brief print statistics about this fetching session
124 *
125 * Subclasses can override this method to print additional stats or change the summary format
126 */
127 virtual void
128 printSummary() const;
129
130 /**
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000131 * @param throughput The throughput in bits/s
132 */
133 static std::string
134 formatThroughput(double throughput);
135
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100136private:
137 /**
Weiwei Liue4765012016-06-01 00:10:29 -0700138 * @brief perform subclass-specific operations to fetch all the segments
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100139 *
Weiwei Liue4765012016-06-01 00:10:29 -0700140 * When overriding this function, at a minimum, the subclass should implement the retrieving
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400141 * of all the segments. Subclass must guarantee that `onData` is called once for every
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400142 * segment that is fetched successfully.
Weiwei Liue4765012016-06-01 00:10:29 -0700143 *
144 * @note m_lastSegmentNo contains a valid value only if m_hasFinalBlockId is true.
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100145 */
Weiwei Liue4765012016-06-01 00:10:29 -0700146 virtual void
147 doRun() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100148
Weiwei Liue4765012016-06-01 00:10:29 -0700149 virtual void
150 doCancel() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100151
Weiwei Liue4765012016-06-01 00:10:29 -0700152protected:
153 Face& m_face;
154 Name m_prefix;
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000155
Weiwei Liu245d7912016-07-28 00:04:25 -0700156PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400157 bool m_hasFinalBlockId; ///< true if the last segment number is known
158 uint64_t m_lastSegmentNo; ///< valid only if m_hasFinalBlockId == true
159 int64_t m_nReceived; ///< number of segments received
160 size_t m_receivedSize; ///< size of received data in bytes
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100161
162private:
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100163 DataCallback m_onData;
164 FailureCallback m_onFailure;
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400165 uint64_t m_nextSegmentNo;
166 uint64_t m_excludedSegmentNo;
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500167 time::steady_clock::TimePoint m_startTime;
Weiwei Liue4765012016-06-01 00:10:29 -0700168 bool m_isStopping;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100169};
170
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500171template<typename Packet>
172uint64_t
173getSegmentFromPacket(const Packet& packet)
174{
175 return packet.getName().at(-1).toSegment();
176}
177
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100178} // namespace chunks
179} // namespace ndn
180
181#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP