Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 23 | #include "base-handle.hpp" |
| 24 | |
| 25 | #include <queue> |
| 26 | |
| 27 | namespace repo { |
| 28 | |
| 29 | using std::map; |
| 30 | using std::pair; |
| 31 | using std::queue; |
| 32 | using namespace ndn::time; |
| 33 | /** |
| 34 | * @brief WatchHandle provides a different way for repo to insert data. |
| 35 | * |
| 36 | * Repo keeps sending interest to request the data with same prefix, |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 37 | * but with different exclude selectors(updated every time). Repo will stop |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 38 | * 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] | 39 | * amount of sent interests reaches a specific number or time out. |
| 40 | */ |
| 41 | class WatchHandle : public BaseHandle |
| 42 | { |
| 43 | |
| 44 | public: |
| 45 | class Error : public BaseHandle::Error |
| 46 | { |
| 47 | public: |
| 48 | explicit |
| 49 | Error(const std::string& what) |
| 50 | : BaseHandle::Error(what) |
| 51 | { |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | public: |
| 57 | WatchHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain, |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 58 | Scheduler& scheduler, Validator& validator); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 59 | |
| 60 | virtual void |
| 61 | listen(const Name& prefix); |
| 62 | |
| 63 | private: // watch-insert command |
| 64 | /** |
| 65 | * @brief handle watch commands |
| 66 | */ |
| 67 | void |
| 68 | onInterest(const Name& prefix, const Interest& interest); |
| 69 | |
| 70 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 71 | onValidated(const std::shared_ptr<const Interest>& interest, const Name& prefix); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 72 | |
| 73 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 74 | onValidationFailed(const std::shared_ptr<const Interest>& interest, const std::string& reason); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 75 | |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 76 | private: // data fetching |
| 77 | /** |
| 78 | * @brief fetch data and send next interest |
| 79 | */ |
| 80 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 81 | onData(const Interest& interest, const Data& data, const Name& name); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * @brief handle when fetching one data timeout |
| 85 | */ |
| 86 | void |
| 87 | onTimeout(const Interest& interest, const Name& name); |
| 88 | |
| 89 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 90 | onDataValidated(const Interest& interest, const std::shared_ptr<const Data>& data, |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 91 | const Name& name); |
| 92 | |
| 93 | /** |
| 94 | * @brief failure of validation |
| 95 | */ |
| 96 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 97 | onDataValidationFailed(const Interest& interest, const std::shared_ptr<const Data>& data, |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 98 | const std::string& reason, const Name& name); |
| 99 | |
| 100 | |
| 101 | void |
| 102 | processWatchCommand(const Interest& interest, RepoCommandParameter& parameter); |
| 103 | |
| 104 | void |
| 105 | watchStop(const Name& name); |
| 106 | |
| 107 | private: // watch state check command |
| 108 | /** |
| 109 | * @brief handle watch check command |
| 110 | */ |
| 111 | void |
| 112 | onCheckInterest(const Name& prefix, const Interest& interest); |
| 113 | |
| 114 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 115 | onCheckValidated(const std::shared_ptr<const Interest>& interest, const Name& prefix); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 116 | |
| 117 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 118 | onCheckValidationFailed(const std::shared_ptr<const Interest>& interest, |
| 119 | const std::string& reason); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 120 | |
| 121 | private: // watch stop command |
| 122 | /** |
| 123 | * @brief handle watch stop command |
| 124 | */ |
| 125 | void |
| 126 | onStopInterest(const Name& prefix, const Interest& interest); |
| 127 | |
| 128 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 129 | onStopValidated(const std::shared_ptr<const Interest>& interest, const Name& prefix); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 130 | |
| 131 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 132 | onStopValidationFailed(const std::shared_ptr<const Interest>& interest, |
| 133 | const std::string& reason); |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 134 | |
| 135 | private: |
| 136 | void |
| 137 | negativeReply(const Interest& interest, int statusCode); |
| 138 | |
| 139 | void |
| 140 | deferredDeleteProcess(const Name& name); |
| 141 | |
| 142 | void |
| 143 | deleteProcess(const Name& name); |
| 144 | |
| 145 | bool |
| 146 | onRunning(const Name& name); |
| 147 | |
| 148 | private: |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 149 | Validator& m_validator; |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 150 | |
| 151 | map<Name, std::pair<RepoCommandResponse, bool> > m_processes; |
| 152 | int64_t m_interestNum; |
| 153 | int64_t m_maxInterestNum; |
| 154 | milliseconds m_interestLifetime; |
| 155 | milliseconds m_watchTimeout; |
| 156 | ndn::time::steady_clock::TimePoint m_startTime; |
| 157 | int64_t m_size; |
| 158 | }; |
| 159 | |
| 160 | } // namespace repo |
| 161 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 162 | #endif // REPO_HANDLES_WATCH_HANDLE_HPP |