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