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 | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2016-2021, 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 | |
| 33 | namespace ndn { |
| 34 | namespace chunks { |
| 35 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 36 | PipelineInterestsFixed::PipelineInterestsFixed(Face& face, const Options& opts) |
| 37 | : PipelineInterests(face, opts) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 38 | { |
| 39 | m_segmentFetchers.resize(m_options.maxPipelineSize); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 40 | |
| 41 | if (m_options.isVerbose) { |
| 42 | printOptions(); |
| 43 | std::cerr << "\tPipeline size = " << m_options.maxPipelineSize << "\n"; |
| 44 | } |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 45 | } |
| 46 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 47 | PipelineInterestsFixed::~PipelineInterestsFixed() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 48 | { |
| 49 | cancel(); |
| 50 | } |
| 51 | |
| 52 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 53 | PipelineInterestsFixed::doRun() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 54 | { |
| 55 | // if the FinalBlockId is unknown, this could potentially request non-existent segments |
| 56 | for (size_t nRequestedSegments = 0; |
| 57 | nRequestedSegments < m_options.maxPipelineSize; |
| 58 | ++nRequestedSegments) { |
| 59 | if (!fetchNextSegment(nRequestedSegments)) |
| 60 | // all segments have been requested |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 66 | PipelineInterestsFixed::fetchNextSegment(std::size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 67 | { |
| 68 | if (isStopping()) |
| 69 | return false; |
| 70 | |
| 71 | if (m_hasFailure) { |
| 72 | onFailure("Fetching terminated but no final segment number has been found"); |
| 73 | return false; |
| 74 | } |
| 75 | |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 76 | uint64_t nextSegmentNo = getNextSegmentNo(); |
| 77 | if (m_hasFinalBlockId && nextSegmentNo > m_lastSegmentNo) |
| 78 | return false; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 79 | |
| 80 | // send interest for next segment |
| 81 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame^] | 82 | std::cerr << "Requesting segment #" << nextSegmentNo << "\n"; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 83 | |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 84 | auto interest = Interest() |
| 85 | .setName(Name(m_prefix).appendSegment(nextSegmentNo)) |
| 86 | .setCanBePrefix(false) |
| 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 | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame^] | 93 | [=] (const auto& interest, const auto& data) { |
| 94 | handleData(interest, data, pipeNo); |
| 95 | }, |
| 96 | [=] (const auto&, const auto& reason) { |
| 97 | handleFail(reason, pipeNo); |
| 98 | }, |
| 99 | [=] (const auto&, const auto& reason) { |
| 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 |
| 149 | return onFailure("Failure retrieving segment #" + to_string(fetcher.second)); |
| 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 | |
| 197 | } // namespace chunks |
| 198 | } // namespace ndn |