blob: a80ca5eb3651b8cc6113f792753c0291d6f16604 [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/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, 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
Davide Pesaventob3570c62022-02-19 19:19:00 -050033namespace ndn::chunks {
Weiwei Liue4765012016-06-01 00:10:29 -070034
Davide Pesavento97a33b22019-10-17 22:10:47 -040035PipelineInterestsFixed::PipelineInterestsFixed(Face& face, const Options& opts)
36 : PipelineInterests(face, opts)
Weiwei Liue4765012016-06-01 00:10:29 -070037{
38 m_segmentFetchers.resize(m_options.maxPipelineSize);
Davide Pesavento97a33b22019-10-17 22:10:47 -040039
40 if (m_options.isVerbose) {
41 printOptions();
42 std::cerr << "\tPipeline size = " << m_options.maxPipelineSize << "\n";
43 }
Weiwei Liue4765012016-06-01 00:10:29 -070044}
45
schneiderklausd8197df2019-03-16 11:31:40 -070046PipelineInterestsFixed::~PipelineInterestsFixed()
Weiwei Liue4765012016-06-01 00:10:29 -070047{
48 cancel();
49}
50
51void
schneiderklausd8197df2019-03-16 11:31:40 -070052PipelineInterestsFixed::doRun()
Weiwei Liue4765012016-06-01 00:10:29 -070053{
54 // if the FinalBlockId is unknown, this could potentially request non-existent segments
55 for (size_t nRequestedSegments = 0;
56 nRequestedSegments < m_options.maxPipelineSize;
57 ++nRequestedSegments) {
58 if (!fetchNextSegment(nRequestedSegments))
59 // all segments have been requested
60 break;
61 }
62}
63
64bool
schneiderklausd8197df2019-03-16 11:31:40 -070065PipelineInterestsFixed::fetchNextSegment(std::size_t pipeNo)
Weiwei Liue4765012016-06-01 00:10:29 -070066{
67 if (isStopping())
68 return false;
69
70 if (m_hasFailure) {
71 onFailure("Fetching terminated but no final segment number has been found");
72 return false;
73 }
74
Davide Pesaventob587cfd2017-11-04 16:29:50 -040075 uint64_t nextSegmentNo = getNextSegmentNo();
76 if (m_hasFinalBlockId && nextSegmentNo > m_lastSegmentNo)
77 return false;
Weiwei Liue4765012016-06-01 00:10:29 -070078
79 // send interest for next segment
80 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040081 std::cerr << "Requesting segment #" << nextSegmentNo << "\n";
Weiwei Liue4765012016-06-01 00:10:29 -070082
Davide Pesavento84d84772019-04-07 14:40:07 -040083 auto interest = Interest()
84 .setName(Name(m_prefix).appendSegment(nextSegmentNo))
Davide Pesavento84d84772019-04-07 14:40:07 -040085 .setMustBeFresh(m_options.mustBeFresh)
86 .setInterestLifetime(m_options.interestLifetime);
Weiwei Liue4765012016-06-01 00:10:29 -070087
88 auto fetcher = DataFetcher::fetch(m_face, interest,
89 m_options.maxRetriesOnTimeoutOrNack,
90 m_options.maxRetriesOnTimeoutOrNack,
Davide Pesaventof8d9a532021-07-03 16:04:12 -040091 [=] (const auto& interest, const auto& data) {
92 handleData(interest, data, pipeNo);
93 },
94 [=] (const auto&, const auto& reason) {
95 handleFail(reason, pipeNo);
96 },
97 [=] (const auto&, const auto& reason) {
98 handleFail(reason, pipeNo);
99 },
Weiwei Liue4765012016-06-01 00:10:29 -0700100 m_options.isVerbose);
101
102 BOOST_ASSERT(!m_segmentFetchers[pipeNo].first || !m_segmentFetchers[pipeNo].first->isRunning());
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400103 m_segmentFetchers[pipeNo] = make_pair(fetcher, nextSegmentNo);
Weiwei Liue4765012016-06-01 00:10:29 -0700104
105 return true;
106}
107
108void
schneiderklausd8197df2019-03-16 11:31:40 -0700109PipelineInterestsFixed::doCancel()
Weiwei Liue4765012016-06-01 00:10:29 -0700110{
111 for (auto& fetcher : m_segmentFetchers) {
112 if (fetcher.first)
113 fetcher.first->cancel();
114 }
115
116 m_segmentFetchers.clear();
117}
118
119void
schneiderklausd8197df2019-03-16 11:31:40 -0700120PipelineInterestsFixed::handleData(const Interest& interest, const Data& data, size_t pipeNo)
Weiwei Liue4765012016-06-01 00:10:29 -0700121{
122 if (isStopping())
123 return;
124
Davide Pesavento84d84772019-04-07 14:40:07 -0400125 // Interest was expressed with CanBePrefix=false
Weiwei Liue4765012016-06-01 00:10:29 -0700126 BOOST_ASSERT(data.getName().equals(interest.getName()));
127
128 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400129 std::cerr << "Received segment #" << getSegmentFromPacket(data) << "\n";
Weiwei Liue4765012016-06-01 00:10:29 -0700130
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400131 onData(data);
Weiwei Liue4765012016-06-01 00:10:29 -0700132
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400133 if (!m_hasFinalBlockId && data.getFinalBlock()) {
134 m_lastSegmentNo = data.getFinalBlock()->toSegment();
Weiwei Liue4765012016-06-01 00:10:29 -0700135 m_hasFinalBlockId = true;
136
137 for (auto& fetcher : m_segmentFetchers) {
138 if (fetcher.first == nullptr)
139 continue;
140
141 if (fetcher.second > m_lastSegmentNo) {
142 // stop trying to fetch segments that are beyond m_lastSegmentNo
143 fetcher.first->cancel();
144 }
145 else if (fetcher.first->hasError()) { // fetcher.second <= m_lastSegmentNo
146 // there was an error while fetching a segment that is part of the content
147 return onFailure("Failure retrieving segment #" + to_string(fetcher.second));
148 }
149 }
150 }
151
Ryan Wickman2c9933c2018-06-12 11:51:51 -0500152 if (allSegmentsReceived()) {
Davide Pesaventof6991e12018-01-08 20:58:50 -0500153 if (!m_options.isQuiet) {
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000154 printSummary();
155 }
156 }
157 else {
158 fetchNextSegment(pipeNo);
159 }
Weiwei Liue4765012016-06-01 00:10:29 -0700160}
161
schneiderklausd8197df2019-03-16 11:31:40 -0700162void PipelineInterestsFixed::handleFail(const std::string& reason, std::size_t pipeNo)
Weiwei Liue4765012016-06-01 00:10:29 -0700163{
164 if (isStopping())
165 return;
166
167 // if the failed segment is definitely part of the content, raise a fatal error
168 if (m_hasFinalBlockId && m_segmentFetchers[pipeNo].second <= m_lastSegmentNo)
169 return onFailure(reason);
170
171 if (!m_hasFinalBlockId) {
172 bool areAllFetchersStopped = true;
173 for (auto& fetcher : m_segmentFetchers) {
174 if (fetcher.first == nullptr)
175 continue;
176
177 // cancel fetching all segments that follow
178 if (fetcher.second > m_segmentFetchers[pipeNo].second) {
179 fetcher.first->cancel();
180 }
181 else if (fetcher.first->isRunning()) { // fetcher.second <= m_segmentFetchers[pipeNo].second
182 areAllFetchersStopped = false;
183 }
184 }
185
186 if (areAllFetchersStopped) {
187 onFailure("Fetching terminated but no final segment number has been found");
188 }
189 else {
190 m_hasFailure = true;
191 }
192 }
193}
194
Davide Pesaventob3570c62022-02-19 19:19:00 -0500195} // namespace ndn::chunks