Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, Regents of the University of California, |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 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 | * @author Davide Pesavento |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 27 | * @author Chavoosh Ghasemi |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 30 | #include "pipeline-interests-fixed.hpp" |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 31 | #include "data-fetcher.hpp" |
| 32 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 33 | #include <iostream> |
| 34 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 35 | namespace ndn::chunks { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 37 | PipelineInterestsFixed::PipelineInterestsFixed(Face& face, const Options& opts) |
| 38 | : PipelineInterests(face, opts) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 39 | { |
| 40 | m_segmentFetchers.resize(m_options.maxPipelineSize); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 41 | |
| 42 | if (m_options.isVerbose) { |
| 43 | printOptions(); |
| 44 | std::cerr << "\tPipeline size = " << m_options.maxPipelineSize << "\n"; |
| 45 | } |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 46 | } |
| 47 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 48 | PipelineInterestsFixed::~PipelineInterestsFixed() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 49 | { |
| 50 | cancel(); |
| 51 | } |
| 52 | |
| 53 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 54 | PipelineInterestsFixed::doRun() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 55 | { |
| 56 | // if the FinalBlockId is unknown, this could potentially request non-existent segments |
| 57 | for (size_t nRequestedSegments = 0; |
| 58 | nRequestedSegments < m_options.maxPipelineSize; |
| 59 | ++nRequestedSegments) { |
| 60 | if (!fetchNextSegment(nRequestedSegments)) |
| 61 | // all segments have been requested |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | bool |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 67 | PipelineInterestsFixed::fetchNextSegment(std::size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 68 | { |
| 69 | if (isStopping()) |
| 70 | return false; |
| 71 | |
| 72 | if (m_hasFailure) { |
| 73 | onFailure("Fetching terminated but no final segment number has been found"); |
| 74 | return false; |
| 75 | } |
| 76 | |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 77 | uint64_t nextSegmentNo = getNextSegmentNo(); |
| 78 | if (m_hasFinalBlockId && nextSegmentNo > m_lastSegmentNo) |
| 79 | return false; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 80 | |
| 81 | // send interest for next segment |
| 82 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 83 | std::cerr << "Requesting segment #" << nextSegmentNo << "\n"; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 84 | |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 85 | auto interest = Interest() |
| 86 | .setName(Name(m_prefix).appendSegment(nextSegmentNo)) |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 87 | .setMustBeFresh(m_options.mustBeFresh) |
| 88 | .setInterestLifetime(m_options.interestLifetime); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 89 | |
| 90 | auto fetcher = DataFetcher::fetch(m_face, interest, |
| 91 | m_options.maxRetriesOnTimeoutOrNack, |
| 92 | m_options.maxRetriesOnTimeoutOrNack, |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 93 | [this, pipeNo] (const auto& interest, const auto& data) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 94 | handleData(interest, data, pipeNo); |
| 95 | }, |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 96 | [this, pipeNo] (const auto&, const auto& reason) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 97 | handleFail(reason, pipeNo); |
| 98 | }, |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 99 | [this, pipeNo] (const auto&, const auto& reason) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 100 | handleFail(reason, pipeNo); |
| 101 | }, |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 102 | m_options.isVerbose); |
| 103 | |
| 104 | BOOST_ASSERT(!m_segmentFetchers[pipeNo].first || !m_segmentFetchers[pipeNo].first->isRunning()); |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 105 | m_segmentFetchers[pipeNo] = make_pair(fetcher, nextSegmentNo); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 111 | PipelineInterestsFixed::doCancel() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 112 | { |
| 113 | for (auto& fetcher : m_segmentFetchers) { |
| 114 | if (fetcher.first) |
| 115 | fetcher.first->cancel(); |
| 116 | } |
| 117 | |
| 118 | m_segmentFetchers.clear(); |
| 119 | } |
| 120 | |
| 121 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 122 | PipelineInterestsFixed::handleData(const Interest& interest, const Data& data, size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 123 | { |
| 124 | if (isStopping()) |
| 125 | return; |
| 126 | |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 127 | // Interest was expressed with CanBePrefix=false |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 128 | BOOST_ASSERT(data.getName().equals(interest.getName())); |
| 129 | |
| 130 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 131 | std::cerr << "Received segment #" << getSegmentFromPacket(data) << "\n"; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 132 | |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 133 | onData(data); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 134 | |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 135 | if (!m_hasFinalBlockId && data.getFinalBlock()) { |
| 136 | m_lastSegmentNo = data.getFinalBlock()->toSegment(); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 137 | m_hasFinalBlockId = true; |
| 138 | |
| 139 | for (auto& fetcher : m_segmentFetchers) { |
| 140 | if (fetcher.first == nullptr) |
| 141 | continue; |
| 142 | |
| 143 | if (fetcher.second > m_lastSegmentNo) { |
| 144 | // stop trying to fetch segments that are beyond m_lastSegmentNo |
| 145 | fetcher.first->cancel(); |
| 146 | } |
| 147 | else if (fetcher.first->hasError()) { // fetcher.second <= m_lastSegmentNo |
| 148 | // there was an error while fetching a segment that is part of the content |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 149 | return onFailure("Failure retrieving segment #" + std::to_string(fetcher.second)); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 154 | if (allSegmentsReceived()) { |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 155 | if (!m_options.isQuiet) { |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 156 | printSummary(); |
| 157 | } |
| 158 | } |
| 159 | else { |
| 160 | fetchNextSegment(pipeNo); |
| 161 | } |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 162 | } |
| 163 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 164 | void PipelineInterestsFixed::handleFail(const std::string& reason, std::size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 165 | { |
| 166 | if (isStopping()) |
| 167 | return; |
| 168 | |
| 169 | // if the failed segment is definitely part of the content, raise a fatal error |
| 170 | if (m_hasFinalBlockId && m_segmentFetchers[pipeNo].second <= m_lastSegmentNo) |
| 171 | return onFailure(reason); |
| 172 | |
| 173 | if (!m_hasFinalBlockId) { |
| 174 | bool areAllFetchersStopped = true; |
| 175 | for (auto& fetcher : m_segmentFetchers) { |
| 176 | if (fetcher.first == nullptr) |
| 177 | continue; |
| 178 | |
| 179 | // cancel fetching all segments that follow |
| 180 | if (fetcher.second > m_segmentFetchers[pipeNo].second) { |
| 181 | fetcher.first->cancel(); |
| 182 | } |
| 183 | else if (fetcher.first->isRunning()) { // fetcher.second <= m_segmentFetchers[pipeNo].second |
| 184 | areAllFetchersStopped = false; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (areAllFetchersStopped) { |
| 189 | onFailure("Fetching terminated but no final segment number has been found"); |
| 190 | } |
| 191 | else { |
| 192 | m_hasFailure = true; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 197 | } // namespace ndn::chunks |