blob: 221b67da227595251e328655e612e43ab5fdfa48 [file] [log] [blame]
Weiqi Shi098f91c2014-07-23 17:41:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc0e26582017-08-13 21:16:49 -04002/*
weijia yuan82cf9142018-10-21 12:25:02 -07003 * Copyright (c) 2014-2018, 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
weijia yuan82cf9142018-10-21 12:25:02 -070023#include "command-base-handle.hpp"
24
25#include <ndn-cxx/mgmt/dispatcher.hpp>
Weiqi Shi098f91c2014-07-23 17:41:35 -070026
27#include <queue>
28
29namespace repo {
30
31using std::map;
32using std::pair;
33using std::queue;
34using namespace ndn::time;
35/**
36 * @brief WatchHandle provides a different way for repo to insert data.
37 *
38 * Repo keeps sending interest to request the data with same prefix,
Weiqi Shi098f91c2014-07-23 17:41:35 -070039 * but with different exclude selectors(updated every time). Repo will stop
Weiqi Shi098f91c2014-07-23 17:41:35 -070040 * watching the prefix until a command interest tell it to stop, the total
Weiqi Shi098f91c2014-07-23 17:41:35 -070041 * amount of sent interests reaches a specific number or time out.
42 */
weijia yuan82cf9142018-10-21 12:25:02 -070043class WatchHandle : public CommandBaseHandle
Weiqi Shi098f91c2014-07-23 17:41:35 -070044{
45
46public:
weijia yuan82cf9142018-10-21 12:25:02 -070047 class Error : public CommandBaseHandle::Error
Weiqi Shi098f91c2014-07-23 17:41:35 -070048 {
49 public:
50 explicit
51 Error(const std::string& what)
weijia yuan82cf9142018-10-21 12:25:02 -070052 : CommandBaseHandle::Error(what)
Weiqi Shi098f91c2014-07-23 17:41:35 -070053 {
54 }
55 };
56
57
58public:
weijia yuan82cf9142018-10-21 12:25:02 -070059 WatchHandle(Face& face, RepoStorage& storageHandle,
60 ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler,
61 Validator& validator);
Weiqi Shi098f91c2014-07-23 17:41:35 -070062
63private: // watch-insert command
64 /**
65 * @brief handle watch commands
66 */
Weiqi Shi098f91c2014-07-23 17:41:35 -070067
68 void
weijia yuan82cf9142018-10-21 12:25:02 -070069 handleStartCommand(const Name& prefix, const Interest& interest,
70 const ndn::mgmt::ControlParameters& parameters,
71 const ndn::mgmt::CommandContinuation& done);
Weiqi Shi098f91c2014-07-23 17:41:35 -070072 void
weijia yuan82cf9142018-10-21 12:25:02 -070073 onValidationFailed(const std::shared_ptr<const Interest>& interest, const std::string& reason);
Weiqi Shi098f91c2014-07-23 17:41:35 -070074
Weiqi Shi098f91c2014-07-23 17:41:35 -070075private: // data fetching
76 /**
77 * @brief fetch data and send next interest
78 */
79 void
Alexander Afanasyev42290b22017-03-09 12:58:29 -080080 onData(const Interest& interest, const Data& data, const Name& name);
Weiqi Shi098f91c2014-07-23 17:41:35 -070081
82 /**
83 * @brief handle when fetching one data timeout
84 */
85 void
86 onTimeout(const Interest& interest, const Name& name);
87
88 void
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040089 onDataValidated(const Interest& interest, const Data& data, const Name& name);
Weiqi Shi098f91c2014-07-23 17:41:35 -070090
91 /**
92 * @brief failure of validation
93 */
94 void
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040095 onDataValidationFailed(const Interest& interest, const Data& data,
96 const ValidationError& error, const Name& name);
Weiqi Shi098f91c2014-07-23 17:41:35 -070097
98
99 void
weijia yuan82cf9142018-10-21 12:25:02 -0700100 processWatchCommand(const Interest& interest, const RepoCommandParameter& parameter,
101 const ndn::mgmt::CommandContinuation& done);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700102
103 void
104 watchStop(const Name& name);
105
106private: // watch state check command
107 /**
108 * @brief handle watch check command
109 */
Weiqi Shi098f91c2014-07-23 17:41:35 -0700110
111 void
weijia yuan82cf9142018-10-21 12:25:02 -0700112 handleCheckCommand(const Name& prefix, const Interest& interest,
113 const ndn::mgmt::ControlParameters& parameters,
114 const ndn::mgmt::CommandContinuation& done);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700115
116 void
Alexander Afanasyevc0e26582017-08-13 21:16:49 -0400117 onCheckValidationFailed(const Interest& interest, const ValidationError& error);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700118
119private: // watch stop command
120 /**
121 * @brief handle watch stop command
122 */
Weiqi Shi098f91c2014-07-23 17:41:35 -0700123
124 void
weijia yuan82cf9142018-10-21 12:25:02 -0700125 handleStopCommand(const Name& prefix, const Interest& interest,
126 const ndn::mgmt::ControlParameters& parameters,
127 const ndn::mgmt::CommandContinuation& done);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700128
129 void
Alexander Afanasyevc0e26582017-08-13 21:16:49 -0400130 onStopValidationFailed(const Interest& interest, const ValidationError& error);
Weiqi Shi098f91c2014-07-23 17:41:35 -0700131
132private:
133 void
Weiqi Shi098f91c2014-07-23 17:41:35 -0700134 deferredDeleteProcess(const Name& name);
135
136 void
137 deleteProcess(const Name& name);
138
139 bool
140 onRunning(const Name& name);
141
142private:
Junxiao Shi047a6fb2017-06-08 16:16:05 +0000143 Validator& m_validator;
Weiqi Shi098f91c2014-07-23 17:41:35 -0700144 map<Name, std::pair<RepoCommandResponse, bool> > m_processes;
145 int64_t m_interestNum;
146 int64_t m_maxInterestNum;
147 milliseconds m_interestLifetime;
148 milliseconds m_watchTimeout;
149 ndn::time::steady_clock::TimePoint m_startTime;
150 int64_t m_size;
151};
152
153} // namespace repo
154
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800155#endif // REPO_HANDLES_WATCH_HANDLE_HPP