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