blob: 1d386ef3d387cf6b164db66b4fd256a1b6bc8e93 [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "status-dataset-context.hpp"
27
28namespace ndn {
29namespace mgmt {
30
31const time::milliseconds DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD = time::milliseconds(1000);
32
33const Name&
34StatusDatasetContext::getPrefix() const
35{
36 return m_prefix;
37}
38
39StatusDatasetContext&
40StatusDatasetContext::setPrefix(const Name& prefix)
41{
42 if (!m_interest.getName().isPrefixOf(prefix)) {
43 BOOST_THROW_EXCEPTION(std::invalid_argument("prefix does not start with Interest Name"));
44 }
45
46 if (m_state != State::INITIAL) {
47 BOOST_THROW_EXCEPTION(std::domain_error("state is not in INITIAL"));
48 }
49
50 m_prefix = prefix;
51
52 if (!m_prefix[-1].isVersion()) {
53 m_prefix.appendVersion();
54 }
55
56 return *this;
57}
58
59const time::milliseconds&
60StatusDatasetContext::getExpiry() const
61{
62 return m_expiry;
63}
64
65StatusDatasetContext&
66StatusDatasetContext::setExpiry(const time::milliseconds& expiry)
67{
68 m_expiry = expiry;
69 return *this;
70}
71
72void
73StatusDatasetContext::append(const Block& block)
74{
75 if (m_state == State::FINALIZED) {
76 BOOST_THROW_EXCEPTION(std::domain_error("state is in FINALIZED"));
77 }
78
79 m_state = State::RESPONDED;
80
81 size_t nBytesLeft = block.size();
82
83 while (nBytesLeft > 0) {
84 size_t nBytesAppend = std::min(nBytesLeft,
85 (ndn::MAX_NDN_PACKET_SIZE >> 1) - m_buffer->size());
86 m_buffer->appendByteArray(block.wire() + (block.size() - nBytesLeft), nBytesAppend);
87 nBytesLeft -= nBytesAppend;
88
89 if (nBytesLeft > 0) {
90 const Block& content = makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size());
91 m_dataSender(Name(m_prefix).appendSegment(m_segmentNo++), content,
92 MetaInfo().setFreshnessPeriod(m_expiry));
93
94 m_buffer = std::make_shared<EncodingBuffer>();
95 }
96 }
97}
98
99void
100StatusDatasetContext::end()
101{
102 if (m_state == State::FINALIZED) {
103 BOOST_THROW_EXCEPTION(std::domain_error("state is in FINALIZED"));
104 }
105
106 m_state = State::FINALIZED;
107
108 auto dataName = Name(m_prefix).appendSegment(m_segmentNo++);
109 m_dataSender(dataName, makeBinaryBlock(tlv::Content, m_buffer->buf(), m_buffer->size()),
110 MetaInfo().setFreshnessPeriod(m_expiry).setFinalBlockId(dataName[-1]));
111}
112
113void
114StatusDatasetContext::reject(const ControlResponse& resp /*= a ControlResponse with 400*/)
115{
116 if (m_state != State::INITIAL) {
117 BOOST_THROW_EXCEPTION(std::domain_error("state is in REPONSED or FINALIZED"));
118 }
119
120 m_state = State::FINALIZED;
121
122 m_dataSender(m_interest.getName(), resp.wireEncode(),
123 MetaInfo().setType(tlv::ContentType_Nack));
124}
125
126StatusDatasetContext::StatusDatasetContext(const Interest& interest,
127 const DataSender& dataSender)
128 : m_interest(interest)
129 , m_dataSender(dataSender)
130 , m_expiry(DEFAULT_STATUS_DATASET_FRESHNESS_PERIOD)
131 , m_buffer(make_shared<EncodingBuffer>())
132 , m_segmentNo(0)
133 , m_state(State::INITIAL)
134{
135 setPrefix(interest.getName());
136}
137
138} // namespace mgmt
139} // namespace ndn