weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
| 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 | |
| 22 | namespace repo { |
| 23 | |
| 24 | RepoCommandResponse& |
| 25 | RepoCommandResponse::setStartBlockId(uint64_t startBlockId) |
| 26 | { |
| 27 | m_startBlockId = startBlockId; |
| 28 | m_hasStartBlockId = true; |
| 29 | m_wire.reset(); |
| 30 | return *this; |
| 31 | } |
| 32 | |
| 33 | bool |
| 34 | RepoCommandResponse::hasStartBlockId() const |
| 35 | { |
| 36 | return m_hasStartBlockId; |
| 37 | } |
| 38 | |
| 39 | RepoCommandResponse& |
| 40 | RepoCommandResponse::setEndBlockId(uint64_t endBlockId) |
| 41 | { |
| 42 | m_endBlockId = endBlockId; |
| 43 | m_hasEndBlockId = true; |
| 44 | m_wire.reset(); |
| 45 | return *this; |
| 46 | } |
| 47 | |
| 48 | bool |
| 49 | RepoCommandResponse::hasEndBlockId() const |
| 50 | { |
| 51 | return m_hasEndBlockId; |
| 52 | } |
| 53 | |
| 54 | RepoCommandResponse& |
| 55 | RepoCommandResponse::setProcessId(uint64_t processId) |
| 56 | { |
| 57 | m_processId = processId; |
| 58 | m_hasProcessId = true; |
| 59 | m_wire.reset(); |
| 60 | return *this; |
| 61 | } |
| 62 | |
| 63 | bool |
| 64 | RepoCommandResponse::hasProcessId() const |
| 65 | { |
| 66 | return m_hasProcessId; |
| 67 | } |
| 68 | |
| 69 | RepoCommandResponse& |
| 70 | RepoCommandResponse::setCode(uint32_t statusCode) |
| 71 | { |
| 72 | m_hasStatusCode = true; |
| 73 | |
| 74 | RepoCommandResponse* response = |
| 75 | static_cast<RepoCommandResponse *> (&ndn::mgmt::ControlResponse::setCode(statusCode)); |
| 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 | |
| 121 | EncodingEstimator estimator; |
| 122 | size_t estimatedSize = wireEncode(estimator); |
| 123 | |
| 124 | EncodingBuffer buffer(estimatedSize, 0); |
| 125 | wireEncode(buffer); |
| 126 | |
| 127 | m_wire = buffer.block(); |
| 128 | return m_wire; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | template<ndn::encoding::Tag T> |
| 133 | size_t |
| 134 | RepoCommandResponse::wireEncode(EncodingImpl<T>& encoder) const |
| 135 | { |
| 136 | size_t totalLength = 0; |
| 137 | size_t variableLength = 0; |
| 138 | |
| 139 | if (m_hasDeleteNum) { |
| 140 | variableLength = encoder.prependNonNegativeInteger(m_deleteNum); |
| 141 | totalLength += variableLength; |
| 142 | totalLength += encoder.prependVarNumber(variableLength); |
| 143 | totalLength += encoder.prependVarNumber(tlv::DeleteNum); |
| 144 | } |
| 145 | |
| 146 | if (m_hasInsertNum) { |
| 147 | variableLength = encoder.prependNonNegativeInteger(m_insertNum); |
| 148 | totalLength += variableLength; |
| 149 | totalLength += encoder.prependVarNumber(variableLength); |
| 150 | totalLength += encoder.prependVarNumber(tlv::InsertNum); |
| 151 | } |
| 152 | |
| 153 | if (m_hasEndBlockId) { |
| 154 | variableLength = encoder.prependNonNegativeInteger(m_endBlockId); |
| 155 | totalLength += variableLength; |
| 156 | totalLength += encoder.prependVarNumber(variableLength); |
| 157 | totalLength += encoder.prependVarNumber(tlv::EndBlockId); |
| 158 | } |
| 159 | |
| 160 | if (m_hasStartBlockId) { |
| 161 | variableLength = encoder.prependNonNegativeInteger(m_startBlockId); |
| 162 | totalLength += variableLength; |
| 163 | totalLength += encoder.prependVarNumber(variableLength); |
| 164 | totalLength += encoder.prependVarNumber(repo::tlv::StartBlockId); |
| 165 | } |
| 166 | |
| 167 | if (m_hasStatusCode) { |
| 168 | variableLength = encoder.prependNonNegativeInteger(getCode()); |
| 169 | totalLength += variableLength; |
| 170 | totalLength += encoder.prependVarNumber(variableLength); |
| 171 | totalLength += encoder.prependVarNumber(tlv::StatusCode); |
| 172 | } |
| 173 | else { |
| 174 | BOOST_THROW_EXCEPTION(Error("required field StatusCode is missing")); |
| 175 | } |
| 176 | |
| 177 | if (m_hasProcessId) { |
| 178 | variableLength = encoder.prependNonNegativeInteger(m_processId); |
| 179 | totalLength += variableLength; |
| 180 | totalLength += encoder.prependVarNumber(variableLength); |
| 181 | totalLength += encoder.prependVarNumber(tlv::ProcessId); |
| 182 | } |
| 183 | |
| 184 | totalLength += encoder.prependVarNumber(totalLength); |
| 185 | totalLength += encoder.prependVarNumber(tlv::RepoCommandResponse); |
| 186 | return totalLength; |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | RepoCommandResponse::wireDecode(const Block& wire) |
| 191 | { |
| 192 | m_hasStartBlockId = false; |
| 193 | m_hasEndBlockId = false; |
| 194 | m_hasProcessId = false; |
| 195 | m_hasStatusCode = false; |
| 196 | m_hasInsertNum = false; |
| 197 | m_hasDeleteNum = false; |
| 198 | |
| 199 | m_wire = wire; |
| 200 | |
| 201 | m_wire.parse(); |
| 202 | |
| 203 | Block::element_const_iterator val; |
| 204 | |
| 205 | if (m_wire.type() != tlv::RepoCommandResponse) |
| 206 | BOOST_THROW_EXCEPTION(Error("RepoCommandResponse malformed")); |
| 207 | |
| 208 | // StartBlockId |
| 209 | val = m_wire.find(tlv::StartBlockId); |
| 210 | if (val != m_wire.elements_end()) { |
| 211 | m_hasStartBlockId = true; |
| 212 | m_startBlockId = readNonNegativeInteger(*val); |
| 213 | } |
| 214 | |
| 215 | // EndBlockId |
| 216 | val = m_wire.find(tlv::EndBlockId); |
| 217 | if (val != m_wire.elements_end()) { |
| 218 | m_hasEndBlockId = true; |
| 219 | m_endBlockId = readNonNegativeInteger(*val); |
| 220 | } |
| 221 | |
| 222 | // ProcessId |
| 223 | val = m_wire.find(tlv::ProcessId); |
| 224 | if (val != m_wire.elements_end()) { |
| 225 | m_hasProcessId = true; |
| 226 | m_processId = readNonNegativeInteger(*val); |
| 227 | } |
| 228 | |
| 229 | //StatusCode |
| 230 | val = m_wire.find(tlv::StatusCode); |
| 231 | if (val != m_wire.elements_end()) { |
| 232 | setCode(readNonNegativeInteger(*val)); |
| 233 | } |
| 234 | else { |
| 235 | BOOST_THROW_EXCEPTION(Error("required field StatusCode is missing")); |
| 236 | } |
| 237 | |
| 238 | // InsertNum |
| 239 | val = m_wire.find(tlv::InsertNum); |
| 240 | if (val != m_wire.elements_end()) { |
| 241 | m_hasInsertNum = true; |
| 242 | m_insertNum = readNonNegativeInteger(*val); |
| 243 | } |
| 244 | |
| 245 | // DeleteNum |
| 246 | val = m_wire.find(tlv::DeleteNum); |
| 247 | if (val != m_wire.elements_end()) { |
| 248 | m_hasDeleteNum = true; |
| 249 | m_deleteNum = readNonNegativeInteger(*val); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandResponse); |
| 254 | |
| 255 | std::ostream& |
| 256 | operator<<(std::ostream& os, const RepoCommandResponse& repoCommandResponse) |
| 257 | { |
| 258 | os << "RepoCommandResponse("; |
| 259 | |
| 260 | if (repoCommandResponse.hasProcessId()) { |
| 261 | os << " ProcessId: " << repoCommandResponse.getProcessId(); |
| 262 | } |
| 263 | // if (repoCommandResponse.hasStatusCode()) { |
| 264 | // os << " StatusCode: " << repoCommandResponse.getStatusCode(); |
| 265 | // } |
| 266 | if (repoCommandResponse.hasStartBlockId()) { |
| 267 | os << " StartBlockId: " << repoCommandResponse.getStartBlockId(); |
| 268 | } |
| 269 | if (repoCommandResponse.hasEndBlockId()) { |
| 270 | os << " EndBlockId: " << repoCommandResponse.getEndBlockId(); |
| 271 | } |
| 272 | if (repoCommandResponse.hasInsertNum()) { |
| 273 | os << " InsertNum: " << repoCommandResponse.getInsertNum(); |
| 274 | } |
| 275 | if (repoCommandResponse.hasDeleteNum()) { |
| 276 | os << " DeleteNum: " << repoCommandResponse.getDeleteNum(); |
| 277 | |
| 278 | } |
| 279 | os << " )"; |
| 280 | return os; |
| 281 | } |
| 282 | |
| 283 | } // namespace repo |