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 | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2016-2022, 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 | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 33 | namespace ndn::chunks { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 35 | PipelineInterestsFixed::PipelineInterestsFixed(Face& face, const Options& opts) |
| 36 | : PipelineInterests(face, opts) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 37 | { |
| 38 | m_segmentFetchers.resize(m_options.maxPipelineSize); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 39 | |
| 40 | if (m_options.isVerbose) { |
| 41 | printOptions(); |
| 42 | std::cerr << "\tPipeline size = " << m_options.maxPipelineSize << "\n"; |
| 43 | } |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 44 | } |
| 45 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 46 | PipelineInterestsFixed::~PipelineInterestsFixed() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 47 | { |
| 48 | cancel(); |
| 49 | } |
| 50 | |
| 51 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 52 | PipelineInterestsFixed::doRun() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 53 | { |
| 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 | |
| 64 | bool |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 65 | PipelineInterestsFixed::fetchNextSegment(std::size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 66 | { |
| 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 Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 75 | uint64_t nextSegmentNo = getNextSegmentNo(); |
| 76 | if (m_hasFinalBlockId && nextSegmentNo > m_lastSegmentNo) |
| 77 | return false; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 78 | |
| 79 | // send interest for next segment |
| 80 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 81 | std::cerr << "Requesting segment #" << nextSegmentNo << "\n"; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 82 | |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 83 | auto interest = Interest() |
| 84 | .setName(Name(m_prefix).appendSegment(nextSegmentNo)) |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 85 | .setMustBeFresh(m_options.mustBeFresh) |
| 86 | .setInterestLifetime(m_options.interestLifetime); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 87 | |
| 88 | auto fetcher = DataFetcher::fetch(m_face, interest, |
| 89 | m_options.maxRetriesOnTimeoutOrNack, |
| 90 | m_options.maxRetriesOnTimeoutOrNack, |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 91 | [=] (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 Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 100 | m_options.isVerbose); |
| 101 | |
| 102 | BOOST_ASSERT(!m_segmentFetchers[pipeNo].first || !m_segmentFetchers[pipeNo].first->isRunning()); |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 103 | m_segmentFetchers[pipeNo] = make_pair(fetcher, nextSegmentNo); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 109 | PipelineInterestsFixed::doCancel() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 110 | { |
| 111 | for (auto& fetcher : m_segmentFetchers) { |
| 112 | if (fetcher.first) |
| 113 | fetcher.first->cancel(); |
| 114 | } |
| 115 | |
| 116 | m_segmentFetchers.clear(); |
| 117 | } |
| 118 | |
| 119 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 120 | PipelineInterestsFixed::handleData(const Interest& interest, const Data& data, size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 121 | { |
| 122 | if (isStopping()) |
| 123 | return; |
| 124 | |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 125 | // Interest was expressed with CanBePrefix=false |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 126 | BOOST_ASSERT(data.getName().equals(interest.getName())); |
| 127 | |
| 128 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 129 | std::cerr << "Received segment #" << getSegmentFromPacket(data) << "\n"; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 130 | |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 131 | onData(data); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 132 | |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 133 | if (!m_hasFinalBlockId && data.getFinalBlock()) { |
| 134 | m_lastSegmentNo = data.getFinalBlock()->toSegment(); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 135 | 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 Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 152 | if (allSegmentsReceived()) { |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 153 | if (!m_options.isQuiet) { |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 154 | printSummary(); |
| 155 | } |
| 156 | } |
| 157 | else { |
| 158 | fetchNextSegment(pipeNo); |
| 159 | } |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 160 | } |
| 161 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 162 | void PipelineInterestsFixed::handleFail(const std::string& reason, std::size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 163 | { |
| 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 Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 195 | } // namespace ndn::chunks |