blob: 78a20663821f706d4543f235aa4d9be0a917ca27 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyev4671bf72014-05-19 09:01:37 -040020 */
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
31namespace ndn {
32namespace 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 */
39class ChannelStatus
40{
41public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060042 class Error : public tlv::Error
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040043 {
44 public:
45 explicit
46 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060047 : tlv::Error(what)
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040048 {
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
72public: // 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
87private:
88 std::string m_localUri;
89
90 mutable Block m_wire;
91};
92
93
94template<bool T>
95inline size_t
96ChannelStatus::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
108inline const Block&
109ChannelStatus::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
124inline void
125ChannelStatus::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