Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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: |
| 95 | typedef std::function<void(const Name& dataName, const Block& content, |
| 96 | const MetaInfo& metaInfo)> DataSender; |
| 97 | |
| 98 | StatusDatasetContext(const Interest& interest, const DataSender& dataSender); |
| 99 | |
| 100 | private: |
| 101 | friend class Dispatcher; |
| 102 | |
| 103 | const Interest& m_interest; |
| 104 | DataSender m_dataSender; |
| 105 | Name m_prefix; |
| 106 | time::milliseconds m_expiry; |
| 107 | |
| 108 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 109 | shared_ptr<EncodingBuffer> m_buffer; |
| 110 | uint64_t m_segmentNo; |
| 111 | |
| 112 | enum class State { |
| 113 | INITIAL, ///< none of .append, .end, .reject has been invoked |
| 114 | RESPONDED, ///< .append has been invoked |
| 115 | FINALIZED ///< .end or .reject has been invoked |
| 116 | }; |
| 117 | State m_state; |
| 118 | }; |
| 119 | |
| 120 | } // namespace mgmt |
| 121 | } // namespace ndn |
| 122 | |
| 123 | #endif // NDN_MGMT_STATUS_DATASET_CONTEXT_HPP |