Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 14 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 20 | */ |
| 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 | |
| 31 | namespace ndn { |
| 32 | namespace mgmt { |
| 33 | |
Junxiao Shi | f65a336 | 2015-09-06 20:54:54 -0700 | [diff] [blame] | 34 | /** \brief provides a context for generating response to a StatusDataset request |
| 35 | */ |
| 36 | class StatusDatasetContext : noncopyable |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 37 | { |
| 38 | public: |
| 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 Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 89 | * \param resp Content of producer-generated NACK |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 90 | */ |
| 91 | void |
| 92 | reject(const ControlResponse& resp = ControlResponse().setCode(400)); |
| 93 | |
| 94 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 95 | 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 Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 98 | |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 99 | StatusDatasetContext(const Interest& interest, |
| 100 | const DataSender& dataSender, |
| 101 | const NackSender& nackSender); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 102 | |
| 103 | private: |
| 104 | friend class Dispatcher; |
| 105 | |
| 106 | const Interest& m_interest; |
| 107 | DataSender m_dataSender; |
Yanbiao Li | 4b4f754 | 2016-03-11 02:04:43 +0800 | [diff] [blame] | 108 | NackSender m_nackSender; |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 109 | Name m_prefix; |
| 110 | time::milliseconds m_expiry; |
| 111 | |
| 112 | NDN_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 |