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