status-dataset-context.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
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 
23 
24 namespace ndn::mgmt {
25 
26 constexpr size_t MAX_PAYLOAD_LENGTH = MAX_NDN_PACKET_SIZE - 800;
27 
28 StatusDatasetContext::StatusDatasetContext(const Interest& interest,
29  DataSender dataSender, NackSender nackSender)
30  : m_interest(interest)
31  , m_dataSender(std::move(dataSender))
32  , m_nackSender(std::move(nackSender))
33 {
34  setPrefix(interest.getName());
35  m_buffer.reserve(MAX_PAYLOAD_LENGTH);
36 }
37 
38 StatusDatasetContext&
39 StatusDatasetContext::setPrefix(const Name& prefix)
40 {
41  if (m_state != State::INITIAL) {
42  NDN_THROW(std::logic_error("cannot call setPrefix() after append/end/reject"));
43  }
44 
45  if (!m_interest.getName().isPrefixOf(prefix)) {
46  NDN_THROW(std::invalid_argument("prefix must start with the Interest's name"));
47  }
48 
49  if (prefix.at(-1).isSegment()) {
50  NDN_THROW(std::invalid_argument("prefix must not contain a segment component"));
51  }
52 
53  m_prefix = prefix;
54  if (!m_prefix.at(-1).isVersion()) {
55  m_prefix.appendVersion();
56  }
57 
58  return *this;
59 }
60 
61 void
62 StatusDatasetContext::append(span<const uint8_t> bytes)
63 {
64  if (m_state == State::FINALIZED) {
65  NDN_THROW(std::logic_error("cannot call append() on a finalized context"));
66  }
67 
68  m_state = State::RESPONDED;
69 
70  while (!bytes.empty()) {
71  if (m_buffer.size() == MAX_PAYLOAD_LENGTH) {
72  m_dataSender(Name(m_prefix).appendSegment(m_segmentNo++),
73  makeBinaryBlock(tlv::Content, m_buffer), false);
74  m_buffer.clear();
75  }
76 
77  auto chunk = bytes.first(std::min(bytes.size(), MAX_PAYLOAD_LENGTH - m_buffer.size()));
78  m_buffer.insert(m_buffer.end(), chunk.begin(), chunk.end());
79  bytes = bytes.subspan(chunk.size());
80  }
81 }
82 
83 void
84 StatusDatasetContext::end()
85 {
86  if (m_state == State::FINALIZED) {
87  NDN_THROW(std::logic_error("cannot call end() on a finalized context"));
88  }
89 
90  m_state = State::FINALIZED;
91 
92  BOOST_ASSERT(m_buffer.size() <= MAX_PAYLOAD_LENGTH);
93  m_dataSender(Name(m_prefix).appendSegment(m_segmentNo),
94  makeBinaryBlock(tlv::Content, m_buffer), true);
95 }
96 
97 void
98 StatusDatasetContext::reject(const ControlResponse& resp)
99 {
100  if (m_state != State::INITIAL) {
101  NDN_THROW(std::logic_error("cannot call reject() after append/end"));
102  }
103 
104  m_state = State::FINALIZED;
105  m_nackSender(resp);
106 }
107 
108 } // namespace ndn::mgmt
Represents an Interest packet.
Definition: interest.hpp:50
const Name & getName() const noexcept
Get the Interest name.
Definition: interest.hpp:179
Represents an absolute name.
Definition: name.hpp:45
Name & appendVersion(const std::optional< uint64_t > &version=std::nullopt)
Append a version component.
Definition: name.cpp:205
const Component & at(ssize_t i) const
Returns an immutable reference to the component at the specified index, with bounds checking.
Definition: name.cpp:146
ControlCommand response.
bool isSegment() const noexcept
Check if the component is a segment number per NDN naming conventions.
#define NDN_THROW(e)
Definition: exception.hpp:56
Block makeBinaryBlock(uint32_t type, span< const uint8_t > value)
Create a TLV block copying the TLV-VALUE from a byte range.
constexpr size_t MAX_PAYLOAD_LENGTH
@ Name
Definition: tlv.hpp:71
@ Content
Definition: tlv.hpp:93
constexpr size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
Definition: tlv.hpp:41