blob: 2abbaa74c40e6b703e20de45547e5ff5ae1170e2 [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07004 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07006 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07007 * 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 Li8ee37ed2015-05-19 12:44:04 -070010 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070011 * 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 Li8ee37ed2015-05-19 12:44:04 -070014 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070015 * 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 Li8ee37ed2015-05-19 12:44:04 -070020 */
21
22#include "status-dataset-context.hpp"
23
24namespace ndn {
25namespace mgmt {
26
Davide Pesavento0f830802018-01-16 23:58:58 -050027const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD = 1_s;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070028
29const Name&
30StatusDatasetContext::getPrefix() const
31{
32 return m_prefix;
33}
34
35StatusDatasetContext&
36StatusDatasetContext::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
55const time::milliseconds&
56StatusDatasetContext::getExpiry() const
57{
58 return m_expiry;
59}
60
61StatusDatasetContext&
62StatusDatasetContext::setExpiry(const time::milliseconds& expiry)
63{
64 m_expiry = expiry;
65 return *this;
66}
67
68void
69StatusDatasetContext::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 Li8ee37ed2015-05-19 12:44:04 -070078 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 Li4b4f7542016-03-11 02:04:43 +080085 m_dataSender(Name(m_prefix).appendSegment(m_segmentNo++),
86 makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()),
87 m_expiry, false);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070088
89 m_buffer = std::make_shared<EncodingBuffer>();
90 }
91 }
92}
93
94void
95StatusDatasetContext::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 Li4b4f7542016-03-11 02:04:43 +0800103 m_dataSender(Name(m_prefix).appendSegment(m_segmentNo),
104 makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()),
105 m_expiry, true);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700106}
107
108void
109StatusDatasetContext::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 Li4b4f7542016-03-11 02:04:43 +0800116 m_nackSender(resp);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700117}
118
119StatusDatasetContext::StatusDatasetContext(const Interest& interest,
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800120 const DataSender& dataSender,
121 const NackSender& nackSender)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700122 : m_interest(interest)
123 , m_dataSender(dataSender)
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800124 , m_nackSender(nackSender)
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700125 , 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