Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_FACE_STATUS_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_FACE_STATUS_HPP |
| 24 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 25 | // This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own. |
| 26 | #include "nfd-face-flags.hpp" |
| 27 | |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 28 | #include "../encoding/tlv-nfd.hpp" |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 29 | #include "../encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 30 | #include "../encoding/block-helpers.hpp" |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace nfd { |
| 34 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 35 | /** |
| 36 | * \ingroup management |
| 37 | * \brief represents Face status |
| 38 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 39 | */ |
| 40 | class FaceStatus : public FaceFlagsTraits<FaceStatus> |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | class Error : public Tlv::Error |
| 44 | { |
| 45 | public: |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 46 | explicit |
| 47 | Error(const std::string& what) |
| 48 | : Tlv::Error(what) |
| 49 | { |
| 50 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 53 | FaceStatus(); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 54 | |
| 55 | explicit |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 56 | FaceStatus(const Block& block) |
| 57 | { |
| 58 | this->wireDecode(block); |
| 59 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 61 | /** \brief prepend FaceStatus to the encoder |
| 62 | */ |
| 63 | template<bool T> |
| 64 | size_t |
| 65 | wireEncode(EncodingImpl<T>& encoder) const; |
| 66 | |
| 67 | /** \brief encode FaceStatus |
| 68 | */ |
| 69 | const Block& |
| 70 | wireEncode() const; |
| 71 | |
| 72 | /** \brief decode FaceStatus |
| 73 | */ |
| 74 | void |
| 75 | wireDecode(const Block& wire); |
| 76 | |
| 77 | public: // getters & setters |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 78 | uint64_t |
| 79 | getFaceId() const |
| 80 | { |
| 81 | return m_faceId; |
| 82 | } |
| 83 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 84 | FaceStatus& |
| 85 | setFaceId(uint64_t faceId) |
| 86 | { |
| 87 | m_wire.reset(); |
| 88 | m_faceId = faceId; |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | const std::string& |
| 93 | getRemoteUri() const |
| 94 | { |
| 95 | return m_remoteUri; |
| 96 | } |
| 97 | |
| 98 | FaceStatus& |
| 99 | setRemoteUri(const std::string& remoteUri) |
| 100 | { |
| 101 | m_wire.reset(); |
| 102 | m_remoteUri = remoteUri; |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | const std::string& |
| 107 | getLocalUri() const |
| 108 | { |
| 109 | return m_localUri; |
| 110 | } |
| 111 | |
| 112 | FaceStatus& |
| 113 | setLocalUri(const std::string& localUri) |
| 114 | { |
| 115 | m_wire.reset(); |
| 116 | m_localUri = localUri; |
| 117 | return *this; |
| 118 | } |
| 119 | |
| 120 | uint64_t |
| 121 | getFlags() const |
| 122 | { |
| 123 | return m_flags; |
| 124 | } |
| 125 | |
| 126 | FaceStatus& |
| 127 | setFlags(uint64_t flags) |
| 128 | { |
| 129 | m_wire.reset(); |
| 130 | m_flags = flags; |
| 131 | return *this; |
| 132 | } |
| 133 | |
| 134 | uint64_t |
| 135 | getNInInterests() const |
| 136 | { |
| 137 | return m_nInInterests; |
| 138 | } |
| 139 | |
| 140 | FaceStatus& |
| 141 | setNInInterests(uint64_t nInInterests) |
| 142 | { |
| 143 | m_wire.reset(); |
| 144 | m_nInInterests = nInInterests; |
| 145 | return *this; |
| 146 | } |
| 147 | |
| 148 | uint64_t |
| 149 | getNInDatas() const |
| 150 | { |
| 151 | return m_nInDatas; |
| 152 | } |
| 153 | |
| 154 | FaceStatus& |
| 155 | setNInDatas(uint64_t nInDatas) |
| 156 | { |
| 157 | m_wire.reset(); |
| 158 | m_nInDatas = nInDatas; |
| 159 | return *this; |
| 160 | } |
| 161 | |
| 162 | uint64_t |
| 163 | getNOutInterests() const |
| 164 | { |
| 165 | return m_nOutInterests; |
| 166 | } |
| 167 | |
| 168 | FaceStatus& |
| 169 | setNOutInterests(uint64_t nOutInterests) |
| 170 | { |
| 171 | m_wire.reset(); |
| 172 | m_nOutInterests = nOutInterests; |
| 173 | return *this; |
| 174 | } |
| 175 | |
| 176 | uint64_t |
| 177 | getNOutDatas() const |
| 178 | { |
| 179 | return m_nOutDatas; |
| 180 | } |
| 181 | |
| 182 | FaceStatus& |
| 183 | setNOutDatas(uint64_t nOutDatas) |
| 184 | { |
| 185 | m_wire.reset(); |
| 186 | m_nOutDatas = nOutDatas; |
| 187 | return *this; |
| 188 | } |
| 189 | |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 190 | private: |
| 191 | uint64_t m_faceId; |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 192 | std::string m_remoteUri; |
| 193 | std::string m_localUri; |
| 194 | uint64_t m_flags; |
| 195 | uint64_t m_nInInterests; |
| 196 | uint64_t m_nInDatas; |
| 197 | uint64_t m_nOutInterests; |
| 198 | uint64_t m_nOutDatas; |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 199 | |
| 200 | mutable Block m_wire; |
| 201 | }; |
| 202 | |
| 203 | inline |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 204 | FaceStatus::FaceStatus() |
| 205 | : m_faceId(0) |
| 206 | , m_flags(0) |
| 207 | , m_nInInterests(0) |
| 208 | , m_nInDatas(0) |
| 209 | , m_nOutInterests(0) |
| 210 | , m_nOutDatas(0) |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 211 | { |
| 212 | } |
| 213 | |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 214 | template<bool T> |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 215 | inline size_t |
| 216 | FaceStatus::wireEncode(EncodingImpl<T>& encoder) const |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 217 | { |
| 218 | size_t totalLength = 0; |
| 219 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 220 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 221 | tlv::nfd::NOutDatas, m_nOutDatas); |
| 222 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 223 | tlv::nfd::NOutInterests, m_nOutInterests); |
| 224 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 225 | tlv::nfd::NInDatas, m_nInDatas); |
| 226 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 227 | tlv::nfd::NInInterests, m_nInInterests); |
| 228 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 229 | tlv::nfd::FaceFlags, m_flags); |
| 230 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 231 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 232 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri, |
| 233 | reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size()); |
| 234 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 235 | tlv::nfd::FaceId, m_faceId); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 236 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 237 | totalLength += encoder.prependVarNumber(totalLength); |
| 238 | totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 239 | return totalLength; |
| 240 | } |
| 241 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 242 | inline const Block& |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 243 | FaceStatus::wireEncode() const |
| 244 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 245 | if (m_wire.hasWire()) |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 246 | return m_wire; |
| 247 | |
| 248 | EncodingEstimator estimator; |
| 249 | size_t estimatedSize = wireEncode(estimator); |
| 250 | |
| 251 | EncodingBuffer buffer(estimatedSize, 0); |
| 252 | wireEncode(buffer); |
| 253 | |
| 254 | m_wire = buffer.block(); |
| 255 | return m_wire; |
| 256 | } |
| 257 | |
| 258 | inline void |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 259 | FaceStatus::wireDecode(const Block& block) |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 260 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 261 | if (block.type() != tlv::nfd::FaceStatus) { |
| 262 | throw Error("expecting FaceStatus block"); |
| 263 | } |
| 264 | m_wire = block; |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 265 | m_wire.parse(); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 266 | Block::element_const_iterator val = m_wire.elements_begin(); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 267 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 268 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 269 | m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 270 | ++val; |
| 271 | } |
| 272 | else { |
| 273 | throw Error("missing required FaceId field"); |
| 274 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 275 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 276 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) { |
| 277 | m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 278 | ++val; |
| 279 | } |
| 280 | else { |
| 281 | throw Error("missing required Uri field"); |
| 282 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 283 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 284 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 285 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 286 | ++val; |
| 287 | } |
| 288 | else { |
| 289 | throw Error("missing required LocalUri field"); |
| 290 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 291 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 292 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) { |
| 293 | m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 294 | ++val; |
| 295 | } |
| 296 | else { |
| 297 | throw Error("missing required FaceFlags field"); |
| 298 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 299 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 300 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) { |
| 301 | m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 302 | ++val; |
| 303 | } |
| 304 | else { |
| 305 | throw Error("missing required NInInterests field"); |
| 306 | } |
| 307 | |
| 308 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) { |
| 309 | m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 310 | ++val; |
| 311 | } |
| 312 | else { |
| 313 | throw Error("missing required NInDatas field"); |
| 314 | } |
| 315 | |
| 316 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) { |
| 317 | m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 318 | ++val; |
| 319 | } |
| 320 | else { |
| 321 | throw Error("missing required NOutInterests field"); |
| 322 | } |
| 323 | |
| 324 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) { |
| 325 | m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 326 | ++val; |
| 327 | } |
| 328 | else { |
| 329 | throw Error("missing required NOutDatas field"); |
| 330 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | inline std::ostream& |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 334 | operator<<(std::ostream& os, const FaceStatus& status) |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 335 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 336 | os << "FaceStatus(" |
| 337 | << "FaceID: " << status.getFaceId() << ", " |
| 338 | << "RemoteUri: " << status.getRemoteUri() << ", " |
| 339 | << "LocalUri: " << status.getLocalUri() << ", " |
| 340 | << "Flags: " << status.getFlags() << ", " |
| 341 | << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas() |
| 342 | << "|" << status.getNOutInterests() << "|" << status.getNOutDatas() |
| 343 | << ")"; |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 344 | return os; |
| 345 | } |
| 346 | |
| 347 | } // namespace nfd |
| 348 | } // namespace ndn |
| 349 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 350 | #endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP |