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 | /* |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016-2019, 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 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 36 | PipelineInterestsFixed::PipelineInterestsFixed(Face& face, const Options& options) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 37 | : PipelineInterests(face) |
| 38 | , m_options(options) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 39 | , m_hasFailure(false) |
| 40 | { |
| 41 | m_segmentFetchers.resize(m_options.maxPipelineSize); |
| 42 | } |
| 43 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 44 | PipelineInterestsFixed::~PipelineInterestsFixed() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 45 | { |
| 46 | cancel(); |
| 47 | } |
| 48 | |
| 49 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 50 | PipelineInterestsFixed::doRun() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 51 | { |
| 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 | |
| 62 | bool |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 63 | PipelineInterestsFixed::fetchNextSegment(std::size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 64 | { |
| 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 Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 73 | uint64_t nextSegmentNo = getNextSegmentNo(); |
| 74 | if (m_hasFinalBlockId && nextSegmentNo > m_lastSegmentNo) |
| 75 | return false; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 76 | |
| 77 | // send interest for next segment |
| 78 | if (m_options.isVerbose) |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 79 | std::cerr << "Requesting segment #" << nextSegmentNo << std::endl; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 80 | |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 81 | auto interest = Interest() |
| 82 | .setName(Name(m_prefix).appendSegment(nextSegmentNo)) |
| 83 | .setCanBePrefix(false) |
| 84 | .setMustBeFresh(m_options.mustBeFresh) |
| 85 | .setInterestLifetime(m_options.interestLifetime); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 86 | |
| 87 | auto fetcher = DataFetcher::fetch(m_face, interest, |
| 88 | m_options.maxRetriesOnTimeoutOrNack, |
| 89 | m_options.maxRetriesOnTimeoutOrNack, |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 90 | bind(&PipelineInterestsFixed::handleData, this, _1, _2, pipeNo), |
| 91 | bind(&PipelineInterestsFixed::handleFail, this, _2, pipeNo), |
| 92 | bind(&PipelineInterestsFixed::handleFail, this, _2, pipeNo), |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 93 | m_options.isVerbose); |
| 94 | |
| 95 | BOOST_ASSERT(!m_segmentFetchers[pipeNo].first || !m_segmentFetchers[pipeNo].first->isRunning()); |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 96 | m_segmentFetchers[pipeNo] = make_pair(fetcher, nextSegmentNo); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 102 | PipelineInterestsFixed::doCancel() |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 103 | { |
| 104 | for (auto& fetcher : m_segmentFetchers) { |
| 105 | if (fetcher.first) |
| 106 | fetcher.first->cancel(); |
| 107 | } |
| 108 | |
| 109 | m_segmentFetchers.clear(); |
| 110 | } |
| 111 | |
| 112 | void |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 113 | PipelineInterestsFixed::handleData(const Interest& interest, const Data& data, size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 114 | { |
| 115 | if (isStopping()) |
| 116 | return; |
| 117 | |
Davide Pesavento | 84d8477 | 2019-04-07 14:40:07 -0400 | [diff] [blame] | 118 | // Interest was expressed with CanBePrefix=false |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 119 | BOOST_ASSERT(data.getName().equals(interest.getName())); |
| 120 | |
| 121 | if (m_options.isVerbose) |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 122 | std::cerr << "Received segment #" << getSegmentFromPacket(data) << std::endl; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 123 | |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 124 | onData(data); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 125 | |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 126 | if (!m_hasFinalBlockId && data.getFinalBlock()) { |
| 127 | m_lastSegmentNo = data.getFinalBlock()->toSegment(); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 128 | m_hasFinalBlockId = true; |
| 129 | |
| 130 | for (auto& fetcher : m_segmentFetchers) { |
| 131 | if (fetcher.first == nullptr) |
| 132 | continue; |
| 133 | |
| 134 | if (fetcher.second > m_lastSegmentNo) { |
| 135 | // stop trying to fetch segments that are beyond m_lastSegmentNo |
| 136 | fetcher.first->cancel(); |
| 137 | } |
| 138 | else if (fetcher.first->hasError()) { // fetcher.second <= m_lastSegmentNo |
| 139 | // there was an error while fetching a segment that is part of the content |
| 140 | return onFailure("Failure retrieving segment #" + to_string(fetcher.second)); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 145 | if (allSegmentsReceived()) { |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 146 | if (!m_options.isQuiet) { |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 147 | printSummary(); |
| 148 | } |
| 149 | } |
| 150 | else { |
| 151 | fetchNextSegment(pipeNo); |
| 152 | } |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 153 | } |
| 154 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame] | 155 | void PipelineInterestsFixed::handleFail(const std::string& reason, std::size_t pipeNo) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 156 | { |
| 157 | if (isStopping()) |
| 158 | return; |
| 159 | |
| 160 | // if the failed segment is definitely part of the content, raise a fatal error |
| 161 | if (m_hasFinalBlockId && m_segmentFetchers[pipeNo].second <= m_lastSegmentNo) |
| 162 | return onFailure(reason); |
| 163 | |
| 164 | if (!m_hasFinalBlockId) { |
| 165 | bool areAllFetchersStopped = true; |
| 166 | for (auto& fetcher : m_segmentFetchers) { |
| 167 | if (fetcher.first == nullptr) |
| 168 | continue; |
| 169 | |
| 170 | // cancel fetching all segments that follow |
| 171 | if (fetcher.second > m_segmentFetchers[pipeNo].second) { |
| 172 | fetcher.first->cancel(); |
| 173 | } |
| 174 | else if (fetcher.first->isRunning()) { // fetcher.second <= m_segmentFetchers[pipeNo].second |
| 175 | areAllFetchersStopped = false; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (areAllFetchersStopped) { |
| 180 | onFailure("Fetching terminated but no final segment number has been found"); |
| 181 | } |
| 182 | else { |
| 183 | m_hasFailure = true; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | } // namespace chunks |
| 189 | } // namespace ndn |