Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 14 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "status-dataset-context.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace mgmt { |
| 26 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 27 | const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD = 1_s; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 28 | |
| 29 | const Name& |
| 30 | StatusDatasetContext::getPrefix() const |
| 31 | { |
| 32 | return m_prefix; |
| 33 | } |
| 34 | |
| 35 | StatusDatasetContext& |
| 36 | StatusDatasetContext::setPrefix(const Name& prefix) |
| 37 | { |
| 38 | if (!m_interest.getName().isPrefixOf(prefix)) { |
| 39 | BOOST_THROW_EXCEPTION(std::invalid_argument("prefix does not start with Interest Name")); |
| 40 | } |
| 41 | |
| 42 | if (m_state != State::INITIAL) { |
| 43 | BOOST_THROW_EXCEPTION(std::domain_error("state is not in INITIAL")); |
| 44 | } |
| 45 | |
| 46 | m_prefix = prefix; |
| 47 | |
| 48 | if (!m_prefix[-1].isVersion()) { |
| 49 | m_prefix.appendVersion(); |
| 50 | } |
| 51 | |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | const time::milliseconds& |
| 56 | StatusDatasetContext::getExpiry() const |
| 57 | { |
| 58 | return m_expiry; |
| 59 | } |
| 60 | |
| 61 | StatusDatasetContext& |
| 62 | StatusDatasetContext::setExpiry(const time::milliseconds& expiry) |
| 63 | { |
| 64 | m_expiry = expiry; |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | StatusDatasetContext::append(const Block& block) |
| 70 | { |
| 71 | if (m_state == State::FINALIZED) { |
| 72 | BOOST_THROW_EXCEPTION(std::domain_error("state is in FINALIZED")); |
| 73 | } |
| 74 | |
| 75 | m_state = State::RESPONDED; |
| 76 | |
| 77 | size_t nBytesLeft = block.size(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 78 | while (nBytesLeft > 0) { |
| 79 | size_t nBytesAppend = std::min(nBytesLeft, |
| 80 | (ndn::MAX_NDN_PACKET_SIZE >> 1) - m_buffer->size()); |
| 81 | m_buffer->appendByteArray(block.wire() + (block.size() - nBytesLeft), nBytesAppend); |
| 82 | nBytesLeft -= nBytesAppend; |
| 83 | |
| 84 | if (nBytesLeft > 0) { |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 85 | m_dataSender(Name(m_prefix).appendSegment(m_segmentNo++), |
| 86 | makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()), |
| 87 | m_expiry, false); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 88 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 89 | m_buffer = make_shared<EncodingBuffer>(); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | StatusDatasetContext::end() |
| 96 | { |
| 97 | if (m_state == State::FINALIZED) { |
| 98 | BOOST_THROW_EXCEPTION(std::domain_error("state is in FINALIZED")); |
| 99 | } |
| 100 | |
| 101 | m_state = State::FINALIZED; |
| 102 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 103 | m_dataSender(Name(m_prefix).appendSegment(m_segmentNo), |
| 104 | makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()), |
| 105 | m_expiry, true); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | StatusDatasetContext::reject(const ControlResponse& resp /*= a ControlResponse with 400*/) |
| 110 | { |
| 111 | if (m_state != State::INITIAL) { |
| 112 | BOOST_THROW_EXCEPTION(std::domain_error("state is in REPONSED or FINALIZED")); |
| 113 | } |
| 114 | |
| 115 | m_state = State::FINALIZED; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 116 | m_nackSender(resp); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | StatusDatasetContext::StatusDatasetContext(const Interest& interest, |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 120 | const DataSender& dataSender, |
| 121 | const NackSender& nackSender) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 122 | : m_interest(interest) |
| 123 | , m_dataSender(dataSender) |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 124 | , m_nackSender(nackSender) |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 125 | , m_expiry(DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD) |
| 126 | , m_buffer(make_shared<EncodingBuffer>()) |
| 127 | , m_segmentNo(0) |
| 128 | , m_state(State::INITIAL) |
| 129 | { |
| 130 | setPrefix(interest.getName()); |
| 131 | } |
| 132 | |
| 133 | } // namespace mgmt |
| 134 | } // namespace ndn |