blob: 7f5b5fa8e6b795838ecb2e436040baa88c940029 [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 Afanasyev258ec2b2014-05-14 16:15:37 -070021#include "../encoding/block-helpers.hpp"
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070022
23namespace ndn {
24namespace nfd {
25
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040026/**
27 * \ingroup management
28 */
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070029enum FaceEventKind {
30 FACE_EVENT_CREATED = 1,
31 FACE_EVENT_DESTROYED = 2
32};
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070033
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040034/**
35 * \ingroup management
36 * \brief represents a Face status change notification
37 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Status-Change-Notification
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070038 */
39class FaceEventNotification : public FaceFlagsTraits<FaceEventNotification>
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070040{
41public:
42 class Error : public Tlv::Error
43 {
44 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070045 explicit
46 Error(const std::string& what)
47 : Tlv::Error(what)
48 {
49 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070050 };
51
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070052 FaceEventNotification();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070053
54 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070055 FaceEventNotification(const Block& block)
56 {
57 this->wireDecode(block);
58 }
59
60 /** \brief prepend FaceEventNotification to the encoder
61 */
62 template<bool T>
63 size_t
64 wireEncode(EncodingImpl<T>& encoder) const;
65
66 /** \brief encode FaceEventNotification
67 */
68 const Block&
69 wireEncode() const;
70
71 /** \brief decode FaceEventNotification
72 */
73 void
74 wireDecode(const Block& wire);
75
76public: // getters & setters
77 FaceEventKind
78 getKind() const
79 {
80 return m_kind;
81 }
82
83 FaceEventNotification&
84 setKind(FaceEventKind kind)
85 {
86 m_wire.reset();
87 m_kind = kind;
88 return *this;
89 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070090
91 uint64_t
92 getFaceId() const
93 {
94 return m_faceId;
95 }
96
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070097 FaceEventNotification&
98 setFaceId(uint64_t faceId)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070099 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700100 m_wire.reset();
101 m_faceId = faceId;
102 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700103 }
104
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700105 const std::string&
106 getRemoteUri() const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700107 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700108 return m_remoteUri;
109 }
110
111 FaceEventNotification&
112 setRemoteUri(const std::string& remoteUri)
113 {
114 m_wire.reset();
115 m_remoteUri = remoteUri;
116 return *this;
117 }
118
119 const std::string&
120 getLocalUri() const
121 {
122 return m_localUri;
123 }
124
125 FaceEventNotification&
126 setLocalUri(const std::string& localUri)
127 {
128 m_wire.reset();
129 m_localUri = localUri;
130 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700131 }
132
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700133 uint64_t
134 getFlags() const
135 {
136 return m_flags;
137 }
138
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700139 FaceEventNotification&
140 setFlags(uint64_t flags)
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700141 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700142 m_wire.reset();
143 m_flags = flags;
144 return *this;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700145 }
146
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700147private:
148 FaceEventKind m_kind;
149 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700150 std::string m_remoteUri;
151 std::string m_localUri;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700152 uint64_t m_flags;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700153
154 mutable Block m_wire;
155};
156
157inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700158FaceEventNotification::FaceEventNotification()
159 : m_kind(static_cast<FaceEventKind>(0))
160 , m_faceId(0)
161 , m_flags(0)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700162{
163}
164
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700165template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700166inline size_t
167FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700168{
169 size_t totalLength = 0;
170
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700171 totalLength += prependNonNegativeIntegerBlock(encoder,
172 tlv::nfd::FaceFlags, m_flags);
173 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
174 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
175 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
176 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
177 totalLength += prependNonNegativeIntegerBlock(encoder,
178 tlv::nfd::FaceId, m_faceId);
179 totalLength += prependNonNegativeIntegerBlock(encoder,
180 tlv::nfd::FaceEventKind, m_kind);
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700181
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700182 totalLength += encoder.prependVarNumber(totalLength);
183 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700184 return totalLength;
185}
186
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600187inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700188FaceEventNotification::wireEncode() const
189{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700190 if (m_wire.hasWire())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700191 return m_wire;
192
193 EncodingEstimator estimator;
194 size_t estimatedSize = wireEncode(estimator);
195
196 EncodingBuffer buffer(estimatedSize, 0);
197 wireEncode(buffer);
198
199 m_wire = buffer.block();
200 return m_wire;
201}
202
203inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700204FaceEventNotification::wireDecode(const Block& block)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700205{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700206 if (block.type() != tlv::nfd::FaceEventNotification) {
207 throw Error("expecting FaceEventNotification block");
208 }
209 m_wire = block;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700210 m_wire.parse();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700211 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700212
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700213 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
214 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
215 ++val;
216 }
217 else {
218 throw Error("missing required FaceEventKind field");
219 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700220
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700221 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
222 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
223 ++val;
224 }
225 else {
226 throw Error("missing required FaceId field");
227 }
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700228
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700229 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
230 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
231 ++val;
232 }
233 else {
234 throw Error("missing required Uri field");
235 }
236
237 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
238 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
239 ++val;
240 }
241 else {
242 throw Error("missing required LocalUri field");
243 }
244
245 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
246 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
247 ++val;
248 }
249 else {
250 throw Error("missing required FaceFlags field");
251 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700252}
253
254inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700255operator<<(std::ostream& os, const FaceEventNotification& notification)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700256{
257 os << "FaceEventNotification(";
258
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700259 switch (notification.getKind())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700260 {
261 case FACE_EVENT_CREATED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700262 os << "Kind: created, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700263 break;
264 case FACE_EVENT_DESTROYED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700265 os << "Kind: destroyed, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700266 break;
267 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700268
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700269 os << "FaceID: " << notification.getFaceId() << ", "
270 << "RemoteUri: " << notification.getRemoteUri() << ", "
271 << "LocalUri: " << notification.getLocalUri() << ", "
272 << "Flags: " << notification.getFlags()
273 << ")";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700274 return os;
275}
276
277} // namespace nfd
278} // namespace ndn
279
280#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP