Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -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 | /* |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -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/>. |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 20 | #ifndef REPO_HANDLES_WRITE_HANDLE_HPP |
| 21 | #define REPO_HANDLES_WRITE_HANDLE_HPP |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 22 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 23 | #include "base-handle.hpp" |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 24 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 25 | #include <queue> |
| 26 | |
| 27 | namespace repo { |
| 28 | |
| 29 | using std::map; |
| 30 | using std::pair; |
| 31 | using std::queue; |
| 32 | |
| 33 | /** |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 34 | * @brief WriteHandle provides basic credit based congestion control. |
| 35 | * |
| 36 | * First repo sends interests of credit number and then credit will be 0. |
| 37 | * |
| 38 | * If a data comes, credit++ and sends a interest then credit--. |
| 39 | * |
| 40 | * If the interest timeout, repo will retry and send interest in retrytimes. |
| 41 | * |
| 42 | * If one interest timeout beyond retrytimes, the fetching process will terminate. |
| 43 | * |
| 44 | * Another case is that if command will insert segmented data without EndBlockId. |
| 45 | * |
| 46 | * The repo will keep fetching data in noendTimeout time. |
| 47 | * |
| 48 | * If data returns with FinalBlockId, this detecting timeout process will terminate. |
| 49 | * |
| 50 | * If client sends a insert check command, the noendTimeout timer will be set to 0. |
| 51 | * |
| 52 | * If repo cannot get FinalBlockId in noendTimeout time, the fetching process will terminate. |
| 53 | */ |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 54 | class WriteHandle : public BaseHandle |
| 55 | { |
| 56 | |
| 57 | public: |
| 58 | class Error : public BaseHandle::Error |
| 59 | { |
| 60 | public: |
| 61 | explicit |
| 62 | Error(const std::string& what) |
| 63 | : BaseHandle::Error(what) |
| 64 | { |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | public: |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 70 | WriteHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain, |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 71 | Scheduler& scheduler, Validator& validator); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 72 | |
| 73 | virtual void |
| 74 | listen(const Name& prefix); |
| 75 | |
| 76 | private: |
| 77 | /** |
| 78 | * @brief Information of insert process including variables for response |
| 79 | * and credit based flow control |
| 80 | */ |
| 81 | struct ProcessInfo |
| 82 | { |
| 83 | //ProcessId id; |
| 84 | RepoCommandResponse response; |
| 85 | queue<SegmentNo> nextSegmentQueue; ///< queue of waiting segment |
| 86 | /// to be sent when having credits |
| 87 | SegmentNo nextSegment; ///< last segment put into the nextSegmentQueue |
| 88 | map<SegmentNo, int> retryCounts; ///< to store retrying times of timeout segment |
| 89 | int credit; ///< congestion control credits of process |
| 90 | |
| 91 | /** |
| 92 | * @brief the latest time point at which EndBlockId must be determined |
| 93 | * |
| 94 | * Segmented fetch process will terminate if EndBlockId cannot be |
| 95 | * determined before this time point. |
| 96 | * It is initialized to now()+noEndTimeout when segmented fetch process begins, |
| 97 | * and reset to now()+noEndTimeout each time an insert status check command is processed. |
| 98 | */ |
| 99 | ndn::time::steady_clock::TimePoint noEndTime; |
| 100 | }; |
| 101 | |
| 102 | private: // insert command |
| 103 | /** |
| 104 | * @brief handle insert commands |
| 105 | */ |
| 106 | void |
| 107 | onInterest(const Name& prefix, const Interest& interest); |
| 108 | |
| 109 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 110 | onValidated(const Interest& interest, const Name& prefix); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 111 | |
| 112 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 113 | onValidationFailed(const Interest& interest, const ValidationError& error); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 114 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 115 | private: // single data fetching |
| 116 | /** |
| 117 | * @brief fetch one data |
| 118 | */ |
| 119 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 120 | onData(const Interest& interest, const Data& data, ProcessId processId); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 121 | |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 122 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 123 | onDataValidated(const Interest& interest, const Data& data, ProcessId processId); |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 124 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 125 | /** |
| 126 | * @brief handle when fetching one data timeout |
| 127 | */ |
| 128 | void |
| 129 | onTimeout(const Interest& interest, ProcessId processId); |
| 130 | |
| 131 | void |
| 132 | processSingleInsertCommand(const Interest& interest, RepoCommandParameter& parameter); |
| 133 | |
| 134 | private: // segmented data fetching |
| 135 | /** |
| 136 | * @brief fetch segmented data |
| 137 | */ |
| 138 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 139 | onSegmentData(const Interest& interest, const Data& data, ProcessId processId); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 140 | |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 141 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 142 | onSegmentDataValidated(const Interest& interest, const Data& data, ProcessId processId); |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 143 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 144 | /** |
| 145 | * @brief Timeout when fetching segmented data. Data can be fetched RETRY_TIMEOUT times. |
| 146 | */ |
| 147 | void |
| 148 | onSegmentTimeout(const Interest& interest, ProcessId processId); |
| 149 | |
| 150 | /** |
| 151 | * @brief initiate fetching segmented data |
| 152 | */ |
| 153 | void |
| 154 | segInit(ProcessId processId, const RepoCommandParameter& parameter); |
| 155 | |
| 156 | /** |
| 157 | * @brief control for sending interests in function onSegmentData() |
| 158 | */ |
| 159 | void |
| 160 | onSegmentDataControl(ProcessId processId, const Interest& interest); |
| 161 | |
| 162 | /** |
| 163 | * @brief control for sending interest in function onSegmentTimeout |
| 164 | */ |
| 165 | void |
| 166 | onSegmentTimeoutControl(ProcessId processId, const Interest& interest); |
| 167 | |
| 168 | void |
| 169 | processSegmentedInsertCommand(const Interest& interest, RepoCommandParameter& parameter); |
| 170 | |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 171 | private: |
| 172 | /** |
| 173 | * @brief failure of validation for both one or segmented data |
| 174 | */ |
| 175 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 176 | onDataValidationFailed(const Data& data, const ValidationError& error); |
Shuo Chen | c88c87d | 2014-06-25 20:21:02 +0800 | [diff] [blame] | 177 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 178 | /** |
| 179 | * @brief extends noEndTime of process if not noEndTimeout, set StatusCode 405 |
| 180 | * |
| 181 | * called by onCheckValidated() if there is no EndBlockId. If not noEndTimeout, |
| 182 | * extends noEndTime of process. If noEndTimeout, set status code 405 as noEndTimeout. |
| 183 | */ |
| 184 | void |
| 185 | extendNoEndTime(ProcessInfo& process); |
| 186 | |
| 187 | private: // insert state check command |
| 188 | /** |
| 189 | * @brief handle insert check command |
| 190 | */ |
| 191 | void |
| 192 | onCheckInterest(const Name& prefix, const Interest& interest); |
| 193 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 194 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 195 | onCheckValidated(const Interest& interest, const Name& prefix); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 196 | |
| 197 | void |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 198 | onCheckValidationFailed(const Interest& interest, const ValidationError& error); |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 199 | |
| 200 | private: |
| 201 | void |
| 202 | deleteProcess(ProcessId processId); |
| 203 | |
| 204 | /** |
| 205 | * @brief schedule a event to delete the process |
| 206 | */ |
| 207 | void |
| 208 | deferredDeleteProcess(ProcessId processId); |
| 209 | |
| 210 | void |
| 211 | negativeReply(const Interest& interest, int statusCode); |
| 212 | |
| 213 | private: |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 214 | Validator& m_validator; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 215 | |
| 216 | map<ProcessId, ProcessInfo> m_processes; |
| 217 | |
| 218 | int m_retryTime; |
| 219 | int m_credit; |
| 220 | ndn::time::milliseconds m_noEndTimeout; |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 221 | ndn::time::milliseconds m_interestLifetime; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace repo |
| 225 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 226 | #endif // REPO_HANDLES_WRITE_HANDLE_HPP |