blob: 307c0d90641993f2d0ef8863b050c282b93f5a06 [file] [log] [blame]
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev44b438a2014-03-19 12:51:49 -07002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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.
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070011 */
12
13#ifndef NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
14#define NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
15
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070016// This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own.
17#include "nfd-face-flags.hpp"
18
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070019#include "../encoding/tlv-nfd.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070020#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070021
22namespace ndn {
23namespace nfd {
24
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070025enum FaceEventKind {
26 FACE_EVENT_CREATED = 1,
27 FACE_EVENT_DESTROYED = 2
28};
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070029
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070030/** \brief represents a Face status change notification
31 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Status-Change-Notification
32 */
33class FaceEventNotification : public FaceFlagsTraits<FaceEventNotification>
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070034{
35public:
36 class Error : public Tlv::Error
37 {
38 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070039 explicit
40 Error(const std::string& what)
41 : Tlv::Error(what)
42 {
43 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070044 };
45
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070046 FaceEventNotification();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070047
48 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070049 FaceEventNotification(const Block& block)
50 {
51 this->wireDecode(block);
52 }
53
54 /** \brief prepend FaceEventNotification to the encoder
55 */
56 template<bool T>
57 size_t
58 wireEncode(EncodingImpl<T>& encoder) const;
59
60 /** \brief encode FaceEventNotification
61 */
62 const Block&
63 wireEncode() const;
64
65 /** \brief decode FaceEventNotification
66 */
67 void
68 wireDecode(const Block& wire);
69
70public: // getters & setters
71 FaceEventKind
72 getKind() const
73 {
74 return m_kind;
75 }
76
77 FaceEventNotification&
78 setKind(FaceEventKind kind)
79 {
80 m_wire.reset();
81 m_kind = kind;
82 return *this;
83 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070084
85 uint64_t
86 getFaceId() const
87 {
88 return m_faceId;
89 }
90
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070091 FaceEventNotification&
92 setFaceId(uint64_t faceId)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070093 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070094 m_wire.reset();
95 m_faceId = faceId;
96 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070097 }
98
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070099 const std::string&
100 getRemoteUri() const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700101 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700102 return m_remoteUri;
103 }
104
105 FaceEventNotification&
106 setRemoteUri(const std::string& remoteUri)
107 {
108 m_wire.reset();
109 m_remoteUri = remoteUri;
110 return *this;
111 }
112
113 const std::string&
114 getLocalUri() const
115 {
116 return m_localUri;
117 }
118
119 FaceEventNotification&
120 setLocalUri(const std::string& localUri)
121 {
122 m_wire.reset();
123 m_localUri = localUri;
124 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700125 }
126
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700127 uint64_t
128 getFlags() const
129 {
130 return m_flags;
131 }
132
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700133 FaceEventNotification&
134 setFlags(uint64_t flags)
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700135 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700136 m_wire.reset();
137 m_flags = flags;
138 return *this;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700139 }
140
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700141private:
142 FaceEventKind m_kind;
143 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700144 std::string m_remoteUri;
145 std::string m_localUri;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700146 uint64_t m_flags;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700147
148 mutable Block m_wire;
149};
150
151inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700152FaceEventNotification::FaceEventNotification()
153 : m_kind(static_cast<FaceEventKind>(0))
154 , m_faceId(0)
155 , m_flags(0)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700156{
157}
158
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700159template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700160inline size_t
161FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700162{
163 size_t totalLength = 0;
164
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700165 totalLength += prependNonNegativeIntegerBlock(encoder,
166 tlv::nfd::FaceFlags, m_flags);
167 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
168 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
169 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
170 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
171 totalLength += prependNonNegativeIntegerBlock(encoder,
172 tlv::nfd::FaceId, m_faceId);
173 totalLength += prependNonNegativeIntegerBlock(encoder,
174 tlv::nfd::FaceEventKind, m_kind);
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700175
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700176 totalLength += encoder.prependVarNumber(totalLength);
177 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700178 return totalLength;
179}
180
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600181inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700182FaceEventNotification::wireEncode() const
183{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700184 if (m_wire.hasWire())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700185 return m_wire;
186
187 EncodingEstimator estimator;
188 size_t estimatedSize = wireEncode(estimator);
189
190 EncodingBuffer buffer(estimatedSize, 0);
191 wireEncode(buffer);
192
193 m_wire = buffer.block();
194 return m_wire;
195}
196
197inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700198FaceEventNotification::wireDecode(const Block& block)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700199{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700200 if (block.type() != tlv::nfd::FaceEventNotification) {
201 throw Error("expecting FaceEventNotification block");
202 }
203 m_wire = block;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700204 m_wire.parse();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700205 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700206
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700207 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
208 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
209 ++val;
210 }
211 else {
212 throw Error("missing required FaceEventKind field");
213 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700214
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700215 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
216 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
217 ++val;
218 }
219 else {
220 throw Error("missing required FaceId field");
221 }
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700222
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700223 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
224 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
225 ++val;
226 }
227 else {
228 throw Error("missing required Uri field");
229 }
230
231 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
232 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
233 ++val;
234 }
235 else {
236 throw Error("missing required LocalUri field");
237 }
238
239 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
240 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
241 ++val;
242 }
243 else {
244 throw Error("missing required FaceFlags field");
245 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700246}
247
248inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700249operator<<(std::ostream& os, const FaceEventNotification& notification)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700250{
251 os << "FaceEventNotification(";
252
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700253 switch (notification.getKind())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700254 {
255 case FACE_EVENT_CREATED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700256 os << "Kind: created, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700257 break;
258 case FACE_EVENT_DESTROYED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700259 os << "Kind: destroyed, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700260 break;
261 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700262
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700263 os << "FaceID: " << notification.getFaceId() << ", "
264 << "RemoteUri: " << notification.getRemoteUri() << ", "
265 << "LocalUri: " << notification.getLocalUri() << ", "
266 << "Flags: " << notification.getFlags()
267 << ")";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700268 return os;
269}
270
271} // namespace nfd
272} // namespace ndn
273
274#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP