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