blob: 6432d3052b7952ee2c46fbce6fbdef322afc656e [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
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070026enum FaceEventKind {
27 FACE_EVENT_CREATED = 1,
28 FACE_EVENT_DESTROYED = 2
29};
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070030
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070031/** \brief represents a Face status change notification
32 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Status-Change-Notification
33 */
34class FaceEventNotification : public FaceFlagsTraits<FaceEventNotification>
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070035{
36public:
37 class Error : public Tlv::Error
38 {
39 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070040 explicit
41 Error(const std::string& what)
42 : Tlv::Error(what)
43 {
44 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070045 };
46
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070047 FaceEventNotification();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070048
49 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070050 FaceEventNotification(const Block& block)
51 {
52 this->wireDecode(block);
53 }
54
55 /** \brief prepend FaceEventNotification to the encoder
56 */
57 template<bool T>
58 size_t
59 wireEncode(EncodingImpl<T>& encoder) const;
60
61 /** \brief encode FaceEventNotification
62 */
63 const Block&
64 wireEncode() const;
65
66 /** \brief decode FaceEventNotification
67 */
68 void
69 wireDecode(const Block& wire);
70
71public: // getters & setters
72 FaceEventKind
73 getKind() const
74 {
75 return m_kind;
76 }
77
78 FaceEventNotification&
79 setKind(FaceEventKind kind)
80 {
81 m_wire.reset();
82 m_kind = kind;
83 return *this;
84 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070085
86 uint64_t
87 getFaceId() const
88 {
89 return m_faceId;
90 }
91
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070092 FaceEventNotification&
93 setFaceId(uint64_t faceId)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070094 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070095 m_wire.reset();
96 m_faceId = faceId;
97 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070098 }
99
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700100 const std::string&
101 getRemoteUri() const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700102 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700103 return m_remoteUri;
104 }
105
106 FaceEventNotification&
107 setRemoteUri(const std::string& remoteUri)
108 {
109 m_wire.reset();
110 m_remoteUri = remoteUri;
111 return *this;
112 }
113
114 const std::string&
115 getLocalUri() const
116 {
117 return m_localUri;
118 }
119
120 FaceEventNotification&
121 setLocalUri(const std::string& localUri)
122 {
123 m_wire.reset();
124 m_localUri = localUri;
125 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700126 }
127
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700128 uint64_t
129 getFlags() const
130 {
131 return m_flags;
132 }
133
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700134 FaceEventNotification&
135 setFlags(uint64_t flags)
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700136 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700137 m_wire.reset();
138 m_flags = flags;
139 return *this;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700140 }
141
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700142private:
143 FaceEventKind m_kind;
144 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700145 std::string m_remoteUri;
146 std::string m_localUri;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700147 uint64_t m_flags;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700148
149 mutable Block m_wire;
150};
151
152inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700153FaceEventNotification::FaceEventNotification()
154 : m_kind(static_cast<FaceEventKind>(0))
155 , m_faceId(0)
156 , m_flags(0)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700157{
158}
159
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700160template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700161inline size_t
162FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700163{
164 size_t totalLength = 0;
165
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700166 totalLength += prependNonNegativeIntegerBlock(encoder,
167 tlv::nfd::FaceFlags, m_flags);
168 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
169 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
170 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
171 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
172 totalLength += prependNonNegativeIntegerBlock(encoder,
173 tlv::nfd::FaceId, m_faceId);
174 totalLength += prependNonNegativeIntegerBlock(encoder,
175 tlv::nfd::FaceEventKind, m_kind);
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700176
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700177 totalLength += encoder.prependVarNumber(totalLength);
178 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700179 return totalLength;
180}
181
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600182inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700183FaceEventNotification::wireEncode() const
184{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700185 if (m_wire.hasWire())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700186 return m_wire;
187
188 EncodingEstimator estimator;
189 size_t estimatedSize = wireEncode(estimator);
190
191 EncodingBuffer buffer(estimatedSize, 0);
192 wireEncode(buffer);
193
194 m_wire = buffer.block();
195 return m_wire;
196}
197
198inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700199FaceEventNotification::wireDecode(const Block& block)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700200{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700201 if (block.type() != tlv::nfd::FaceEventNotification) {
202 throw Error("expecting FaceEventNotification block");
203 }
204 m_wire = block;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700205 m_wire.parse();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700206 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700207
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700208 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
209 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
210 ++val;
211 }
212 else {
213 throw Error("missing required FaceEventKind field");
214 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700215
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700216 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
217 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
218 ++val;
219 }
220 else {
221 throw Error("missing required FaceId field");
222 }
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700223
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700224 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
225 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
226 ++val;
227 }
228 else {
229 throw Error("missing required Uri field");
230 }
231
232 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
233 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
234 ++val;
235 }
236 else {
237 throw Error("missing required LocalUri field");
238 }
239
240 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
241 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
242 ++val;
243 }
244 else {
245 throw Error("missing required FaceFlags field");
246 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700247}
248
249inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700250operator<<(std::ostream& os, const FaceEventNotification& notification)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700251{
252 os << "FaceEventNotification(";
253
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700254 switch (notification.getKind())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700255 {
256 case FACE_EVENT_CREATED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700257 os << "Kind: created, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700258 break;
259 case FACE_EVENT_DESTROYED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700260 os << "Kind: destroyed, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700261 break;
262 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700263
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700264 os << "FaceID: " << notification.getFaceId() << ", "
265 << "RemoteUri: " << notification.getRemoteUri() << ", "
266 << "LocalUri: " << notification.getLocalUri() << ", "
267 << "Flags: " << notification.getFlags()
268 << ")";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700269 return os;
270}
271
272} // namespace nfd
273} // namespace ndn
274
275#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP