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-2021 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(const Block& block)
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  auto cur = block.begin();
72  while (cur != block.end()) {
73  auto nBytesToAppend = std::min<std::ptrdiff_t>(std::distance(cur, block.end()),
74  MAX_PAYLOAD_LENGTH - m_buffer.size());
75  auto next = std::next(cur, nBytesToAppend);
76  m_buffer.insert(m_buffer.end(), cur, next);
77  cur = next;
78 
79  if (cur != block.end()) {
80  BOOST_ASSERT(m_buffer.size() == MAX_PAYLOAD_LENGTH);
81  m_dataSender(Name(m_prefix).appendSegment(m_segmentNo++),
82  makeBinaryBlock(tlv::Content, m_buffer),
83  false);
84  m_buffer.clear();
85  }
86  }
87 }
88 
89 void
90 StatusDatasetContext::end()
91 {
92  if (m_state == State::FINALIZED) {
93  NDN_THROW(std::logic_error("cannot call end() on a finalized context"));
94  }
95 
96  m_state = State::FINALIZED;
97  m_dataSender(Name(m_prefix).appendSegment(m_segmentNo),
98  makeBinaryBlock(tlv::Content, m_buffer),
99  true);
100 }
101 
102 void
103 StatusDatasetContext::reject(const ControlResponse& resp)
104 {
105  if (m_state != State::INITIAL) {
106  NDN_THROW(std::logic_error("cannot call reject() after append/end"));
107  }
108 
109  m_state = State::FINALIZED;
110  m_nackSender(resp);
111 }
112 
113 } // namespace mgmt
114 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
Definition: block.cpp:278
Buffer::const_iterator end() const
Get end iterator of encoded wire.
Definition: block.cpp:287
Represents an Interest packet.
Definition: interest.hpp:50
const Name & getName() const noexcept
Definition: interest.hpp:173
Represents an absolute name.
Definition: name.hpp:46
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
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:67
@ Content
Definition: tlv.hpp:81
Definition: data.cpp:25
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
Definition: tlv.hpp:41