Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef REPO_HANDLES_WATCH_HANDLE_HPP |
| 21 | #define REPO_HANDLES_WATCH_HANDLE_HPP |
| 22 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 23 | #include "command-base-handle.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/mgmt/dispatcher.hpp> |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 26 | |
| 27 | #include <queue> |
| 28 | |
| 29 | namespace repo { |
| 30 | |
| 31 | using std::map; |
| 32 | using std::pair; |
| 33 | using std::queue; |
| 34 | using namespace ndn::time; |
| 35 | /** |
| 36 | * @brief WatchHandle provides a different way for repo to insert data. |
| 37 | * |
| 38 | * Repo keeps sending interest to request the data with same prefix, |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 39 | * but with different exclude selectors(updated every time). Repo will stop |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 40 | * watching the prefix until a command interest tell it to stop, the total |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 41 | * amount of sent interests reaches a specific number or time out. |
| 42 | */ |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 43 | class WatchHandle : public CommandBaseHandle |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 44 | { |
| 45 | |
| 46 | public: |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 47 | class Error : public CommandBaseHandle::Error |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | explicit |
| 51 | Error(const std::string& what) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 52 | : CommandBaseHandle::Error(what) |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | public: |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 59 | WatchHandle(Face& face, RepoStorage& storageHandle, |
| 60 | ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler, |
| 61 | Validator& validator); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 62 | |
| 63 | private: // watch-insert command |
| 64 | /** |
| 65 | * @brief handle watch commands |
| 66 | */ |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 67 | |
| 68 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 69 | handleStartCommand(const Name& prefix, const Interest& interest, |
| 70 | const ndn::mgmt::ControlParameters& parameters, |
| 71 | const ndn::mgmt::CommandContinuation& done); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 72 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 73 | onValidationFailed(const std::shared_ptr<const Interest>& interest, const std::string& reason); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 74 | |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 75 | private: // data fetching |
| 76 | /** |
| 77 | * @brief fetch data and send next interest |
| 78 | */ |
| 79 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 80 | onData(const Interest& interest, const Data& data, const Name& name); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * @brief handle when fetching one data timeout |
| 84 | */ |
| 85 | void |
| 86 | onTimeout(const Interest& interest, const Name& name); |
| 87 | |
| 88 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 89 | onDataValidated(const Interest& interest, const Data& data, const Name& name); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * @brief failure of validation |
| 93 | */ |
| 94 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 95 | onDataValidationFailed(const Interest& interest, const Data& data, |
| 96 | const ValidationError& error, const Name& name); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 97 | |
| 98 | |
| 99 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 100 | processWatchCommand(const Interest& interest, const RepoCommandParameter& parameter, |
| 101 | const ndn::mgmt::CommandContinuation& done); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 102 | |
| 103 | void |
| 104 | watchStop(const Name& name); |
| 105 | |
| 106 | private: // watch state check command |
| 107 | /** |
| 108 | * @brief handle watch check command |
| 109 | */ |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 110 | |
| 111 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 112 | handleCheckCommand(const Name& prefix, const Interest& interest, |
| 113 | const ndn::mgmt::ControlParameters& parameters, |
| 114 | const ndn::mgmt::CommandContinuation& done); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 115 | |
| 116 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 117 | onCheckValidationFailed(const Interest& interest, const ValidationError& error); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 118 | |
| 119 | private: // watch stop command |
| 120 | /** |
| 121 | * @brief handle watch stop command |
| 122 | */ |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 123 | |
| 124 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 125 | handleStopCommand(const Name& prefix, const Interest& interest, |
| 126 | const ndn::mgmt::ControlParameters& parameters, |
| 127 | const ndn::mgmt::CommandContinuation& done); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 128 | |
| 129 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 130 | onStopValidationFailed(const Interest& interest, const ValidationError& error); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 131 | |
| 132 | private: |
| 133 | void |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 134 | deferredDeleteProcess(const Name& name); |
| 135 | |
| 136 | void |
| 137 | deleteProcess(const Name& name); |
| 138 | |
| 139 | bool |
| 140 | onRunning(const Name& name); |
| 141 | |
| 142 | private: |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 143 | Validator& m_validator; |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 144 | map<Name, std::pair<RepoCommandResponse, bool> > m_processes; |
| 145 | int64_t m_interestNum; |
| 146 | int64_t m_maxInterestNum; |
| 147 | milliseconds m_interestLifetime; |
| 148 | milliseconds m_watchTimeout; |
| 149 | ndn::time::steady_clock::TimePoint m_startTime; |
| 150 | int64_t m_size; |
| 151 | }; |
| 152 | |
| 153 | } // namespace repo |
| 154 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 155 | #endif // REPO_HANDLES_WATCH_HANDLE_HPP |