NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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 
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 
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
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
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
void reject(const ControlResponse &resp=ControlResponse().setCode(400))
Rejects the request.
Copyright (c) 2011-2015 Regents of the University of California.
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:300
STL namespace.
Represents an Interest packet.
Definition: interest.hpp:48
Name & appendVersion(const optional< uint64_t > &version=nullopt)
Append a version component.
Definition: name.cpp:230
#define NDN_THROW(e)
Definition: exception.hpp:61
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
std::function< void(const ControlResponse &)> NackSender
bool isSegment() const
Check if the component is a segment number per NDN naming conventions.
void end()
Finalizes the response successfully after appending zero or more blocks.
Represents an absolute name.
Definition: name.hpp:41
Block makeBinaryBlock(uint32_t type, span< const uint8_t > value)
Create a TLV block copying the TLV-VALUE from a byte range.
void append(span< const uint8_t > bytes)
Appends a sequence of bytes to the response.
const Name & getName() const noexcept
Definition: interest.hpp:172
StatusDatasetContext & setPrefix(const Name &prefix)
Changes the prefix of the response Data packets.
const size_t MAX_PAYLOAD_LENGTH
ControlCommand response.
bool isVersion() const
Check if the component is a version per NDN naming conventions.
Provides a context for generating the response to a StatusDataset request.
StatusDatasetContext(const Interest &interest, DataSender dataSender, NackSender nackSender)
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
Definition: tlv.hpp:41