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-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 
119  const DataSender& dataSender,
120  const NackSender& nackSender)
121  : m_interest(interest)
122  , m_dataSender(dataSender)
123  , m_nackSender(nackSender)
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
ndn::mgmt::StatusDatasetContext::reject
void reject(const ControlResponse &resp=ControlResponse().setCode(400))
declare the non-existence of a response
Definition: status-dataset-context.cpp:108
ndn::mgmt::StatusDatasetContext::append
void append(const Block &block)
append a Block to the response
Definition: status-dataset-context.cpp:69
ndn::mgmt::StatusDatasetContext::NackSender
std::function< void(const ControlResponse &resp)> NackSender
Definition: status-dataset-context.hpp:97
ndn::Name::isPrefixOf
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:299
ndn::mgmt::StatusDatasetContext::StatusDatasetContext
StatusDatasetContext(const Interest &interest, const DataSender &dataSender, const NackSender &nackSender)
Definition: status-dataset-context.cpp:118
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
ndn::Name::appendVersion
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:230
ndn::mgmt::StatusDatasetContext::getPrefix
const Name & getPrefix() const
Definition: status-dataset-context.cpp:30
ndn::mgmt::StatusDatasetContext
provides a context for generating response to a StatusDataset request
Definition: status-dataset-context.hpp:37
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::mgmt::ControlResponse
ControlCommand response.
Definition: control-response.hpp:33
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::mgmt::StatusDatasetContext::setPrefix
StatusDatasetContext & setPrefix(const Name &prefix)
change prefix of Data packets
Definition: status-dataset-context.cpp:36
ndn::MAX_NDN_PACKET_SIZE
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:41
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
ndn::mgmt::StatusDatasetContext::end
void end()
end the response successfully after appending zero or more blocks
Definition: status-dataset-context.cpp:95
ndn::mgmt::StatusDatasetContext::setExpiry
StatusDatasetContext & setExpiry(const time::milliseconds &expiry)
set expiration duration
Definition: status-dataset-context.cpp:62
ndn::Interest::getName
const Name & getName() const noexcept
Definition: interest.hpp:121
status-dataset-context.hpp
ndn::mgmt::StatusDatasetContext::getExpiry
const time::milliseconds & getExpiry() const
Definition: status-dataset-context.cpp:56
ndn::Block::wire
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:281
ndn::mgmt::DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD
const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD
Definition: status-dataset-context.cpp:27
ndn::encoding::makeBinaryBlock
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Definition: block-helpers.cpp:181
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38
ndn::tlv::Content
@ Content
Definition: tlv.hpp:79