weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 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/>. |
| 18 | */ |
| 19 | |
| 20 | #include "repo-command-response.hpp" |
| 21 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 22 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 23 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 24 | namespace repo { |
| 25 | |
| 26 | RepoCommandResponse& |
| 27 | RepoCommandResponse::setStartBlockId(uint64_t startBlockId) |
| 28 | { |
| 29 | m_startBlockId = startBlockId; |
| 30 | m_hasStartBlockId = true; |
| 31 | m_wire.reset(); |
| 32 | return *this; |
| 33 | } |
| 34 | |
| 35 | bool |
| 36 | RepoCommandResponse::hasStartBlockId() const |
| 37 | { |
| 38 | return m_hasStartBlockId; |
| 39 | } |
| 40 | |
| 41 | RepoCommandResponse& |
| 42 | RepoCommandResponse::setEndBlockId(uint64_t endBlockId) |
| 43 | { |
| 44 | m_endBlockId = endBlockId; |
| 45 | m_hasEndBlockId = true; |
| 46 | m_wire.reset(); |
| 47 | return *this; |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | RepoCommandResponse::hasEndBlockId() const |
| 52 | { |
| 53 | return m_hasEndBlockId; |
| 54 | } |
| 55 | |
| 56 | RepoCommandResponse& |
| 57 | RepoCommandResponse::setProcessId(uint64_t processId) |
| 58 | { |
| 59 | m_processId = processId; |
| 60 | m_hasProcessId = true; |
| 61 | m_wire.reset(); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | bool |
| 66 | RepoCommandResponse::hasProcessId() const |
| 67 | { |
| 68 | return m_hasProcessId; |
| 69 | } |
| 70 | |
| 71 | RepoCommandResponse& |
| 72 | RepoCommandResponse::setCode(uint32_t statusCode) |
| 73 | { |
| 74 | m_hasStatusCode = true; |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 75 | auto* response = static_cast<RepoCommandResponse*>(&ndn::mgmt::ControlResponse::setCode(statusCode)); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 76 | return *response; |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | RepoCommandResponse::hasStatusCode() const |
| 81 | { |
| 82 | return m_hasStatusCode; |
| 83 | } |
| 84 | |
| 85 | RepoCommandResponse& |
| 86 | RepoCommandResponse::setInsertNum(uint64_t insertNum) |
| 87 | { |
| 88 | m_insertNum = insertNum; |
| 89 | m_hasInsertNum = true; |
| 90 | m_wire.reset(); |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | bool |
| 95 | RepoCommandResponse::hasInsertNum() const |
| 96 | { |
| 97 | return m_hasInsertNum; |
| 98 | } |
| 99 | |
| 100 | RepoCommandResponse& |
| 101 | RepoCommandResponse::setDeleteNum(uint64_t deleteNum) |
| 102 | { |
| 103 | m_deleteNum = deleteNum; |
| 104 | m_hasDeleteNum = true; |
| 105 | m_wire.reset(); |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | bool |
| 110 | RepoCommandResponse::hasDeleteNum() const |
| 111 | { |
| 112 | return m_hasDeleteNum; |
| 113 | } |
| 114 | |
| 115 | const Block& |
| 116 | RepoCommandResponse::wireEncode() const |
| 117 | { |
| 118 | if (m_wire.hasWire()) |
| 119 | return m_wire; |
| 120 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 121 | ndn::EncodingEstimator estimator; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 122 | size_t estimatedSize = wireEncode(estimator); |
| 123 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 124 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 125 | wireEncode(buffer); |
| 126 | |
| 127 | m_wire = buffer.block(); |
| 128 | return m_wire; |
| 129 | } |
| 130 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 131 | template<ndn::encoding::Tag T> |
| 132 | size_t |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 133 | RepoCommandResponse::wireEncode(ndn::EncodingImpl<T>& encoder) const |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 134 | { |
| 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 Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 173 | NDN_THROW(Error("Required field StatusCode is missing")); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 174 | } |
| 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 | |
| 188 | void |
| 189 | RepoCommandResponse::wireDecode(const Block& wire) |
| 190 | { |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 191 | if (wire.type() != tlv::RepoCommandResponse) { |
| 192 | NDN_THROW(Error("RepoCommandResponse", wire.type())); |
| 193 | } |
| 194 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 195 | 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 yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 203 | m_wire.parse(); |
| 204 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 205 | // StartBlockId |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 206 | auto val = m_wire.find(tlv::StartBlockId); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 207 | 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 Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 226 | // StatusCode |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 227 | val = m_wire.find(tlv::StatusCode); |
| 228 | if (val != m_wire.elements_end()) { |
| 229 | setCode(readNonNegativeInteger(*val)); |
| 230 | } |
| 231 | else { |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 232 | NDN_THROW(Error("Required field StatusCode is missing")); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 233 | } |
| 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 | |
| 250 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandResponse); |
| 251 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 252 | } // namespace repo |