blob: 42ad0c1ee8d7f2f30136dc301368a9c9cef6d121 [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Li4b4f7542016-03-11 02:04:43 +08003 * Copyright (c) 2013-2016 Regents of the University of California.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07004 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07006 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07007 * 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.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070010 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070011 * 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.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070014 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070015 * 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.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070020 */
21
22#ifndef NDN_MGMT_STATUS_DATASET_CONTEXT_HPP
23#define NDN_MGMT_STATUS_DATASET_CONTEXT_HPP
24
25#include "../interest.hpp"
26#include "../data.hpp"
27#include "../util/time.hpp"
28#include "../encoding/encoding-buffer.hpp"
29#include "control-response.hpp"
30
31namespace ndn {
32namespace mgmt {
33
Junxiao Shif65a3362015-09-06 20:54:54 -070034/** \brief provides a context for generating response to a StatusDataset request
35 */
36class StatusDatasetContext : noncopyable
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070037{
38public:
39 /** \return prefix of Data packets, with version component but without segment component
40 */
41 const Name&
42 getPrefix() const;
43
44 /** \brief change prefix of Data packets
45 * \param prefix the prefix; it must start with Interest Name, may contain version component,
46 * but must not contain segment component
47 * \throw std::invalid_argument prefix does not start with Interest Name
48 * \throw std::domain_error append, end, or reject has been invoked
49 *
50 * StatusDatasetHandler may change the prefix of Data packets with this method,
51 * before sending any response.
52 * The version component is optional, and will be generated from current timestamp when omitted.
53 */
54 StatusDatasetContext&
55 setPrefix(const Name& prefix);
56
57 /** \return expiration duration for this dataset response
58 */
59 const time::milliseconds&
60 getExpiry() const;
61
62 /** \brief set expiration duration
63 *
64 * The response will be cached for the specified duration.
65 * Incoming Interest that matches a cached response will be satisfied with that response,
66 * without invoking StatusDatasetHandler again.
67 */
68 StatusDatasetContext&
69 setExpiry(const time::milliseconds& expiry);
70
71 /** \brief append a Block to the response
72 * \throw std::domain_error end or reject has been invoked
73 */
74 void
75 append(const Block& block);
76
77 /** \brief end the response successfully after appending zero or more blocks
78 * \throw std::domain_error reject has been invoked
79 */
80 void
81 end();
82
83 /** \brief declare the non-existence of a response
84 * \throw std::domain_error append or end has been invoked
85 *
86 * This should be invoked when the incoming Interest is malformed.
87 * A producer-generated NACK will be returned to requester.
88 *
Davide Pesavento18cf81b2015-09-12 23:36:43 +020089 * \param resp Content of producer-generated NACK
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070090 */
91 void
92 reject(const ControlResponse& resp = ControlResponse().setCode(400));
93
94NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yanbiao Li4b4f7542016-03-11 02:04:43 +080095 typedef std::function<void(const Name& dataName, const Block& content, time::milliseconds imsFresh,
96 bool isFinalBlock)> DataSender;
97 typedef std::function<void(const ControlResponse& resp)> NackSender;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070098
Yanbiao Li4b4f7542016-03-11 02:04:43 +080099 StatusDatasetContext(const Interest& interest,
100 const DataSender& dataSender,
101 const NackSender& nackSender);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700102
103private:
104 friend class Dispatcher;
105
106 const Interest& m_interest;
107 DataSender m_dataSender;
Yanbiao Li4b4f7542016-03-11 02:04:43 +0800108 NackSender m_nackSender;
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700109 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