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 | |
| 49 | void |
| 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*) {}); |
| 57 | fetch(face, baseInterest, validatorPtr, completeCallback, errorCallback); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 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); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | SegmentFetcher::fetchFirstSegment(const Interest& baseInterest, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 75 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 76 | { |
| 77 | Interest interest(baseInterest); |
| 78 | interest.setChildSelector(1); |
| 79 | interest.setMustBeFresh(true); |
| 80 | |
| 81 | m_face.expressInterest(interest, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 82 | bind(&SegmentFetcher::afterSegmentReceived, this, _1, _2, true, self), |
| 83 | bind(&SegmentFetcher::afterNackReceived, this, _1, _2, 0, self), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 84 | bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout")); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | SegmentFetcher::fetchNextSegment(const Interest& origInterest, const Name& dataName, |
| 89 | uint64_t segmentNo, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 90 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 91 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 92 | Interest interest(origInterest); // to preserve any selectors |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 93 | interest.refreshNonce(); |
| 94 | interest.setChildSelector(0); |
| 95 | interest.setMustBeFresh(false); |
| 96 | interest.setName(dataName.getPrefix(-1).appendSegment(segmentNo)); |
| 97 | m_face.expressInterest(interest, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 98 | bind(&SegmentFetcher::afterSegmentReceived, this, _1, _2, false, self), |
| 99 | bind(&SegmentFetcher::afterNackReceived, this, _1, _2, 0, self), |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 100 | bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout")); |
| 101 | } |
| 102 | |
| 103 | void |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 104 | SegmentFetcher::afterSegmentReceived(const Interest& origInterest, |
| 105 | const Data& data, bool isSegmentZeroExpected, |
| 106 | shared_ptr<SegmentFetcher> self) |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 107 | { |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 108 | m_validator->validate(data, |
| 109 | bind(&SegmentFetcher::afterValidationSuccess, this, _1, |
| 110 | isSegmentZeroExpected, origInterest, self), |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 111 | bind(&SegmentFetcher::afterValidationFailure, this, _1, _2)); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 112 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 113 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 114 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 115 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 116 | SegmentFetcher::afterValidationSuccess(const Data& data, |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 117 | bool isSegmentZeroExpected, |
| 118 | const Interest& origInterest, |
| 119 | shared_ptr<SegmentFetcher> self) |
| 120 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 121 | name::Component currentSegment = data.getName().get(-1); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 122 | |
| 123 | if (currentSegment.isSegment()) { |
| 124 | if (isSegmentZeroExpected && currentSegment.toSegment() != 0) { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 125 | fetchNextSegment(origInterest, data.getName(), 0, self); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 126 | } |
| 127 | else { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 128 | m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()), |
| 129 | data.getContent().value_size()); |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 131 | const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId(); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 132 | if (finalBlockId.empty() || (finalBlockId > currentSegment)) { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 133 | fetchNextSegment(origInterest, data.getName(), currentSegment.toSegment() + 1, self); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 134 | } |
Alexander Afanasyev | f3cfab5 | 2014-08-17 22:15:25 -0700 | [diff] [blame] | 135 | else { |
| 136 | return m_completeCallback(m_buffer->buf()); |
| 137 | } |
| 138 | } |
| 139 | } |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 140 | else { |
| 141 | m_errorCallback(DATA_HAS_NO_SEGMENT, "Data Name has no segment number."); |
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 | void |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 146 | SegmentFetcher::afterValidationFailure(const Data& data, const security::v2::ValidationError& error) |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 147 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 148 | return m_errorCallback(SEGMENT_VALIDATION_FAIL, "Segment validation fail " + |
| 149 | boost::lexical_cast<std::string>(error)); |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | void |
| 154 | SegmentFetcher::afterNackReceived(const Interest& origInterest, const lp::Nack& nack, |
| 155 | uint32_t reExpressCount, shared_ptr<SegmentFetcher> self) |
| 156 | { |
| 157 | if (reExpressCount >= MAX_INTEREST_REEXPRESS) { |
| 158 | m_errorCallback(NACK_ERROR, "Nack Error"); |
| 159 | } |
| 160 | else { |
| 161 | switch (nack.getReason()) { |
| 162 | case lp::NackReason::DUPLICATE: |
| 163 | reExpressInterest(origInterest, reExpressCount, self); |
| 164 | break; |
| 165 | case lp::NackReason::CONGESTION: |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 166 | using ms = time::milliseconds; |
| 167 | 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] | 168 | bind(&SegmentFetcher::reExpressInterest, this, |
| 169 | origInterest, reExpressCount, self)); |
| 170 | break; |
| 171 | default: |
| 172 | m_errorCallback(NACK_ERROR, "Nack Error"); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | SegmentFetcher::reExpressInterest(Interest interest, uint32_t reExpressCount, |
| 180 | shared_ptr<SegmentFetcher> self) |
| 181 | { |
| 182 | interest.refreshNonce(); |
| 183 | BOOST_ASSERT(interest.hasNonce()); |
| 184 | |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 185 | bool isSegmentZeroExpected = true; |
| 186 | if (!interest.getName().empty()) { |
| 187 | name::Component lastComponent = interest.getName().get(-1); |
| 188 | isSegmentZeroExpected = !lastComponent.isSegment(); |
| 189 | } |
| 190 | |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 191 | m_face.expressInterest(interest, |
Muktadir R Chowdhury | 2bc2df0 | 2016-04-05 16:55:41 -0500 | [diff] [blame] | 192 | bind(&SegmentFetcher::afterSegmentReceived, this, _1, _2, |
| 193 | isSegmentZeroExpected, self), |
| 194 | bind(&SegmentFetcher::afterNackReceived, this, _1, _2, |
| 195 | ++reExpressCount, self), |
Muktadir R Chowdhury | f58f8f4 | 2015-09-02 11:56:49 -0500 | [diff] [blame] | 196 | bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout")); |
| 197 | } |
| 198 | |
| 199 | } // namespace util |
| 200 | } // namespace ndn |