blob: 32d5b79ab5f47050b60886d93997bb95dbaad120 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
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
26 */
27
28#ifndef NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP
29#define NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP
30
31#include "core/common.hpp"
32#include "options.hpp"
33
34namespace ndn {
35namespace chunks {
36
37class DataFetcher;
38
39class PipelineInterestsOptions : public Options
40{
41public:
42 explicit
43 PipelineInterestsOptions(const Options& options = Options())
44 : Options(options)
45 , maxPipelineSize(1)
46 {
47 }
48
49public:
50 size_t maxPipelineSize;
51};
52
53/**
54 * @brief Service for retrieving Data via an Interest pipeline
55 *
56 * Retrieves all segmented Data under the specified prefix by maintaining a pipeline of N Interests
57 * in flight.
58 *
59 * Provides retrieved Data on arrival with no ordering guarantees. Data is delivered to the
60 * PipelineInterests' user via callback immediately upon arrival.
61 */
62class PipelineInterests
63{
64public:
65 typedef PipelineInterestsOptions Options;
66 typedef function<void(const std::string& reason)> FailureCallback;
67
68public:
69 /**
70 * @brief create a PipelineInterests service
71 *
72 * Configures the pipelining service without specifying the retrieval namespace. After this
73 * configuration the method runWithExcludedSegment must be called to run the Pipeline.
74 */
75 explicit
76 PipelineInterests(Face& face, const Options& options = Options());
77
78 ~PipelineInterests();
79
80 /**
81 * @brief fetch all the segments between 0 and lastSegment of the specified prefix
82 *
83 * Starts the pipeline of size defined inside the options. The pipeline retrieves all the segments
84 * until the last segment is received, @p data is excluded from the retrieving.
85 *
86 * @param data a segment of the segmented Data to retrive; data.getName() must end with a segment
87 * number
88 * @param onData callback for every segment correctly received, must not be empty
89 * @param onfailure callback called if an error occurs
90 */
91 void
92 runWithExcludedSegment(const Data& data, DataCallback onData, FailureCallback onFailure);
93
94 /**
95 * @brief stop all fetch operations
96 */
97 void
98 cancel();
99
100private:
101 /**
102 * @brief fetch the next segment that has not been requested yet
103 *
104 * @return false if there is an error or all the segments have been fetched, true otherwise
105 */
106 bool
107 fetchNextSegment(size_t pipeNo);
108
109 void
110 fail(const std::string& reason);
111
112 void
113 handleData(const Interest& interest, const Data& data, size_t pipeNo);
114
115 void
116 handleFail(const std::string& reason, size_t pipeNo);
117
118private:
119 Name m_prefix;
120 Face& m_face;
121 uint64_t m_nextSegmentNo;
122 uint64_t m_lastSegmentNo;
123 uint64_t m_excludeSegmentNo;
124 DataCallback m_onData;
125 FailureCallback m_onFailure;
126 const Options m_options;
127 std::vector<std::pair<shared_ptr<DataFetcher>, uint64_t>> m_segmentFetchers;
128 bool m_hasFinalBlockId;
129 /**
130 * true if there's a critical error
131 */
132 bool m_hasError;
133 /**
134 * true if one or more segmentFetcher failed, if lastSegmentNo is not set this is usually not a
135 * fatal error for the pipeline
136 */
137 bool m_hasFailure;
138};
139
140} // namespace chunks
141} // namespace ndn
142
143#endif // NDN_TOOLS_CHUNKS_CATCHUNKS_PIPELINE_INTERESTS_HPP