blob: 8638a2384d1ecdb6365a3477a458abf4a340d783 [file] [log] [blame]
Shuo Chen29c77fe2014-03-18 11:29:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07003 * 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/>.
Shuo Chen29c77fe2014-03-18 11:29:41 -070018 */
19
Alexander Afanasyev39d98072014-05-04 12:46:29 -070020#ifndef REPO_HANDLES_WRITE_HANDLE_HPP
21#define REPO_HANDLES_WRITE_HANDLE_HPP
Shuo Chen29c77fe2014-03-18 11:29:41 -070022
Shuo Chen29c77fe2014-03-18 11:29:41 -070023#include "base-handle.hpp"
Alexander Afanasyev39d98072014-05-04 12:46:29 -070024
Shuo Chen29c77fe2014-03-18 11:29:41 -070025#include <queue>
26
27namespace repo {
28
29using std::map;
30using std::pair;
31using std::queue;
32
33/**
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -070034 * @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 Chen29c77fe2014-03-18 11:29:41 -070054class WriteHandle : public BaseHandle
55{
56
57public:
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
69public:
70 WriteHandle(Face& face, StorageHandle& storageHandle, KeyChain& keyChain,
Shuo Chen028dcd32014-06-21 16:36:44 +080071 Scheduler& scheduler, ValidatorConfig& validator);
Shuo Chen29c77fe2014-03-18 11:29:41 -070072
73 virtual void
74 listen(const Name& prefix);
75
76private:
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
102private: // insert command
103 /**
104 * @brief handle insert commands
105 */
106 void
107 onInterest(const Name& prefix, const Interest& interest);
108
109 void
110 onValidated(const shared_ptr<const Interest>& interest, const Name& prefix);
111
112 void
Shuo Chen028dcd32014-06-21 16:36:44 +0800113 onValidationFailed(const shared_ptr<const Interest>& interest, const string& reason);
Shuo Chen29c77fe2014-03-18 11:29:41 -0700114
Wentao Shang91fb4f22014-05-20 10:55:22 -0700115 void
116 onRegisterSuccess(const Name& prefix);
117
Shuo Chen29c77fe2014-03-18 11:29:41 -0700118 /**
119 * @brief insert command prefix register failed
120 */
121 void
122 onRegisterFailed(const Name& prefix, const std::string& reason);
123
124private: // single data fetching
125 /**
126 * @brief fetch one data
127 */
128 void
129 onData(const Interest& interest, Data& data, ProcessId processId);
130
Shuo Chenc88c87d2014-06-25 20:21:02 +0800131 void
132 onDataValidated(const Interest& interest, const shared_ptr<const Data>& data,
133 ProcessId processId);
134
Shuo Chen29c77fe2014-03-18 11:29:41 -0700135 /**
136 * @brief handle when fetching one data timeout
137 */
138 void
139 onTimeout(const Interest& interest, ProcessId processId);
140
141 void
142 processSingleInsertCommand(const Interest& interest, RepoCommandParameter& parameter);
143
144private: // segmented data fetching
145 /**
146 * @brief fetch segmented data
147 */
148 void
149 onSegmentData(const Interest& interest, Data& data, ProcessId processId);
150
Shuo Chenc88c87d2014-06-25 20:21:02 +0800151 void
152 onSegmentDataValidated(const Interest& interest, const shared_ptr<const Data>& data,
153 ProcessId processId);
154
Shuo Chen29c77fe2014-03-18 11:29:41 -0700155 /**
156 * @brief Timeout when fetching segmented data. Data can be fetched RETRY_TIMEOUT times.
157 */
158 void
159 onSegmentTimeout(const Interest& interest, ProcessId processId);
160
161 /**
162 * @brief initiate fetching segmented data
163 */
164 void
165 segInit(ProcessId processId, const RepoCommandParameter& parameter);
166
167 /**
168 * @brief control for sending interests in function onSegmentData()
169 */
170 void
171 onSegmentDataControl(ProcessId processId, const Interest& interest);
172
173 /**
174 * @brief control for sending interest in function onSegmentTimeout
175 */
176 void
177 onSegmentTimeoutControl(ProcessId processId, const Interest& interest);
178
179 void
180 processSegmentedInsertCommand(const Interest& interest, RepoCommandParameter& parameter);
181
Shuo Chenc88c87d2014-06-25 20:21:02 +0800182private:
183 /**
184 * @brief failure of validation for both one or segmented data
185 */
186 void
187 onDataValidationFailed(const shared_ptr<const Data>& data, const std::string& reason);
188
Shuo Chen29c77fe2014-03-18 11:29:41 -0700189 /**
190 * @brief extends noEndTime of process if not noEndTimeout, set StatusCode 405
191 *
192 * called by onCheckValidated() if there is no EndBlockId. If not noEndTimeout,
193 * extends noEndTime of process. If noEndTimeout, set status code 405 as noEndTimeout.
194 */
195 void
196 extendNoEndTime(ProcessInfo& process);
197
198private: // insert state check command
199 /**
200 * @brief handle insert check command
201 */
202 void
203 onCheckInterest(const Name& prefix, const Interest& interest);
204
205 /**
206 * @brief insert check command prefix register failed
207 */
208 void
209 onCheckRegisterFailed(const Name& prefix, const std::string& reason);
210
211 void
212 onCheckValidated(const shared_ptr<const Interest>& interest, const Name& prefix);
213
214 void
Shuo Chenc88c87d2014-06-25 20:21:02 +0800215 onCheckValidationFailed(const shared_ptr<const Interest>& interest, const std::string& reason);
Shuo Chen29c77fe2014-03-18 11:29:41 -0700216
217private:
218 void
219 deleteProcess(ProcessId processId);
220
221 /**
222 * @brief schedule a event to delete the process
223 */
224 void
225 deferredDeleteProcess(ProcessId processId);
226
227 void
228 negativeReply(const Interest& interest, int statusCode);
229
230private:
231
Shuo Chen028dcd32014-06-21 16:36:44 +0800232 ValidatorConfig& m_validator;
Shuo Chen29c77fe2014-03-18 11:29:41 -0700233
234 map<ProcessId, ProcessInfo> m_processes;
235
236 int m_retryTime;
237 int m_credit;
238 ndn::time::milliseconds m_noEndTimeout;
239};
240
241} // namespace repo
242
Alexander Afanasyev39d98072014-05-04 12:46:29 -0700243#endif // REPO_HANDLES_WRITE_HANDLE_HPP