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