blob: a3e9e5f60d69e23052bcbe87dca94475d609cff7 [file] [log] [blame]
Weiqi Shi098f91c2014-07-23 17:41:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, Regents of the University of California.
Weiqi Shi098f91c2014-07-23 17:41:35 -07004 *
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
27namespace repo {
28
29using std::map;
30using std::pair;
31using std::queue;
32using 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 Shi098f91c2014-07-23 17:41:35 -070037 * but with different exclude selectors(updated every time). Repo will stop
Weiqi Shi098f91c2014-07-23 17:41:35 -070038 * watching the prefix until a command interest tell it to stop, the total
Weiqi Shi098f91c2014-07-23 17:41:35 -070039 * amount of sent interests reaches a specific number or time out.
40 */
41class WatchHandle : public BaseHandle
42{
43
44public:
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
56public:
57 WatchHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain,
Junxiao Shi047a6fb2017-06-08 16:16:05 +000058 Scheduler& scheduler, Validator& validator);
Weiqi Shi098f91c2014-07-23 17:41:35 -070059
60 virtual void
61 listen(const Name& prefix);
62
63private: // watch-insert command
64 /**
65 * @brief handle watch commands
66 */
67 void
68 onInterest(const Name& prefix, const Interest& interest);
69
70 void
Wentao Shanga8f3c402014-10-30 14:03:27 -070071 onValidated(const std::shared_ptr<const Interest>& interest, const Name& prefix);
Weiqi Shi098f91c2014-07-23 17:41:35 -070072
73 void
Wentao Shanga8f3c402014-10-30 14:03:27 -070074 onValidationFailed(const std::shared_ptr<const Interest>& interest, const std::string& reason);
Weiqi Shi098f91c2014-07-23 17:41:35 -070075
76 void
77 onRegistered(const Name& prefix);
78
79 void
80 onRegisterFailed(const Name& prefix, const std::string& reason);
81
82private: // data fetching
83 /**
84 * @brief fetch data and send next interest
85 */
86 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080087 onData(const Interest& interest, const Data& data, const Name& name);
Weiqi Shi098f91c2014-07-23 17:41:35 -070088
89 /**
90 * @brief handle when fetching one data timeout
91 */
92 void
93 onTimeout(const Interest& interest, const Name& name);
94
95 void
Wentao Shanga8f3c402014-10-30 14:03:27 -070096 onDataValidated(const Interest& interest, const std::shared_ptr<const Data>& data,
Weiqi Shi098f91c2014-07-23 17:41:35 -070097 const Name& name);
98
99 /**
100 * @brief failure of validation
101 */
102 void
Wentao Shanga8f3c402014-10-30 14:03:27 -0700103 onDataValidationFailed(const Interest& interest, const std::shared_ptr<const Data>& data,
Weiqi Shi098f91c2014-07-23 17:41:35 -0700104 const std::string& reason, const Name& name);
105
106
107 void
108 processWatchCommand(const Interest& interest, RepoCommandParameter& parameter);
109
110 void
111 watchStop(const Name& name);
112
113private: // watch state check command
114 /**
115 * @brief handle watch check command
116 */
117 void
118 onCheckInterest(const Name& prefix, const Interest& interest);
119
120 void
Wentao Shanga8f3c402014-10-30 14:03:27 -0700121 onCheckValidated(const std::shared_ptr<const Interest>& interest, const Name& prefix);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700122
123 void
Wentao Shanga8f3c402014-10-30 14:03:27 -0700124 onCheckValidationFailed(const std::shared_ptr<const Interest>& interest,
125 const std::string& reason);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700126
127private: // watch stop command
128 /**
129 * @brief handle watch stop command
130 */
131 void
132 onStopInterest(const Name& prefix, const Interest& interest);
133
134 void
Wentao Shanga8f3c402014-10-30 14:03:27 -0700135 onStopValidated(const std::shared_ptr<const Interest>& interest, const Name& prefix);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700136
137 void
Wentao Shanga8f3c402014-10-30 14:03:27 -0700138 onStopValidationFailed(const std::shared_ptr<const Interest>& interest,
139 const std::string& reason);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700140
141private:
142 void
143 negativeReply(const Interest& interest, int statusCode);
144
145 void
146 deferredDeleteProcess(const Name& name);
147
148 void
149 deleteProcess(const Name& name);
150
151 bool
152 onRunning(const Name& name);
153
154private:
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000155 Validator& m_validator;
Weiqi Shi098f91c2014-07-23 17:41:35 -0700156
157 map<Name, std::pair<RepoCommandResponse, bool> > m_processes;
158 int64_t m_interestNum;
159 int64_t m_maxInterestNum;
160 milliseconds m_interestLifetime;
161 milliseconds m_watchTimeout;
162 ndn::time::steady_clock::TimePoint m_startTime;
163 int64_t m_size;
164};
165
166} // namespace repo
167
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800168#endif // REPO_HANDLES_WATCH_HANDLE_HPP