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 | |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 172 | private: |
| 173 | uint64_t m_faceId; |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 174 | std::string m_remoteUri; |
| 175 | std::string m_localUri; |
| 176 | uint64_t m_flags; |
| 177 | uint64_t m_nInInterests; |
| 178 | uint64_t m_nInDatas; |
| 179 | uint64_t m_nOutInterests; |
| 180 | uint64_t m_nOutDatas; |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 181 | |
| 182 | mutable Block m_wire; |
| 183 | }; |
| 184 | |
| 185 | inline |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 186 | FaceStatus::FaceStatus() |
| 187 | : m_faceId(0) |
| 188 | , m_flags(0) |
| 189 | , m_nInInterests(0) |
| 190 | , m_nInDatas(0) |
| 191 | , m_nOutInterests(0) |
| 192 | , m_nOutDatas(0) |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 193 | { |
| 194 | } |
| 195 | |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 196 | template<bool T> |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 197 | inline size_t |
| 198 | FaceStatus::wireEncode(EncodingImpl<T>& encoder) const |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 199 | { |
| 200 | size_t totalLength = 0; |
| 201 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 202 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 203 | tlv::nfd::NOutDatas, m_nOutDatas); |
| 204 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 205 | tlv::nfd::NOutInterests, m_nOutInterests); |
| 206 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 207 | tlv::nfd::NInDatas, m_nInDatas); |
| 208 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 209 | tlv::nfd::NInInterests, m_nInInterests); |
| 210 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 211 | tlv::nfd::FaceFlags, m_flags); |
| 212 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 213 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 214 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri, |
| 215 | reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size()); |
| 216 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 217 | tlv::nfd::FaceId, m_faceId); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 218 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 219 | totalLength += encoder.prependVarNumber(totalLength); |
| 220 | totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 221 | return totalLength; |
| 222 | } |
| 223 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 224 | inline const Block& |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 225 | FaceStatus::wireEncode() const |
| 226 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 227 | if (m_wire.hasWire()) |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 228 | return m_wire; |
| 229 | |
| 230 | EncodingEstimator estimator; |
| 231 | size_t estimatedSize = wireEncode(estimator); |
| 232 | |
| 233 | EncodingBuffer buffer(estimatedSize, 0); |
| 234 | wireEncode(buffer); |
| 235 | |
| 236 | m_wire = buffer.block(); |
| 237 | return m_wire; |
| 238 | } |
| 239 | |
| 240 | inline void |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 241 | FaceStatus::wireDecode(const Block& block) |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 242 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 243 | if (block.type() != tlv::nfd::FaceStatus) { |
| 244 | throw Error("expecting FaceStatus block"); |
| 245 | } |
| 246 | m_wire = block; |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 247 | m_wire.parse(); |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 248 | Block::element_const_iterator val = m_wire.elements_begin(); |
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 (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 251 | m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 252 | ++val; |
| 253 | } |
| 254 | else { |
| 255 | throw Error("missing required FaceId field"); |
| 256 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 257 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 258 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) { |
| 259 | m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 260 | ++val; |
| 261 | } |
| 262 | else { |
| 263 | throw Error("missing required Uri field"); |
| 264 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 265 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 266 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 267 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 268 | ++val; |
| 269 | } |
| 270 | else { |
| 271 | throw Error("missing required LocalUri field"); |
| 272 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 273 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 274 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) { |
| 275 | m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 276 | ++val; |
| 277 | } |
| 278 | else { |
| 279 | throw Error("missing required FaceFlags field"); |
| 280 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 281 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 282 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) { |
| 283 | m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 284 | ++val; |
| 285 | } |
| 286 | else { |
| 287 | throw Error("missing required NInInterests field"); |
| 288 | } |
| 289 | |
| 290 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) { |
| 291 | m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 292 | ++val; |
| 293 | } |
| 294 | else { |
| 295 | throw Error("missing required NInDatas field"); |
| 296 | } |
| 297 | |
| 298 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) { |
| 299 | m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 300 | ++val; |
| 301 | } |
| 302 | else { |
| 303 | throw Error("missing required NOutInterests field"); |
| 304 | } |
| 305 | |
| 306 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) { |
| 307 | m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 308 | ++val; |
| 309 | } |
| 310 | else { |
| 311 | throw Error("missing required NOutDatas field"); |
| 312 | } |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | inline std::ostream& |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 316 | operator<<(std::ostream& os, const FaceStatus& status) |
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 | os << "FaceStatus(" |
| 319 | << "FaceID: " << status.getFaceId() << ", " |
| 320 | << "RemoteUri: " << status.getRemoteUri() << ", " |
| 321 | << "LocalUri: " << status.getLocalUri() << ", " |
| 322 | << "Flags: " << status.getFlags() << ", " |
| 323 | << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas() |
| 324 | << "|" << status.getNOutInterests() << "|" << status.getNOutDatas() |
| 325 | << ")"; |
Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 326 | return os; |
| 327 | } |
| 328 | |
| 329 | } // namespace nfd |
| 330 | } // namespace ndn |
| 331 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 332 | #endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP |