NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
23 
24 namespace ndn {
25 namespace mgmt {
26 
27 const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD = time::milliseconds(1000);
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 
117  m_nackSender(resp);
118 }
119 
121  const DataSender& dataSender,
122  const NackSender& nackSender)
123  : m_interest(interest)
124  , m_dataSender(dataSender)
125  , m_nackSender(nackSender)
126  , m_expiry(DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD)
127  , m_buffer(make_shared<EncodingBuffer>())
128  , m_segmentNo(0)
129  , m_state(State::INITIAL)
130 {
131  setPrefix(interest.getName());
132 }
133 
134 } // namespace mgmt
135 } // namespace ndn
std::function< void(const ControlResponse &resp)> NackSender
void reject(const ControlResponse &resp=ControlResponse().setCode(400))
declare the non-existence of a response
Copyright (c) 2011-2015 Regents of the University of California.
StatusDatasetContext & setExpiry(const time::milliseconds &expiry)
set expiration duration
const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD
const uint8_t * wire() const
Definition: block.cpp:495
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:45
size_t size() const
Definition: block.cpp:504
EncodingImpl< EncoderTag > EncodingBuffer
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block type type with value from a buffer value of size length.
void end()
end the response successfully after appending zero or more blocks
const time::milliseconds & getExpiry() const
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:320
StatusDatasetContext(const Interest &interest, const DataSender &dataSender, const NackSender &nackSender)
StatusDatasetContext & setPrefix(const Name &prefix)
change prefix of Data packets
void append(const Block &block)
append a Block to the response
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE const Block time::milliseconds bool isFinalBlock DataSender
ControlCommand response.
provides a context for generating response to a StatusDataset request
Name & appendVersion(uint64_t version)
Append version using NDN naming conventions.
Definition: name.cpp:218
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:39
const Name & getName() const
Definition: interest.hpp:218