Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 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. |
| 11 | */ |
| 12 | |
| 13 | #ifndef NDN_MANAGEMENT_NFD_CHANNEL_STATUS_HPP |
| 14 | #define NDN_MANAGEMENT_NFD_CHANNEL_STATUS_HPP |
| 15 | |
| 16 | #include "../encoding/tlv-nfd.hpp" |
| 17 | #include "../encoding/encoding-buffer.hpp" |
| 18 | #include "../encoding/block-helpers.hpp" |
| 19 | |
| 20 | #include "../util/time.hpp" |
| 21 | |
| 22 | namespace ndn { |
| 23 | namespace nfd { |
| 24 | |
| 25 | /** |
| 26 | * @ingroup management |
| 27 | * @brief represents NFD Channel Status dataset |
| 28 | * @sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Channel-Dataset |
| 29 | */ |
| 30 | class ChannelStatus |
| 31 | { |
| 32 | public: |
| 33 | class Error : public Tlv::Error |
| 34 | { |
| 35 | public: |
| 36 | explicit |
| 37 | Error(const std::string& what) |
| 38 | : Tlv::Error(what) |
| 39 | { |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | ChannelStatus() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | explicit |
| 48 | ChannelStatus(const Block& payload) |
| 49 | { |
| 50 | this->wireDecode(payload); |
| 51 | } |
| 52 | |
| 53 | template<bool T> |
| 54 | size_t |
| 55 | wireEncode(EncodingImpl<T>& encoder) const; |
| 56 | |
| 57 | const Block& |
| 58 | wireEncode() const; |
| 59 | |
| 60 | void |
| 61 | wireDecode(const Block& wire); |
| 62 | |
| 63 | public: // getters & setters |
| 64 | const std::string& |
| 65 | getLocalUri() const |
| 66 | { |
| 67 | return m_localUri; |
| 68 | } |
| 69 | |
| 70 | ChannelStatus& |
| 71 | setLocalUri(const std::string localUri) |
| 72 | { |
| 73 | m_wire.reset(); |
| 74 | m_localUri = localUri; |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | std::string m_localUri; |
| 80 | |
| 81 | mutable Block m_wire; |
| 82 | }; |
| 83 | |
| 84 | |
| 85 | template<bool T> |
| 86 | inline size_t |
| 87 | ChannelStatus::wireEncode(EncodingImpl<T>& encoder) const |
| 88 | { |
| 89 | size_t totalLength = 0; |
| 90 | |
| 91 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 92 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 93 | |
| 94 | totalLength += encoder.prependVarNumber(totalLength); |
| 95 | totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus); |
| 96 | return totalLength; |
| 97 | } |
| 98 | |
| 99 | inline const Block& |
| 100 | ChannelStatus::wireEncode() const |
| 101 | { |
| 102 | if (m_wire.hasWire()) |
| 103 | return m_wire; |
| 104 | |
| 105 | EncodingEstimator estimator; |
| 106 | size_t estimatedSize = wireEncode(estimator); |
| 107 | |
| 108 | EncodingBuffer buffer(estimatedSize, 0); |
| 109 | wireEncode(buffer); |
| 110 | |
| 111 | m_wire = buffer.block(); |
| 112 | return m_wire; |
| 113 | } |
| 114 | |
| 115 | inline void |
| 116 | ChannelStatus::wireDecode(const Block& block) |
| 117 | { |
| 118 | if (block.type() != tlv::nfd::ChannelStatus) { |
| 119 | throw Error("expecting ChannelStatus block"); |
| 120 | } |
| 121 | m_wire = block; |
| 122 | m_wire.parse(); |
| 123 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 124 | |
| 125 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 126 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 127 | ++val; |
| 128 | } |
| 129 | else { |
| 130 | throw Error("missing required LocalUri field"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace nfd |
| 135 | } // namespace ndn |
| 136 | |
| 137 | #endif // NDN_MANAGEMENT_NFD_CHANNEL_STATUS_HPP |