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