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" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 23 | #include "../encoding/buffer-stream.hpp" |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 24 | #include "../name-component.hpp" |
| 25 | #include "../lp/nack.hpp" |
| 26 | #include "../lp/nack-header.hpp" |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 29 | #include <cmath> |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace util { |
| 33 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 34 | const uint32_t SegmentFetcher::MAX_INTEREST_REEXPRESS = 3; |
| 35 | |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 36 | SegmentFetcher::SegmentFetcher(Face& face, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 37 | shared_ptr<security::v2::Validator> validator, |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 38 | const CompleteCallback& completeCallback, |
| 39 | const ErrorCallback& errorCallback) |
| 40 | : m_face(face) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 41 | , m_scheduler(m_face.getIoService()) |
| 42 | , m_validator(validator) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 43 | , m_completeCallback(completeCallback) |
| 44 | , m_errorCallback(errorCallback) |
| 45 | , m_buffer(make_shared<OBufferStream>()) |
| 46 | { |
| 47 | } |
| 48 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 49 | shared_ptr<SegmentFetcher> |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 50 | SegmentFetcher::fetch(Face& face, |
| 51 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 52 | security::v2::Validator& validator, |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 53 | const CompleteCallback& completeCallback, |
| 54 | const ErrorCallback& errorCallback) |
| 55 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 56 | shared_ptr<security::v2::Validator> validatorPtr(&validator, [] (security::v2::Validator*) {}); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 57 | return fetch(face, baseInterest, validatorPtr, completeCallback, errorCallback); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 60 | shared_ptr<SegmentFetcher> |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 61 | SegmentFetcher::fetch(Face& face, |
| 62 | const Interest& baseInterest, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 63 | shared_ptr<security::v2::Validator> validator, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 64 | const CompleteCallback& completeCallback, |
| 65 | const ErrorCallback& errorCallback) |
| 66 | { |
| 67 | shared_ptr<SegmentFetcher> fetcher(new SegmentFetcher(face, validator, completeCallback, |
| 68 | errorCallback)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 69 | |
| 70 | fetcher->fetchFirstSegment(baseInterest, fetcher); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 71 | |
| 72 | return fetcher; |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
| 76 | SegmentFetcher::fetchFirstSegment(const Interest& baseInterest, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 77 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 78 | { |
| 79 | Interest interest(baseInterest); |
| 80 | interest.setChildSelector(1); |
| 81 | interest.setMustBeFresh(true); |
| 82 | |
| 83 | m_face.expressInterest(interest, |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 84 | bind(&SegmentFetcher::afterSegmentReceivedCb, this, _1, _2, true, self), |
| 85 | bind(&SegmentFetcher::afterNackReceivedCb, this, _1, _2, 0, self), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 86 | bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout")); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | SegmentFetcher::fetchNextSegment(const Interest& origInterest, const Name& dataName, |
| 91 | uint64_t segmentNo, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 92 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 93 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 94 | Interest interest(origInterest); // to preserve any selectors |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 95 | interest.refreshNonce(); |
| 96 | interest.setChildSelector(0); |
| 97 | interest.setMustBeFresh(false); |
| 98 | interest.setName(dataName.getPrefix(-1).appendSegment(segmentNo)); |
| 99 | m_face.expressInterest(interest, |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 100 | bind(&SegmentFetcher::afterSegmentReceivedCb, this, _1, _2, false, self), |
| 101 | bind(&SegmentFetcher::afterNackReceivedCb, this, _1, _2, 0, self), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 102 | bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout")); |
| 103 | } |
| 104 | |
| 105 | void |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 106 | SegmentFetcher::afterSegmentReceivedCb(const Interest& origInterest, |
| 107 | const Data& data, bool isSegmentZeroExpected, |
| 108 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 109 | { |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 110 | afterSegmentReceived(data); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 111 | m_validator->validate(data, |
| 112 | bind(&SegmentFetcher::afterValidationSuccess, this, _1, |
| 113 | isSegmentZeroExpected, origInterest, self), |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 114 | bind(&SegmentFetcher::afterValidationFailure, this, _1, _2)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 115 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 116 | } |
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 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 119 | SegmentFetcher::afterValidationSuccess(const Data& data, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 120 | bool isSegmentZeroExpected, |
| 121 | const Interest& origInterest, |
| 122 | shared_ptr<SegmentFetcher> self) |
| 123 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 124 | name::Component currentSegment = data.getName().get(-1); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 125 | |
| 126 | if (currentSegment.isSegment()) { |
| 127 | if (isSegmentZeroExpected && currentSegment.toSegment() != 0) { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 128 | fetchNextSegment(origInterest, data.getName(), 0, self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 129 | } |
| 130 | else { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 131 | m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()), |
| 132 | data.getContent().value_size()); |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 133 | afterSegmentValidated(data); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 134 | const auto& finalBlockId = data.getFinalBlock(); |
| 135 | if (!finalBlockId || (*finalBlockId > currentSegment)) { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 136 | fetchNextSegment(origInterest, data.getName(), currentSegment.toSegment() + 1, self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 137 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 138 | else { |
| 139 | return m_completeCallback(m_buffer->buf()); |
| 140 | } |
| 141 | } |
| 142 | } |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 143 | else { |
| 144 | m_errorCallback(DATA_HAS_NO_SEGMENT, "Data Name has no segment number."); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 148 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 149 | SegmentFetcher::afterValidationFailure(const Data& data, const security::v2::ValidationError& error) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 150 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 151 | return m_errorCallback(SEGMENT_VALIDATION_FAIL, "Segment validation fail " + |
| 152 | boost::lexical_cast<std::string>(error)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | void |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 157 | SegmentFetcher::afterNackReceivedCb(const Interest& origInterest, const lp::Nack& nack, |
| 158 | uint32_t reExpressCount, shared_ptr<SegmentFetcher> self) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 159 | { |
| 160 | if (reExpressCount >= MAX_INTEREST_REEXPRESS) { |
| 161 | m_errorCallback(NACK_ERROR, "Nack Error"); |
| 162 | } |
| 163 | else { |
| 164 | switch (nack.getReason()) { |
| 165 | case lp::NackReason::DUPLICATE: |
| 166 | reExpressInterest(origInterest, reExpressCount, self); |
| 167 | break; |
| 168 | case lp::NackReason::CONGESTION: |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 169 | using ms = time::milliseconds; |
| 170 | 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] | 171 | bind(&SegmentFetcher::reExpressInterest, this, |
| 172 | origInterest, reExpressCount, self)); |
| 173 | break; |
| 174 | default: |
| 175 | m_errorCallback(NACK_ERROR, "Nack Error"); |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | SegmentFetcher::reExpressInterest(Interest interest, uint32_t reExpressCount, |
| 183 | shared_ptr<SegmentFetcher> self) |
| 184 | { |
| 185 | interest.refreshNonce(); |
| 186 | BOOST_ASSERT(interest.hasNonce()); |
| 187 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 188 | bool isSegmentZeroExpected = true; |
| 189 | if (!interest.getName().empty()) { |
| 190 | name::Component lastComponent = interest.getName().get(-1); |
| 191 | isSegmentZeroExpected = !lastComponent.isSegment(); |
| 192 | } |
| 193 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 194 | m_face.expressInterest(interest, |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 195 | bind(&SegmentFetcher::afterSegmentReceivedCb, this, _1, _2, |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 196 | isSegmentZeroExpected, self), |
Muktadir Chowdhury | 1c109b4 | 2018-01-10 08:36:00 +0000 | [diff] [blame] | 197 | bind(&SegmentFetcher::afterNackReceivedCb, this, _1, _2, |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 198 | ++reExpressCount, self), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 199 | bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout")); |
| 200 | } |
| 201 | |
| 202 | } // namespace util |
| 203 | } // namespace ndn |