blob: 7afb852c70f1ee458b4b2973b5cf0b8339f95bdd [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev44b438a2014-03-19 12:51:49 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
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 Afanasyev44b438a2014-03-19 12:51:49 -070020 */
21
22#ifndef NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
23#define NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
24
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070025// This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own.
26#include "nfd-face-flags.hpp"
27
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070028#include "../encoding/tlv-nfd.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070029#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030#include "../encoding/block-helpers.hpp"
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070031
32namespace ndn {
33namespace nfd {
34
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040035/**
36 * \ingroup management
37 */
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070038enum FaceEventKind {
39 FACE_EVENT_CREATED = 1,
40 FACE_EVENT_DESTROYED = 2
41};
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070042
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040043/**
44 * \ingroup management
45 * \brief represents a Face status change notification
46 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Status-Change-Notification
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070047 */
48class FaceEventNotification : public FaceFlagsTraits<FaceEventNotification>
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070049{
50public:
51 class Error : public Tlv::Error
52 {
53 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070054 explicit
55 Error(const std::string& what)
56 : Tlv::Error(what)
57 {
58 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070059 };
60
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070061 FaceEventNotification();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070062
63 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070064 FaceEventNotification(const Block& block)
65 {
66 this->wireDecode(block);
67 }
68
69 /** \brief prepend FaceEventNotification to the encoder
70 */
71 template<bool T>
72 size_t
73 wireEncode(EncodingImpl<T>& encoder) const;
74
75 /** \brief encode FaceEventNotification
76 */
77 const Block&
78 wireEncode() const;
79
80 /** \brief decode FaceEventNotification
81 */
82 void
83 wireDecode(const Block& wire);
84
85public: // getters & setters
86 FaceEventKind
87 getKind() const
88 {
89 return m_kind;
90 }
91
92 FaceEventNotification&
93 setKind(FaceEventKind kind)
94 {
95 m_wire.reset();
96 m_kind = kind;
97 return *this;
98 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070099
100 uint64_t
101 getFaceId() const
102 {
103 return m_faceId;
104 }
105
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700106 FaceEventNotification&
107 setFaceId(uint64_t faceId)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700108 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700109 m_wire.reset();
110 m_faceId = faceId;
111 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700112 }
113
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700114 const std::string&
115 getRemoteUri() const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700116 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700117 return m_remoteUri;
118 }
119
120 FaceEventNotification&
121 setRemoteUri(const std::string& remoteUri)
122 {
123 m_wire.reset();
124 m_remoteUri = remoteUri;
125 return *this;
126 }
127
128 const std::string&
129 getLocalUri() const
130 {
131 return m_localUri;
132 }
133
134 FaceEventNotification&
135 setLocalUri(const std::string& localUri)
136 {
137 m_wire.reset();
138 m_localUri = localUri;
139 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700140 }
141
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700142 uint64_t
143 getFlags() const
144 {
145 return m_flags;
146 }
147
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700148 FaceEventNotification&
149 setFlags(uint64_t flags)
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700150 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700151 m_wire.reset();
152 m_flags = flags;
153 return *this;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700154 }
155
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700156private:
157 FaceEventKind m_kind;
158 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700159 std::string m_remoteUri;
160 std::string m_localUri;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700161 uint64_t m_flags;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700162
163 mutable Block m_wire;
164};
165
166inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700167FaceEventNotification::FaceEventNotification()
168 : m_kind(static_cast<FaceEventKind>(0))
169 , m_faceId(0)
170 , m_flags(0)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700171{
172}
173
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700174template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700175inline size_t
176FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700177{
178 size_t totalLength = 0;
179
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700180 totalLength += prependNonNegativeIntegerBlock(encoder,
181 tlv::nfd::FaceFlags, m_flags);
182 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
183 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
184 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
185 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
186 totalLength += prependNonNegativeIntegerBlock(encoder,
187 tlv::nfd::FaceId, m_faceId);
188 totalLength += prependNonNegativeIntegerBlock(encoder,
189 tlv::nfd::FaceEventKind, m_kind);
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700190
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700191 totalLength += encoder.prependVarNumber(totalLength);
192 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700193 return totalLength;
194}
195
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600196inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700197FaceEventNotification::wireEncode() const
198{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700199 if (m_wire.hasWire())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700200 return m_wire;
201
202 EncodingEstimator estimator;
203 size_t estimatedSize = wireEncode(estimator);
204
205 EncodingBuffer buffer(estimatedSize, 0);
206 wireEncode(buffer);
207
208 m_wire = buffer.block();
209 return m_wire;
210}
211
212inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700213FaceEventNotification::wireDecode(const Block& block)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700214{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700215 if (block.type() != tlv::nfd::FaceEventNotification) {
216 throw Error("expecting FaceEventNotification block");
217 }
218 m_wire = block;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700219 m_wire.parse();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700220 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700221
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700222 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
223 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
224 ++val;
225 }
226 else {
227 throw Error("missing required FaceEventKind field");
228 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700229
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700230 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
231 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
232 ++val;
233 }
234 else {
235 throw Error("missing required FaceId field");
236 }
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700237
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700238 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
239 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
240 ++val;
241 }
242 else {
243 throw Error("missing required Uri field");
244 }
245
246 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
247 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
248 ++val;
249 }
250 else {
251 throw Error("missing required LocalUri field");
252 }
253
254 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
255 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
256 ++val;
257 }
258 else {
259 throw Error("missing required FaceFlags field");
260 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700261}
262
263inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700264operator<<(std::ostream& os, const FaceEventNotification& notification)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700265{
266 os << "FaceEventNotification(";
267
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700268 switch (notification.getKind())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700269 {
270 case FACE_EVENT_CREATED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700271 os << "Kind: created, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700272 break;
273 case FACE_EVENT_DESTROYED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700274 os << "Kind: destroyed, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700275 break;
276 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700277
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700278 os << "FaceID: " << notification.getFaceId() << ", "
279 << "RemoteUri: " << notification.getRemoteUri() << ", "
280 << "LocalUri: " << notification.getLocalUri() << ", "
281 << "Flags: " << notification.getFlags()
282 << ")";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700283 return os;
284}
285
286} // namespace nfd
287} // namespace ndn
288
289#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP