blob: 15398e192bb36dfb5b5b2cf55f588966f691db37 [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento11904062022-04-14 22:33:28 -04002/*
3 * Copyright (c) 2014-2022, Regents of the University of California.
weijia yuan82cf9142018-10-21 12:25:02 -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#include "repo-command-response.hpp"
21
Davide Pesavento11904062022-04-14 22:33:28 -040022#include <ndn-cxx/encoding/block-helpers.hpp>
23
weijia yuan82cf9142018-10-21 12:25:02 -070024namespace repo {
25
26RepoCommandResponse&
27RepoCommandResponse::setStartBlockId(uint64_t startBlockId)
28{
29 m_startBlockId = startBlockId;
30 m_hasStartBlockId = true;
31 m_wire.reset();
32 return *this;
33}
34
35bool
36RepoCommandResponse::hasStartBlockId() const
37{
38 return m_hasStartBlockId;
39}
40
41RepoCommandResponse&
42RepoCommandResponse::setEndBlockId(uint64_t endBlockId)
43{
44 m_endBlockId = endBlockId;
45 m_hasEndBlockId = true;
46 m_wire.reset();
47 return *this;
48}
49
50bool
51RepoCommandResponse::hasEndBlockId() const
52{
53 return m_hasEndBlockId;
54}
55
56RepoCommandResponse&
57RepoCommandResponse::setProcessId(uint64_t processId)
58{
59 m_processId = processId;
60 m_hasProcessId = true;
61 m_wire.reset();
62 return *this;
63}
64
65bool
66RepoCommandResponse::hasProcessId() const
67{
68 return m_hasProcessId;
69}
70
71RepoCommandResponse&
72RepoCommandResponse::setCode(uint32_t statusCode)
73{
74 m_hasStatusCode = true;
Davide Pesavento11904062022-04-14 22:33:28 -040075 auto* response = static_cast<RepoCommandResponse*>(&ndn::mgmt::ControlResponse::setCode(statusCode));
weijia yuan82cf9142018-10-21 12:25:02 -070076 return *response;
77}
78
79bool
80RepoCommandResponse::hasStatusCode() const
81{
82 return m_hasStatusCode;
83}
84
85RepoCommandResponse&
86RepoCommandResponse::setInsertNum(uint64_t insertNum)
87{
88 m_insertNum = insertNum;
89 m_hasInsertNum = true;
90 m_wire.reset();
91 return *this;
92}
93
94bool
95RepoCommandResponse::hasInsertNum() const
96{
97 return m_hasInsertNum;
98}
99
100RepoCommandResponse&
101RepoCommandResponse::setDeleteNum(uint64_t deleteNum)
102{
103 m_deleteNum = deleteNum;
104 m_hasDeleteNum = true;
105 m_wire.reset();
106 return *this;
107}
108
109bool
110RepoCommandResponse::hasDeleteNum() const
111{
112 return m_hasDeleteNum;
113}
114
115const Block&
116RepoCommandResponse::wireEncode() const
117{
118 if (m_wire.hasWire())
119 return m_wire;
120
Davide Pesavento11904062022-04-14 22:33:28 -0400121 ndn::EncodingEstimator estimator;
weijia yuan82cf9142018-10-21 12:25:02 -0700122 size_t estimatedSize = wireEncode(estimator);
123
Davide Pesavento11904062022-04-14 22:33:28 -0400124 ndn::EncodingBuffer buffer(estimatedSize, 0);
weijia yuan82cf9142018-10-21 12:25:02 -0700125 wireEncode(buffer);
126
127 m_wire = buffer.block();
128 return m_wire;
129}
130
weijia yuan82cf9142018-10-21 12:25:02 -0700131template<ndn::encoding::Tag T>
132size_t
Davide Pesavento11904062022-04-14 22:33:28 -0400133RepoCommandResponse::wireEncode(ndn::EncodingImpl<T>& encoder) const
weijia yuan82cf9142018-10-21 12:25:02 -0700134{
135 size_t totalLength = 0;
136 size_t variableLength = 0;
137
138 if (m_hasDeleteNum) {
139 variableLength = encoder.prependNonNegativeInteger(m_deleteNum);
140 totalLength += variableLength;
141 totalLength += encoder.prependVarNumber(variableLength);
142 totalLength += encoder.prependVarNumber(tlv::DeleteNum);
143 }
144
145 if (m_hasInsertNum) {
146 variableLength = encoder.prependNonNegativeInteger(m_insertNum);
147 totalLength += variableLength;
148 totalLength += encoder.prependVarNumber(variableLength);
149 totalLength += encoder.prependVarNumber(tlv::InsertNum);
150 }
151
152 if (m_hasEndBlockId) {
153 variableLength = encoder.prependNonNegativeInteger(m_endBlockId);
154 totalLength += variableLength;
155 totalLength += encoder.prependVarNumber(variableLength);
156 totalLength += encoder.prependVarNumber(tlv::EndBlockId);
157 }
158
159 if (m_hasStartBlockId) {
160 variableLength = encoder.prependNonNegativeInteger(m_startBlockId);
161 totalLength += variableLength;
162 totalLength += encoder.prependVarNumber(variableLength);
163 totalLength += encoder.prependVarNumber(repo::tlv::StartBlockId);
164 }
165
166 if (m_hasStatusCode) {
167 variableLength = encoder.prependNonNegativeInteger(getCode());
168 totalLength += variableLength;
169 totalLength += encoder.prependVarNumber(variableLength);
170 totalLength += encoder.prependVarNumber(tlv::StatusCode);
171 }
172 else {
Davide Pesavento11904062022-04-14 22:33:28 -0400173 NDN_THROW(Error("Required field StatusCode is missing"));
weijia yuan82cf9142018-10-21 12:25:02 -0700174 }
175
176 if (m_hasProcessId) {
177 variableLength = encoder.prependNonNegativeInteger(m_processId);
178 totalLength += variableLength;
179 totalLength += encoder.prependVarNumber(variableLength);
180 totalLength += encoder.prependVarNumber(tlv::ProcessId);
181 }
182
183 totalLength += encoder.prependVarNumber(totalLength);
184 totalLength += encoder.prependVarNumber(tlv::RepoCommandResponse);
185 return totalLength;
186}
187
188void
189RepoCommandResponse::wireDecode(const Block& wire)
190{
Davide Pesavento11904062022-04-14 22:33:28 -0400191 if (wire.type() != tlv::RepoCommandResponse) {
192 NDN_THROW(Error("RepoCommandResponse", wire.type()));
193 }
194
weijia yuan82cf9142018-10-21 12:25:02 -0700195 m_hasStartBlockId = false;
196 m_hasEndBlockId = false;
197 m_hasProcessId = false;
198 m_hasStatusCode = false;
199 m_hasInsertNum = false;
200 m_hasDeleteNum = false;
201
202 m_wire = wire;
weijia yuan82cf9142018-10-21 12:25:02 -0700203 m_wire.parse();
204
weijia yuan82cf9142018-10-21 12:25:02 -0700205 // StartBlockId
Davide Pesavento11904062022-04-14 22:33:28 -0400206 auto val = m_wire.find(tlv::StartBlockId);
weijia yuan82cf9142018-10-21 12:25:02 -0700207 if (val != m_wire.elements_end()) {
208 m_hasStartBlockId = true;
209 m_startBlockId = readNonNegativeInteger(*val);
210 }
211
212 // EndBlockId
213 val = m_wire.find(tlv::EndBlockId);
214 if (val != m_wire.elements_end()) {
215 m_hasEndBlockId = true;
216 m_endBlockId = readNonNegativeInteger(*val);
217 }
218
219 // ProcessId
220 val = m_wire.find(tlv::ProcessId);
221 if (val != m_wire.elements_end()) {
222 m_hasProcessId = true;
223 m_processId = readNonNegativeInteger(*val);
224 }
225
Davide Pesavento11904062022-04-14 22:33:28 -0400226 // StatusCode
weijia yuan82cf9142018-10-21 12:25:02 -0700227 val = m_wire.find(tlv::StatusCode);
228 if (val != m_wire.elements_end()) {
229 setCode(readNonNegativeInteger(*val));
230 }
231 else {
Davide Pesavento11904062022-04-14 22:33:28 -0400232 NDN_THROW(Error("Required field StatusCode is missing"));
weijia yuan82cf9142018-10-21 12:25:02 -0700233 }
234
235 // InsertNum
236 val = m_wire.find(tlv::InsertNum);
237 if (val != m_wire.elements_end()) {
238 m_hasInsertNum = true;
239 m_insertNum = readNonNegativeInteger(*val);
240 }
241
242 // DeleteNum
243 val = m_wire.find(tlv::DeleteNum);
244 if (val != m_wire.elements_end()) {
245 m_hasDeleteNum = true;
246 m_deleteNum = readNonNegativeInteger(*val);
247 }
248}
249
250NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandResponse);
251
weijia yuan82cf9142018-10-21 12:25:02 -0700252} // namespace repo