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-2019 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 
27 const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD = 1_s;
28 
29 const Name&
31 {
32  return m_prefix;
33 }
34 
37 {
38  if (!m_interest.getName().isPrefixOf(prefix)) {
39  NDN_THROW(std::invalid_argument("prefix does not start with Interest Name"));
40  }
41 
42  if (m_state != State::INITIAL) {
43  NDN_THROW(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 
55 const time::milliseconds&
57 {
58  return m_expiry;
59 }
60 
62 StatusDatasetContext::setExpiry(const time::milliseconds& expiry)
63 {
64  m_expiry = expiry;
65  return *this;
66 }
67 
68 void
70 {
71  if (m_state == State::FINALIZED) {
72  NDN_THROW(std::domain_error("state is in FINALIZED"));
73  }
74 
75  m_state = State::RESPONDED;
76 
77  size_t nBytesLeft = block.size();
78  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) {
85  m_dataSender(Name(m_prefix).appendSegment(m_segmentNo++),
86  makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()),
87  m_expiry, false);
88 
89  m_buffer = make_shared<EncodingBuffer>();
90  }
91  }
92 }
93 
94 void
96 {
97  if (m_state == State::FINALIZED) {
98  NDN_THROW(std::domain_error("state is in FINALIZED"));
99  }
100 
101  m_state = State::FINALIZED;
102  m_dataSender(Name(m_prefix).appendSegment(m_segmentNo),
103  makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()),
104  m_expiry, true);
105 }
106 
107 void
108 StatusDatasetContext::reject(const ControlResponse& resp /*= a ControlResponse with 400*/)
109 {
110  if (m_state != State::INITIAL) {
111  NDN_THROW(std::domain_error("state is in RESPONDED or FINALIZED"));
112  }
113 
114  m_state = State::FINALIZED;
115  m_nackSender(resp);
116 }
117 
118 StatusDatasetContext::StatusDatasetContext(const Interest& interest,
119  const DataSender& dataSender,
120  const NackSender& nackSender)
121  : m_interest(interest)
122  , m_dataSender(dataSender)
123  , m_nackSender(nackSender)
124  , m_expiry(DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD)
125  , m_buffer(make_shared<EncodingBuffer>())
126  , m_segmentNo(0)
127  , m_state(State::INITIAL)
128 {
129  setPrefix(interest.getName());
130 }
131 
132 } // namespace mgmt
133 } // namespace ndn
void reject(const ControlResponse &resp=ControlResponse().setCode(400))
declare the non-existence of a response
const Name & getName() const
Definition: interest.hpp:134
Definition: data.cpp:26
StatusDatasetContext & setExpiry(const time::milliseconds &expiry)
set expiration duration
const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:44
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:214
#define NDN_THROW(e)
Definition: exception.hpp:61
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:289
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
void end()
end the response successfully after appending zero or more blocks
Represents an absolute name.
Definition: name.hpp:43
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:251
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:280
StatusDatasetContext & setPrefix(const Name &prefix)
change prefix of Data packets
void append(const Block &block)
append a Block to the response
ControlCommand response.
provides a context for generating response to a StatusDataset request
const time::milliseconds & getExpiry() const
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:41