Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame^] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_CHANNEL_STATUS_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_CHANNEL_STATUS_HPP |
| 24 | |
| 25 | #include "../encoding/tlv-nfd.hpp" |
| 26 | #include "../encoding/encoding-buffer.hpp" |
| 27 | #include "../encoding/block-helpers.hpp" |
| 28 | |
| 29 | #include "../util/time.hpp" |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace nfd { |
| 33 | |
| 34 | /** |
| 35 | * @ingroup management |
| 36 | * @brief represents NFD Channel Status dataset |
| 37 | * @sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Channel-Dataset |
| 38 | */ |
| 39 | class ChannelStatus |
| 40 | { |
| 41 | public: |
| 42 | class Error : public Tlv::Error |
| 43 | { |
| 44 | public: |
| 45 | explicit |
| 46 | Error(const std::string& what) |
| 47 | : Tlv::Error(what) |
| 48 | { |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | ChannelStatus() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | explicit |
| 57 | ChannelStatus(const Block& payload) |
| 58 | { |
| 59 | this->wireDecode(payload); |
| 60 | } |
| 61 | |
| 62 | template<bool T> |
| 63 | size_t |
| 64 | wireEncode(EncodingImpl<T>& encoder) const; |
| 65 | |
| 66 | const Block& |
| 67 | wireEncode() const; |
| 68 | |
| 69 | void |
| 70 | wireDecode(const Block& wire); |
| 71 | |
| 72 | public: // getters & setters |
| 73 | const std::string& |
| 74 | getLocalUri() const |
| 75 | { |
| 76 | return m_localUri; |
| 77 | } |
| 78 | |
| 79 | ChannelStatus& |
| 80 | setLocalUri(const std::string localUri) |
| 81 | { |
| 82 | m_wire.reset(); |
| 83 | m_localUri = localUri; |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | std::string m_localUri; |
| 89 | |
| 90 | mutable Block m_wire; |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | template<bool T> |
| 95 | inline size_t |
| 96 | ChannelStatus::wireEncode(EncodingImpl<T>& encoder) const |
| 97 | { |
| 98 | size_t totalLength = 0; |
| 99 | |
| 100 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 101 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 102 | |
| 103 | totalLength += encoder.prependVarNumber(totalLength); |
| 104 | totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus); |
| 105 | return totalLength; |
| 106 | } |
| 107 | |
| 108 | inline const Block& |
| 109 | ChannelStatus::wireEncode() const |
| 110 | { |
| 111 | if (m_wire.hasWire()) |
| 112 | return m_wire; |
| 113 | |
| 114 | EncodingEstimator estimator; |
| 115 | size_t estimatedSize = wireEncode(estimator); |
| 116 | |
| 117 | EncodingBuffer buffer(estimatedSize, 0); |
| 118 | wireEncode(buffer); |
| 119 | |
| 120 | m_wire = buffer.block(); |
| 121 | return m_wire; |
| 122 | } |
| 123 | |
| 124 | inline void |
| 125 | ChannelStatus::wireDecode(const Block& block) |
| 126 | { |
| 127 | if (block.type() != tlv::nfd::ChannelStatus) { |
| 128 | throw Error("expecting ChannelStatus block"); |
| 129 | } |
| 130 | m_wire = block; |
| 131 | m_wire.parse(); |
| 132 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 133 | |
| 134 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 135 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 136 | ++val; |
| 137 | } |
| 138 | else { |
| 139 | throw Error("missing required LocalUri field"); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | } // namespace nfd |
| 144 | } // namespace ndn |
| 145 | |
| 146 | #endif // NDN_MANAGEMENT_NFD_CHANNEL_STATUS_HPP |