blob: 6a2d02af49516d0363788e3779fbd60571006525 [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#ifndef NDN_MGMT_STATUS_DATASET_CONTEXT_HPP
27#define NDN_MGMT_STATUS_DATASET_CONTEXT_HPP
28
29#include "../interest.hpp"
30#include "../data.hpp"
31#include "../util/time.hpp"
32#include "../encoding/encoding-buffer.hpp"
33#include "control-response.hpp"
34
35namespace ndn {
36namespace mgmt {
37
Junxiao Shif65a3362015-09-06 20:54:54 -070038/** \brief provides a context for generating response to a StatusDataset request
39 */
40class StatusDatasetContext : noncopyable
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070041{
42public:
43 /** \return prefix of Data packets, with version component but without segment component
44 */
45 const Name&
46 getPrefix() const;
47
48 /** \brief change prefix of Data packets
49 * \param prefix the prefix; it must start with Interest Name, may contain version component,
50 * but must not contain segment component
51 * \throw std::invalid_argument prefix does not start with Interest Name
52 * \throw std::domain_error append, end, or reject has been invoked
53 *
54 * StatusDatasetHandler may change the prefix of Data packets with this method,
55 * before sending any response.
56 * The version component is optional, and will be generated from current timestamp when omitted.
57 */
58 StatusDatasetContext&
59 setPrefix(const Name& prefix);
60
61 /** \return expiration duration for this dataset response
62 */
63 const time::milliseconds&
64 getExpiry() const;
65
66 /** \brief set expiration duration
67 *
68 * The response will be cached for the specified duration.
69 * Incoming Interest that matches a cached response will be satisfied with that response,
70 * without invoking StatusDatasetHandler again.
71 */
72 StatusDatasetContext&
73 setExpiry(const time::milliseconds& expiry);
74
75 /** \brief append a Block to the response
76 * \throw std::domain_error end or reject has been invoked
77 */
78 void
79 append(const Block& block);
80
81 /** \brief end the response successfully after appending zero or more blocks
82 * \throw std::domain_error reject has been invoked
83 */
84 void
85 end();
86
87 /** \brief declare the non-existence of a response
88 * \throw std::domain_error append or end has been invoked
89 *
90 * This should be invoked when the incoming Interest is malformed.
91 * A producer-generated NACK will be returned to requester.
92 *
93 * \param content Content of producer-generated NACK
94 */
95 void
96 reject(const ControlResponse& resp = ControlResponse().setCode(400));
97
98NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
99 typedef std::function<void(const Name& dataName, const Block& content,
100 const MetaInfo& metaInfo)> DataSender;
101
102 StatusDatasetContext(const Interest& interest, const DataSender& dataSender);
103
104private:
105 friend class Dispatcher;
106
107 const Interest& m_interest;
108 DataSender m_dataSender;
109 Name m_prefix;
110 time::milliseconds m_expiry;
111
112NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
113 shared_ptr<EncodingBuffer> m_buffer;
114 uint64_t m_segmentNo;
115
116 enum class State {
117 INITIAL, ///< none of .append, .end, .reject has been invoked
118 RESPONDED, ///< .append has been invoked
119 FINALIZED ///< .end or .reject has been invoked
120 };
121 State m_state;
122};
123
124} // namespace mgmt
125} // namespace ndn
126
127#endif // NDN_MGMT_STATUS_DATASET_CONTEXT_HPP