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-2018 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  BOOST_THROW_EXCEPTION(std::invalid_argument("prefix does not start with Interest Name"));
40  }
41 
42  if (m_state != State::INITIAL) {
43  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(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 = std::make_shared<EncodingBuffer>();
90  }
91  }
92 }
93 
94 void
96 {
97  if (m_state == State::FINALIZED) {
98  BOOST_THROW_EXCEPTION(std::domain_error("state is in FINALIZED"));
99  }
100 
101  m_state = State::FINALIZED;
102 
103  m_dataSender(Name(m_prefix).appendSegment(m_segmentNo),
104  makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()),
105  m_expiry, true);
106 }
107 
108 void
109 StatusDatasetContext::reject(const ControlResponse& resp /*= a ControlResponse with 400*/)
110 {
111  if (m_state != State::INITIAL) {
112  BOOST_THROW_EXCEPTION(std::domain_error("state is in REPONSED or FINALIZED"));
113  }
114 
115  m_state = State::FINALIZED;
116  m_nackSender(resp);
117 }
118 
119 StatusDatasetContext::StatusDatasetContext(const Interest& interest,
120  const DataSender& dataSender,
121  const NackSender& nackSender)
122  : m_interest(interest)
123  , m_dataSender(dataSender)
124  , m_nackSender(nackSender)
125  , m_expiry(DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD)
126  , m_buffer(make_shared<EncodingBuffer>())
127  , m_segmentNo(0)
128  , m_state(State::INITIAL)
129 {
130  setPrefix(interest.getName());
131 }
132 
133 } // namespace mgmt
134 } // namespace ndn
void reject(const ControlResponse &resp=ControlResponse().setCode(400))
declare the non-existence of a response
const Name & getName() const
Definition: interest.hpp:139
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
StatusDatasetContext & setExpiry(const time::milliseconds &expiry)
set expiration duration
const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD
Name & appendVersion(uint64_t version)
Append a version component.
Definition: name.hpp:349
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
represents an Interest packet
Definition: interest.hpp:42
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Definition: block.cpp:300
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:42
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:260
const uint8_t * wire() const
Get pointer to encoded wire.
Definition: block.cpp:291
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