blob: f39856f7c5ce57de1de0d0ce3d81be20c19ef530 [file] [log] [blame]
Shuo Chen09f09bb2014-03-18 15:37:11 -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.
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -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/>.
Shuo Chen09f09bb2014-03-18 15:37:11 -070018 */
19
20#include "delete-handle.hpp"
21
22namespace repo {
23
Weiqi Shif0330d52014-07-09 10:54:27 -070024DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain,
Junxiao Shi047a6fb2017-06-08 16:16:05 +000025 Scheduler& scheduler,
26 Validator& validator)
Shuo Chen09f09bb2014-03-18 15:37:11 -070027 : BaseHandle(face, storageHandle, keyChain, scheduler)
28 , m_validator(validator)
29{
30}
31
32void
33DeleteHandle::onInterest(const Name& prefix, const Interest& interest)
34{
Shuo Chen09f09bb2014-03-18 15:37:11 -070035 m_validator.validate(interest, bind(&DeleteHandle::onValidated, this, _1, prefix),
Shuo Chen028dcd32014-06-21 16:36:44 +080036 bind(&DeleteHandle::onValidationFailed, this, _1, _2));
Shuo Chen09f09bb2014-03-18 15:37:11 -070037}
38
39
40void
41DeleteHandle::onRegisterFailed(const Name& prefix, const std::string& reason)
42{
Alexander Afanasyev42290b22017-03-09 12:58:29 -080043 BOOST_THROW_EXCEPTION(Error("Delete prefix registration failed"));
Shuo Chen09f09bb2014-03-18 15:37:11 -070044}
45
46
47void
48DeleteHandle::onCheckInterest(const Name& prefix, const Interest& interest)
49{
50 BOOST_ASSERT(false); // Deletion progress check, not implemented
51}
52
53
54void
55DeleteHandle::onCheckRegisterFailed(const Name& prefix, const std::string& reason)
56{
Alexander Afanasyev42290b22017-03-09 12:58:29 -080057 BOOST_THROW_EXCEPTION(Error("Delete check prefix registration failed"));
Shuo Chen09f09bb2014-03-18 15:37:11 -070058}
59
60
61void
62DeleteHandle::onValidated(const shared_ptr<const Interest>& interest, const Name& prefix)
63{
64 RepoCommandParameter parameter;
65
66 try {
67 extractParameter(*interest, prefix, parameter);
68 }
69 catch (RepoCommandParameter::Error) {
70 negativeReply(*interest, 403);
71 return;
72 }
73
74 if (parameter.hasSelectors()) {
75
76 if (parameter.hasStartBlockId() || parameter.hasEndBlockId()) {
77 negativeReply(*interest, 402);
78 return;
79 }
80
81 //choose data with selector and delete it
82 processSelectorDeleteCommand(*interest, parameter);
83 return;
84 }
85
86 if (!parameter.hasStartBlockId() && !parameter.hasEndBlockId()) {
87 processSingleDeleteCommand(*interest, parameter);
88 return;
89 }
90
91 processSegmentDeleteCommand(*interest, parameter);
92}
93
94void
Wentao Shanga8f3c402014-10-30 14:03:27 -070095DeleteHandle::onValidationFailed(const shared_ptr<const Interest>& interest,
96 const std::string& reason)
Shuo Chen09f09bb2014-03-18 15:37:11 -070097{
Shuo Chen028dcd32014-06-21 16:36:44 +080098 std::cerr << reason << std::endl;
Shuo Chen09f09bb2014-03-18 15:37:11 -070099 negativeReply(*interest, 401);
100}
Weiqi Shif0330d52014-07-09 10:54:27 -0700101//listen change the setinterestfilter
Shuo Chen09f09bb2014-03-18 15:37:11 -0700102void
103DeleteHandle::listen(const Name& prefix)
104{
Weiqi Shif0330d52014-07-09 10:54:27 -0700105 ndn::Name deleteprefix = Name(prefix).append("delete");
106 ndn::InterestFilter filter(deleteprefix);
107 getFace().setInterestFilter(filter,
Shuo Chen09f09bb2014-03-18 15:37:11 -0700108 bind(&DeleteHandle::onInterest, this, _1, _2),
Shuo Chen09f09bb2014-03-18 15:37:11 -0700109 bind(&DeleteHandle::onRegisterFailed, this, _1, _2));
110}
111
112void
113DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
114 uint64_t statusCode, uint64_t nDeletedDatas)
115{
116 RepoCommandResponse response;
117 if (parameter.hasProcessId()) {
118 response.setProcessId(parameter.getProcessId());
119 response.setStatusCode(statusCode);
120 response.setDeleteNum(nDeletedDatas);
121 }
122 else {
123 response.setStatusCode(403);
124 }
125 reply(interest, response);
126}
127
128void
129DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode)
130{
131 RepoCommandResponse response;
132 response.setStatusCode(statusCode);
133 reply(interest, response);
134}
135
136void
137DeleteHandle::processSingleDeleteCommand(const Interest& interest,
138 RepoCommandParameter& parameter)
139{
Weiqi Shif0330d52014-07-09 10:54:27 -0700140 int64_t nDeletedDatas = getStorageHandle().deleteData(parameter.getName());
141 if (nDeletedDatas == -1) {
142 std::cerr << "Deletion Failed!" <<std::endl;
143 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700144 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700145 else
146 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700147}
148
149void
150DeleteHandle::processSelectorDeleteCommand(const Interest& interest,
151 RepoCommandParameter& parameter)
152{
Weiqi Shif0330d52014-07-09 10:54:27 -0700153 int64_t nDeletedDatas = getStorageHandle()
154 .deleteData(Interest(parameter.getName())
155 .setSelectors(parameter.getSelectors()));
156 if (nDeletedDatas == -1) {
157 std::cerr << "Deletion Failed!" <<std::endl;
158 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700159 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700160 else
161 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700162}
163
164void
165DeleteHandle::processSegmentDeleteCommand(const Interest& interest,
166 RepoCommandParameter& parameter)
167{
168 if (!parameter.hasStartBlockId())
169 parameter.setStartBlockId(0);
170
171 if (parameter.hasEndBlockId()) {
172 SegmentNo startBlockId = parameter.getStartBlockId();
173 SegmentNo endBlockId = parameter.getEndBlockId();
174
175 if (startBlockId > endBlockId) {
176 negativeReply(interest, 403);
177 return;
178 }
179
180 Name prefix = parameter.getName();
181 uint64_t nDeletedDatas = 0;
182 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
183 Name name = prefix;
184 name.appendSegment(i);
185 if (getStorageHandle().deleteData(name)) {
186 nDeletedDatas++;
187 }
188 }
189 //All the data deleted, return 200
190 positiveReply(interest, parameter, 200, nDeletedDatas);
191 }
192 else {
193 BOOST_ASSERT(false); // segmented deletion without EndBlockId, not implemented
194 }
195}
196
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800197} // namespace repo