blob: 6df6d2755593b289fbbd0c4730ece5cae74d5997 [file] [log] [blame]
Weiwei Liue4765012016-06-01 00:10:29 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +00002/*
schneiderklausd8197df2019-03-16 11:31:40 -07003 * 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.
Weiwei Liue4765012016-06-01 00:10:29 -07006 *
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 * @author Davide Pesavento
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000027 * @author Chavoosh Ghasemi
Weiwei Liue4765012016-06-01 00:10:29 -070028 */
29
schneiderklausd8197df2019-03-16 11:31:40 -070030#include "pipeline-interests-fixed.hpp"
Weiwei Liue4765012016-06-01 00:10:29 -070031#include "data-fetcher.hpp"
32
33namespace ndn {
34namespace chunks {
35
schneiderklausd8197df2019-03-16 11:31:40 -070036PipelineInterestsFixed::PipelineInterestsFixed(Face& face, const Options& options)
Weiwei Liue4765012016-06-01 00:10:29 -070037 : PipelineInterests(face)
38 , m_options(options)
Weiwei Liue4765012016-06-01 00:10:29 -070039 , m_hasFailure(false)
40{
41 m_segmentFetchers.resize(m_options.maxPipelineSize);
42}
43
schneiderklausd8197df2019-03-16 11:31:40 -070044PipelineInterestsFixed::~PipelineInterestsFixed()
Weiwei Liue4765012016-06-01 00:10:29 -070045{
46 cancel();
47}
48
49void
schneiderklausd8197df2019-03-16 11:31:40 -070050PipelineInterestsFixed::doRun()
Weiwei Liue4765012016-06-01 00:10:29 -070051{
52 // if the FinalBlockId is unknown, this could potentially request non-existent segments
53 for (size_t nRequestedSegments = 0;
54 nRequestedSegments < m_options.maxPipelineSize;
55 ++nRequestedSegments) {
56 if (!fetchNextSegment(nRequestedSegments))
57 // all segments have been requested
58 break;
59 }
60}
61
62bool
schneiderklausd8197df2019-03-16 11:31:40 -070063PipelineInterestsFixed::fetchNextSegment(std::size_t pipeNo)
Weiwei Liue4765012016-06-01 00:10:29 -070064{
65 if (isStopping())
66 return false;
67
68 if (m_hasFailure) {
69 onFailure("Fetching terminated but no final segment number has been found");
70 return false;
71 }
72
Davide Pesaventob587cfd2017-11-04 16:29:50 -040073 uint64_t nextSegmentNo = getNextSegmentNo();
74 if (m_hasFinalBlockId && nextSegmentNo > m_lastSegmentNo)
75 return false;
Weiwei Liue4765012016-06-01 00:10:29 -070076
77 // send interest for next segment
78 if (m_options.isVerbose)
Davide Pesaventob587cfd2017-11-04 16:29:50 -040079 std::cerr << "Requesting segment #" << nextSegmentNo << std::endl;
Weiwei Liue4765012016-06-01 00:10:29 -070080
Davide Pesaventob587cfd2017-11-04 16:29:50 -040081 Interest interest(Name(m_prefix).appendSegment(nextSegmentNo));
Weiwei Liue4765012016-06-01 00:10:29 -070082 interest.setInterestLifetime(m_options.interestLifetime);
83 interest.setMustBeFresh(m_options.mustBeFresh);
84 interest.setMaxSuffixComponents(1);
85
86 auto fetcher = DataFetcher::fetch(m_face, interest,
87 m_options.maxRetriesOnTimeoutOrNack,
88 m_options.maxRetriesOnTimeoutOrNack,
schneiderklausd8197df2019-03-16 11:31:40 -070089 bind(&PipelineInterestsFixed::handleData, this, _1, _2, pipeNo),
90 bind(&PipelineInterestsFixed::handleFail, this, _2, pipeNo),
91 bind(&PipelineInterestsFixed::handleFail, this, _2, pipeNo),
Weiwei Liue4765012016-06-01 00:10:29 -070092 m_options.isVerbose);
93
94 BOOST_ASSERT(!m_segmentFetchers[pipeNo].first || !m_segmentFetchers[pipeNo].first->isRunning());
Davide Pesaventob587cfd2017-11-04 16:29:50 -040095 m_segmentFetchers[pipeNo] = make_pair(fetcher, nextSegmentNo);
Weiwei Liue4765012016-06-01 00:10:29 -070096
97 return true;
98}
99
100void
schneiderklausd8197df2019-03-16 11:31:40 -0700101PipelineInterestsFixed::doCancel()
Weiwei Liue4765012016-06-01 00:10:29 -0700102{
103 for (auto& fetcher : m_segmentFetchers) {
104 if (fetcher.first)
105 fetcher.first->cancel();
106 }
107
108 m_segmentFetchers.clear();
109}
110
111void
schneiderklausd8197df2019-03-16 11:31:40 -0700112PipelineInterestsFixed::handleData(const Interest& interest, const Data& data, size_t pipeNo)
Weiwei Liue4765012016-06-01 00:10:29 -0700113{
114 if (isStopping())
115 return;
116
117 BOOST_ASSERT(data.getName().equals(interest.getName()));
118
119 if (m_options.isVerbose)
Davide Pesaventobf1c0692017-01-15 19:15:09 -0500120 std::cerr << "Received segment #" << getSegmentFromPacket(data) << std::endl;
Weiwei Liue4765012016-06-01 00:10:29 -0700121
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400122 onData(data);
Weiwei Liue4765012016-06-01 00:10:29 -0700123
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400124 if (!m_hasFinalBlockId && data.getFinalBlock()) {
125 m_lastSegmentNo = data.getFinalBlock()->toSegment();
Weiwei Liue4765012016-06-01 00:10:29 -0700126 m_hasFinalBlockId = true;
127
128 for (auto& fetcher : m_segmentFetchers) {
129 if (fetcher.first == nullptr)
130 continue;
131
132 if (fetcher.second > m_lastSegmentNo) {
133 // stop trying to fetch segments that are beyond m_lastSegmentNo
134 fetcher.first->cancel();
135 }
136 else if (fetcher.first->hasError()) { // fetcher.second <= m_lastSegmentNo
137 // there was an error while fetching a segment that is part of the content
138 return onFailure("Failure retrieving segment #" + to_string(fetcher.second));
139 }
140 }
141 }
142
Ryan Wickman2c9933c2018-06-12 11:51:51 -0500143 if (allSegmentsReceived()) {
Davide Pesaventof6991e12018-01-08 20:58:50 -0500144 if (!m_options.isQuiet) {
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000145 printSummary();
146 }
147 }
148 else {
149 fetchNextSegment(pipeNo);
150 }
Weiwei Liue4765012016-06-01 00:10:29 -0700151}
152
schneiderklausd8197df2019-03-16 11:31:40 -0700153void PipelineInterestsFixed::handleFail(const std::string& reason, std::size_t pipeNo)
Weiwei Liue4765012016-06-01 00:10:29 -0700154{
155 if (isStopping())
156 return;
157
158 // if the failed segment is definitely part of the content, raise a fatal error
159 if (m_hasFinalBlockId && m_segmentFetchers[pipeNo].second <= m_lastSegmentNo)
160 return onFailure(reason);
161
162 if (!m_hasFinalBlockId) {
163 bool areAllFetchersStopped = true;
164 for (auto& fetcher : m_segmentFetchers) {
165 if (fetcher.first == nullptr)
166 continue;
167
168 // cancel fetching all segments that follow
169 if (fetcher.second > m_segmentFetchers[pipeNo].second) {
170 fetcher.first->cancel();
171 }
172 else if (fetcher.first->isRunning()) { // fetcher.second <= m_segmentFetchers[pipeNo].second
173 areAllFetchersStopped = false;
174 }
175 }
176
177 if (areAllFetchersStopped) {
178 onFailure("Fetching terminated but no final segment number has been found");
179 }
180 else {
181 m_hasFailure = true;
182 }
183 }
184}
185
186} // namespace chunks
187} // namespace ndn