blob: 6ac5aaebc886eb3fc53d71e7452a401955986723 [file] [log] [blame]
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04001/* -*- 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
22namespace ndn {
23namespace 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 */
30class ChannelStatus
31{
32public:
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
63public: // 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
78private:
79 std::string m_localUri;
80
81 mutable Block m_wire;
82};
83
84
85template<bool T>
86inline size_t
87ChannelStatus::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
99inline const Block&
100ChannelStatus::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
115inline void
116ChannelStatus::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