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