blob: 8fe349e210379acff501390fc82474a536f9cd71 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventobf1c0692017-01-15 19:15:09 -05003 * Copyright (c) 2016-2017, Regents of the University of California,
4 * 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
Andrea Tosatto672b9a72016-01-05 16:18:20 +010028 */
29
30#ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP
31#define NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP
32
33#include "core/common.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034
35namespace ndn {
36namespace chunks {
37
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 */
48class PipelineInterests
49{
50public:
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051 typedef function<void(const std::string& reason)> FailureCallback;
52
53public:
54 /**
55 * @brief create a PipelineInterests service
56 *
57 * Configures the pipelining service without specifying the retrieval namespace. After this
Weiwei Liue4765012016-06-01 00:10:29 -070058 * configuration the method run must be called to start the Pipeline.
Andrea Tosatto672b9a72016-01-05 16:18:20 +010059 */
60 explicit
Weiwei Liue4765012016-06-01 00:10:29 -070061 PipelineInterests(Face& face);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010062
Weiwei Liue4765012016-06-01 00:10:29 -070063 virtual
Andrea Tosatto672b9a72016-01-05 16:18:20 +010064 ~PipelineInterests();
65
66 /**
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 *
Weiwei Liue4765012016-06-01 00:10:29 -070069 * @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 +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
Weiwei Liue4765012016-06-01 00:10:29 -070074 run(const Data& data, 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
95 void
96 onData(const Interest& interest, const Data& data) const
97 {
98 m_onData(interest, data);
99 }
100
101 /**
102 * @brief subclasses can call this method to signal an unrecoverable failure
103 */
104 void
105 onFailure(const std::string& reason);
106
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100107private:
108 /**
Weiwei Liue4765012016-06-01 00:10:29 -0700109 * @brief perform subclass-specific operations to fetch all the segments
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100110 *
Weiwei Liue4765012016-06-01 00:10:29 -0700111 * When overriding this function, at a minimum, the subclass should implement the retrieving
112 * of all the segments. Segment m_excludedSegmentNo can be skipped. Subclass must guarantee
113 * that onData is called at least once for every segment that is fetched successfully.
114 *
115 * @note m_lastSegmentNo contains a valid value only if m_hasFinalBlockId is true.
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100116 */
Weiwei Liue4765012016-06-01 00:10:29 -0700117 virtual void
118 doRun() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100119
Weiwei Liue4765012016-06-01 00:10:29 -0700120 virtual void
121 doCancel() = 0;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100122
Weiwei Liue4765012016-06-01 00:10:29 -0700123protected:
124 Face& m_face;
125 Name m_prefix;
126 uint64_t m_lastSegmentNo;
127 uint64_t m_excludedSegmentNo;
Weiwei Liu245d7912016-07-28 00:04:25 -0700128
129PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Weiwei Liue4765012016-06-01 00:10:29 -0700130 bool m_hasFinalBlockId; ///< true if the last segment number is known
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100131
132private:
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100133 DataCallback m_onData;
134 FailureCallback m_onFailure;
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500135 time::steady_clock::TimePoint m_startTime;
Weiwei Liue4765012016-06-01 00:10:29 -0700136 bool m_isStopping;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100137};
138
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500139template<typename Packet>
140uint64_t
141getSegmentFromPacket(const Packet& packet)
142{
143 return packet.getName().at(-1).toSegment();
144}
145
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100146} // namespace chunks
147} // namespace ndn
148
149#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP