Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "segment-fetcher.hpp" |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 23 | #include "../name-component.hpp" |
| 24 | #include "../lp/nack.hpp" |
| 25 | #include "../lp/nack-header.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 28 | #include <cmath> |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace util { |
| 32 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 33 | const uint32_t SegmentFetcher::MAX_INTEREST_REEXPRESS = 3; |
| 34 | |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 35 | SegmentFetcher::SegmentFetcher(Face& face, security::v2::Validator& validator) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 36 | : m_face(face) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 37 | , m_scheduler(m_face.getIoService()) |
| 38 | , m_validator(validator) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 42 | shared_ptr<SegmentFetcher> |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 43 | SegmentFetcher::start(Face& face, |
| 44 | const Interest& baseInterest, |
| 45 | security::v2::Validator& validator) |
| 46 | { |
| 47 | shared_ptr<SegmentFetcher> fetcher(new SegmentFetcher(face, validator)); |
| 48 | fetcher->fetchFirstSegment(baseInterest, fetcher); |
| 49 | return fetcher; |
| 50 | } |
| 51 | |
| 52 | shared_ptr<SegmentFetcher> |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 53 | SegmentFetcher::fetch(Face& face, |
| 54 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 55 | security::v2::Validator& validator, |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 56 | const CompleteCallback& completeCallback, |
| 57 | const ErrorCallback& errorCallback) |
| 58 | { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 59 | shared_ptr<SegmentFetcher> fetcher = start(face, baseInterest, validator); |
| 60 | fetcher->onComplete.connect(completeCallback); |
| 61 | fetcher->onError.connect(errorCallback); |
| 62 | return fetcher; |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 65 | shared_ptr<SegmentFetcher> |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 66 | SegmentFetcher::fetch(Face& face, |
| 67 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 68 | shared_ptr<security::v2::Validator> validator, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 69 | const CompleteCallback& completeCallback, |
| 70 | const ErrorCallback& errorCallback) |
| 71 | { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 72 | auto fetcher = fetch(face, baseInterest, *validator, completeCallback, errorCallback); |
| 73 | fetcher->onComplete.connect([validator] (ConstBufferPtr) {}); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 74 | return fetcher; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
| 78 | SegmentFetcher::fetchFirstSegment(const Interest& baseInterest, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 79 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 80 | { |
| 81 | Interest interest(baseInterest); |
| 82 | interest.setChildSelector(1); |
| 83 | interest.setMustBeFresh(true); |
| 84 | |
| 85 | m_face.expressInterest(interest, |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 86 | bind(&SegmentFetcher::afterSegmentReceivedCb, this, _1, _2, true, self), |
| 87 | bind(&SegmentFetcher::afterNackReceivedCb, this, _1, _2, 0, self), |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 88 | bind([this] { onError(INTEREST_TIMEOUT, "Timeout"); })); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
| 92 | SegmentFetcher::fetchNextSegment(const Interest& origInterest, const Name& dataName, |
| 93 | uint64_t segmentNo, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 94 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 95 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 96 | Interest interest(origInterest); // to preserve any selectors |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 97 | interest.refreshNonce(); |
| 98 | interest.setChildSelector(0); |
| 99 | interest.setMustBeFresh(false); |
| 100 | interest.setName(dataName.getPrefix(-1).appendSegment(segmentNo)); |
| 101 | m_face.expressInterest(interest, |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 102 | bind(&SegmentFetcher::afterSegmentReceivedCb, this, _1, _2, false, self), |
| 103 | bind(&SegmentFetcher::afterNackReceivedCb, this, _1, _2, 0, self), |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 104 | bind([this] { onError(INTEREST_TIMEOUT, "Timeout"); })); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 108 | SegmentFetcher::afterSegmentReceivedCb(const Interest& origInterest, |
| 109 | const Data& data, bool isSegmentZeroExpected, |
| 110 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 111 | { |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 112 | afterSegmentReceived(data); |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 113 | m_validator.validate(data, |
| 114 | bind(&SegmentFetcher::afterValidationSuccess, this, _1, |
| 115 | isSegmentZeroExpected, origInterest, self), |
| 116 | bind(&SegmentFetcher::afterValidationFailure, this, _1, _2)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 117 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 118 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 119 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 120 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 121 | SegmentFetcher::afterValidationSuccess(const Data& data, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 122 | bool isSegmentZeroExpected, |
| 123 | const Interest& origInterest, |
| 124 | shared_ptr<SegmentFetcher> self) |
| 125 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 126 | name::Component currentSegment = data.getName().get(-1); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 127 | |
| 128 | if (currentSegment.isSegment()) { |
| 129 | if (isSegmentZeroExpected && currentSegment.toSegment() != 0) { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 130 | fetchNextSegment(origInterest, data.getName(), 0, self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 131 | } |
| 132 | else { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 133 | m_buffer.write(reinterpret_cast<const char*>(data.getContent().value()), |
| 134 | data.getContent().value_size()); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 135 | afterSegmentValidated(data); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 136 | const auto& finalBlockId = data.getFinalBlock(); |
| 137 | if (!finalBlockId || (*finalBlockId > currentSegment)) { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 138 | fetchNextSegment(origInterest, data.getName(), currentSegment.toSegment() + 1, self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 139 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 140 | else { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 141 | onComplete(m_buffer.buf()); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 145 | else { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 146 | onError(DATA_HAS_NO_SEGMENT, "Data Name has no segment number."); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 150 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 151 | SegmentFetcher::afterValidationFailure(const Data& data, const security::v2::ValidationError& error) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 152 | { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 153 | onError(SEGMENT_VALIDATION_FAIL, "Segment validation fail " + |
| 154 | boost::lexical_cast<std::string>(error)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | void |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 159 | SegmentFetcher::afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack, |
| 160 | uint32_t reExpressCount, shared_ptr<SegmentFetcher> self) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 161 | { |
| 162 | if (reExpressCount >= MAX_INTEREST_REEXPRESS) { |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 163 | onError(NACK_ERROR, "Nack Error"); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 164 | } |
| 165 | else { |
| 166 | switch (nack.getReason()) { |
| 167 | case lp::NackReason::DUPLICATE: |
| 168 | reExpressInterest(origInterest, reExpressCount, self); |
| 169 | break; |
| 170 | case lp::NackReason::CONGESTION: |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 171 | using ms = time::milliseconds; |
| 172 | m_scheduler.scheduleEvent(ms(static_cast<ms::rep>(std::pow(2, reExpressCount + 1))), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 173 | bind(&SegmentFetcher::reExpressInterest, this, |
| 174 | origInterest, reExpressCount, self)); |
| 175 | break; |
| 176 | default: |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 177 | onError(NACK_ERROR, "Nack Error"); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | SegmentFetcher::reExpressInterest(Interest interest, uint32_t reExpressCount, |
| 185 | shared_ptr<SegmentFetcher> self) |
| 186 | { |
| 187 | interest.refreshNonce(); |
| 188 | BOOST_ASSERT(interest.hasNonce()); |
| 189 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 190 | bool isSegmentZeroExpected = true; |
| 191 | if (!interest.getName().empty()) { |
| 192 | name::Component lastComponent = interest.getName().get(-1); |
| 193 | isSegmentZeroExpected = !lastComponent.isSegment(); |
| 194 | } |
| 195 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 196 | m_face.expressInterest(interest, |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 197 | bind(&SegmentFetcher::afterSegmentReceivedCb, this, _1, _2, |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 198 | isSegmentZeroExpected, self), |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 199 | bind(&SegmentFetcher::afterNackReceivedCb, this, _1, _2, |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 200 | ++reExpressCount, self), |
Eric Newberry | cc910cd | 2018-05-06 17:01:40 -0700 | [diff] [blame^] | 201 | bind([this] { onError(INTEREST_TIMEOUT, "Timeout"); })); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } // namespace util |
| 205 | } // namespace ndn |