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