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-parameter.hpp" |
| 21 | |
| 22 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 23 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 24 | #include <ndn-cxx/name.hpp> |
| 25 | #include <ndn-cxx/selectors.hpp> |
| 26 | #include <ndn-cxx/mgmt/control-parameters.hpp> |
| 27 | |
| 28 | namespace repo { |
| 29 | |
| 30 | RepoCommandParameter& |
| 31 | RepoCommandParameter::setName(const Name& name) |
| 32 | { |
| 33 | m_name = name; |
| 34 | m_hasFields[REPO_PARAMETER_NAME] = true; |
| 35 | m_wire.reset(); |
| 36 | return *this; |
| 37 | } |
| 38 | |
| 39 | RepoCommandParameter& |
| 40 | RepoCommandParameter::setSelectors(const Selectors& selectors) |
| 41 | { |
| 42 | m_selectors = selectors; |
| 43 | m_wire.reset(); |
| 44 | return *this; |
| 45 | } |
| 46 | |
| 47 | RepoCommandParameter& |
| 48 | RepoCommandParameter::setStartBlockId(uint64_t startBlockId) |
| 49 | { |
| 50 | m_startBlockId = startBlockId; |
| 51 | m_hasFields[REPO_PARAMETER_START_BLOCK_ID] = true; |
| 52 | m_wire.reset(); |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | RepoCommandParameter& |
| 57 | RepoCommandParameter::setEndBlockId(uint64_t endBlockId) |
| 58 | { |
| 59 | m_endBlockId = endBlockId; |
| 60 | m_hasFields[REPO_PARAMETER_END_BLOCK_ID] = true; |
| 61 | m_wire.reset(); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | RepoCommandParameter& |
| 66 | RepoCommandParameter::setProcessId(uint64_t processId) |
| 67 | { |
| 68 | m_processId = processId; |
| 69 | m_hasFields[REPO_PARAMETER_PROCESS_ID] = true; |
| 70 | m_wire.reset(); |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | RepoCommandParameter& |
| 75 | RepoCommandParameter::setMaxInterestNum(uint64_t maxInterestNum) |
| 76 | { |
| 77 | m_maxInterestNum = maxInterestNum; |
| 78 | m_hasFields[REPO_PARAMETER_MAX_INTEREST_NUM] = true; |
| 79 | m_wire.reset(); |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | RepoCommandParameter& |
| 84 | RepoCommandParameter::setWatchTimeout(milliseconds watchTimeout) |
| 85 | { |
| 86 | m_watchTimeout = watchTimeout; |
| 87 | m_hasFields[REPO_PARAMETER_WATCH_TIME_OUT] = true; |
| 88 | m_wire.reset(); |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | RepoCommandParameter& |
| 93 | RepoCommandParameter::setInterestLifetime(milliseconds interestLifetime) |
| 94 | { |
| 95 | m_interestLifetime = interestLifetime; |
| 96 | m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true; |
| 97 | m_wire.reset(); |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | template<ndn::encoding::Tag T> |
| 102 | size_t |
| 103 | RepoCommandParameter::wireEncode(EncodingImpl<T>& encoder) const |
| 104 | { |
| 105 | size_t totalLength = 0; |
| 106 | size_t variableLength = 0; |
| 107 | |
| 108 | if (m_hasFields[REPO_PARAMETER_PROCESS_ID]) { |
| 109 | variableLength = encoder.prependNonNegativeInteger(m_processId); |
| 110 | totalLength += variableLength; |
| 111 | totalLength += encoder.prependVarNumber(variableLength); |
| 112 | totalLength += encoder.prependVarNumber(tlv::ProcessId); |
| 113 | } |
| 114 | |
| 115 | if (m_hasFields[REPO_PARAMETER_END_BLOCK_ID]) { |
| 116 | variableLength = encoder.prependNonNegativeInteger(m_endBlockId); |
| 117 | totalLength += variableLength; |
| 118 | totalLength += encoder.prependVarNumber(variableLength); |
| 119 | totalLength += encoder.prependVarNumber(tlv::EndBlockId); |
| 120 | } |
| 121 | |
| 122 | if (m_hasFields[REPO_PARAMETER_START_BLOCK_ID]) { |
| 123 | variableLength = encoder.prependNonNegativeInteger(m_startBlockId); |
| 124 | totalLength += variableLength; |
| 125 | totalLength += encoder.prependVarNumber(variableLength); |
| 126 | totalLength += encoder.prependVarNumber(tlv::StartBlockId); |
| 127 | } |
| 128 | |
| 129 | if (m_hasFields[REPO_PARAMETER_MAX_INTEREST_NUM]) { |
| 130 | variableLength = encoder.prependNonNegativeInteger(m_maxInterestNum); |
| 131 | totalLength += variableLength; |
| 132 | totalLength += encoder.prependVarNumber(variableLength); |
| 133 | totalLength += encoder.prependVarNumber(tlv::MaxInterestNum); |
| 134 | } |
| 135 | |
| 136 | if (m_hasFields[REPO_PARAMETER_WATCH_TIME_OUT]) { |
| 137 | variableLength = encoder.prependNonNegativeInteger(m_watchTimeout.count()); |
| 138 | totalLength += variableLength; |
| 139 | totalLength += encoder.prependVarNumber(variableLength); |
| 140 | totalLength += encoder.prependVarNumber(tlv::WatchTimeout); |
| 141 | } |
| 142 | |
| 143 | if (m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME]) { |
| 144 | variableLength = encoder.prependNonNegativeInteger(m_interestLifetime.count()); |
| 145 | totalLength += variableLength; |
| 146 | totalLength += encoder.prependVarNumber(variableLength); |
| 147 | totalLength += encoder.prependVarNumber(tlv::InterestLifetime); |
| 148 | } |
| 149 | |
| 150 | if (!getSelectors().empty()) { |
| 151 | totalLength += getSelectors().wireEncode(encoder); |
| 152 | } |
| 153 | |
| 154 | if (m_hasFields[REPO_PARAMETER_NAME]) { |
| 155 | totalLength += getName().wireEncode(encoder); |
| 156 | } |
| 157 | |
| 158 | totalLength += encoder.prependVarNumber(totalLength); |
| 159 | totalLength += encoder.prependVarNumber(tlv::RepoCommandParameter); |
| 160 | return totalLength; |
| 161 | } |
| 162 | |
| 163 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandParameter); |
| 164 | |
| 165 | Block |
| 166 | RepoCommandParameter::wireEncode() const |
| 167 | { |
| 168 | if (m_wire.hasWire()) |
| 169 | return m_wire; |
| 170 | |
| 171 | EncodingEstimator estimator; |
| 172 | size_t estimatedSize = wireEncode(estimator); |
| 173 | |
| 174 | EncodingBuffer buffer(estimatedSize, 0); |
| 175 | wireEncode(buffer); |
| 176 | |
| 177 | m_wire = buffer.block(); |
| 178 | return m_wire; |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | RepoCommandParameter::wireDecode(const Block& wire) |
| 183 | { |
| 184 | m_wire = wire; |
| 185 | |
| 186 | m_wire.parse(); |
| 187 | |
| 188 | if (m_wire.type() != tlv::RepoCommandParameter) |
| 189 | BOOST_THROW_EXCEPTION(Error("Requested decoding of RepoCommandParameter, but Block is of different type")); |
| 190 | |
| 191 | // Name |
| 192 | Block::element_const_iterator val = m_wire.find(tlv::Name); |
| 193 | if (val != m_wire.elements_end()) |
| 194 | { |
| 195 | m_hasFields[REPO_PARAMETER_NAME] = true; |
| 196 | m_name.wireDecode(m_wire.get(tlv::Name)); |
| 197 | } |
| 198 | |
| 199 | // Selectors |
| 200 | val = m_wire.find(tlv::Selectors); |
| 201 | if (val != m_wire.elements_end()) |
| 202 | { |
| 203 | m_selectors.wireDecode(*val); |
| 204 | } |
| 205 | else |
| 206 | m_selectors = Selectors(); |
| 207 | |
| 208 | // StartBlockId |
| 209 | val = m_wire.find(tlv::StartBlockId); |
| 210 | if (val != m_wire.elements_end()) |
| 211 | { |
| 212 | m_hasFields[REPO_PARAMETER_START_BLOCK_ID] = true; |
| 213 | m_startBlockId = readNonNegativeInteger(*val); |
| 214 | } |
| 215 | |
| 216 | // EndBlockId |
| 217 | val = m_wire.find(tlv::EndBlockId); |
| 218 | if (val != m_wire.elements_end()) |
| 219 | { |
| 220 | m_hasFields[REPO_PARAMETER_END_BLOCK_ID] = true; |
| 221 | m_endBlockId = readNonNegativeInteger(*val); |
| 222 | } |
| 223 | |
| 224 | // ProcessId |
| 225 | val = m_wire.find(tlv::ProcessId); |
| 226 | if (val != m_wire.elements_end()) |
| 227 | { |
| 228 | m_hasFields[REPO_PARAMETER_PROCESS_ID] = true; |
| 229 | m_processId = readNonNegativeInteger(*val); |
| 230 | } |
| 231 | |
| 232 | // MaxInterestNum |
| 233 | val = m_wire.find(tlv::MaxInterestNum); |
| 234 | if (val != m_wire.elements_end()) |
| 235 | { |
| 236 | m_hasFields[REPO_PARAMETER_MAX_INTEREST_NUM] = true; |
| 237 | m_maxInterestNum = readNonNegativeInteger(*val); |
| 238 | } |
| 239 | |
| 240 | // WatchTimeout |
| 241 | val = m_wire.find(tlv::WatchTimeout); |
| 242 | if (val != m_wire.elements_end()) |
| 243 | { |
| 244 | m_hasFields[REPO_PARAMETER_WATCH_TIME_OUT] = true; |
| 245 | m_watchTimeout = milliseconds(readNonNegativeInteger(*val)); |
| 246 | } |
| 247 | |
| 248 | // InterestLifeTime |
| 249 | val = m_wire.find(tlv::InterestLifetime); |
| 250 | if (val != m_wire.elements_end()) |
| 251 | { |
| 252 | m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true; |
| 253 | m_interestLifetime = milliseconds(readNonNegativeInteger(*val)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | std::ostream& |
| 258 | operator<<(std::ostream& os, const RepoCommandParameter& repoCommandParameter) |
| 259 | { |
| 260 | os << "RepoCommandParameter("; |
| 261 | |
| 262 | // Name |
| 263 | if (repoCommandParameter.hasName()) { |
| 264 | os << " Name: " << repoCommandParameter.getName(); |
| 265 | } |
| 266 | if (repoCommandParameter.hasStartBlockId()) { |
| 267 | // StartBlockId |
| 268 | os << " StartBlockId: " << repoCommandParameter.getStartBlockId(); |
| 269 | } |
| 270 | // EndBlockId |
| 271 | if (repoCommandParameter.hasEndBlockId()) { |
| 272 | os << " EndBlockId: " << repoCommandParameter.getEndBlockId(); |
| 273 | } |
| 274 | // ProcessId |
| 275 | if (repoCommandParameter.hasProcessId()) { |
| 276 | os << " ProcessId: " << repoCommandParameter.getProcessId(); |
| 277 | } |
| 278 | // MaxInterestNum |
| 279 | if (repoCommandParameter.hasMaxInterestNum()) { |
| 280 | os << " MaxInterestNum: " << repoCommandParameter.getMaxInterestNum(); |
| 281 | } |
| 282 | // WatchTimeout |
| 283 | if (repoCommandParameter.hasProcessId()) { |
| 284 | os << " WatchTimeout: " << repoCommandParameter.getWatchTimeout(); |
| 285 | } |
| 286 | // InterestLifetime |
| 287 | if (repoCommandParameter.hasProcessId()) { |
| 288 | os << " InterestLifetime: " << repoCommandParameter.getInterestLifetime(); |
| 289 | } |
| 290 | os << " )"; |
| 291 | return os; |
| 292 | } |
| 293 | |
| 294 | } // namespace repo |