blob: 53640c83d86c4153d2ad1a9032eccc8efab1054b [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/**
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -07003 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev44b438a2014-03-19 12:51:49 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
8#define NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
9
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070010// This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own.
11#include "nfd-face-flags.hpp"
12
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070013#include "../encoding/tlv-nfd.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070014#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070015
16namespace ndn {
17namespace nfd {
18
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070019enum FaceEventKind {
20 FACE_EVENT_CREATED = 1,
21 FACE_EVENT_DESTROYED = 2
22};
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070023
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070024/** \brief represents a Face status change notification
25 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Status-Change-Notification
26 */
27class FaceEventNotification : public FaceFlagsTraits<FaceEventNotification>
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070028{
29public:
30 class Error : public Tlv::Error
31 {
32 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070033 explicit
34 Error(const std::string& what)
35 : Tlv::Error(what)
36 {
37 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070038 };
39
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070040 FaceEventNotification();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070041
42 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070043 FaceEventNotification(const Block& block)
44 {
45 this->wireDecode(block);
46 }
47
48 /** \brief prepend FaceEventNotification to the encoder
49 */
50 template<bool T>
51 size_t
52 wireEncode(EncodingImpl<T>& encoder) const;
53
54 /** \brief encode FaceEventNotification
55 */
56 const Block&
57 wireEncode() const;
58
59 /** \brief decode FaceEventNotification
60 */
61 void
62 wireDecode(const Block& wire);
63
64public: // getters & setters
65 FaceEventKind
66 getKind() const
67 {
68 return m_kind;
69 }
70
71 FaceEventNotification&
72 setKind(FaceEventKind kind)
73 {
74 m_wire.reset();
75 m_kind = kind;
76 return *this;
77 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070078
79 uint64_t
80 getFaceId() const
81 {
82 return m_faceId;
83 }
84
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070085 FaceEventNotification&
86 setFaceId(uint64_t faceId)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070087 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070088 m_wire.reset();
89 m_faceId = faceId;
90 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070091 }
92
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070093 const std::string&
94 getRemoteUri() const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070095 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070096 return m_remoteUri;
97 }
98
99 FaceEventNotification&
100 setRemoteUri(const std::string& remoteUri)
101 {
102 m_wire.reset();
103 m_remoteUri = remoteUri;
104 return *this;
105 }
106
107 const std::string&
108 getLocalUri() const
109 {
110 return m_localUri;
111 }
112
113 FaceEventNotification&
114 setLocalUri(const std::string& localUri)
115 {
116 m_wire.reset();
117 m_localUri = localUri;
118 return *this;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700119 }
120
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700121 uint64_t
122 getFlags() const
123 {
124 return m_flags;
125 }
126
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700127 FaceEventNotification&
128 setFlags(uint64_t flags)
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700129 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700130 m_wire.reset();
131 m_flags = flags;
132 return *this;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700133 }
134
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700135public: // deprecated
136 /** \deprecated use default constructor and setters
137 */
138 FaceEventNotification(FaceEventKind kind,
139 uint64_t faceId,
140 const std::string& uri,
141 uint64_t flags);
142
143 /** \deprecated
144 */
145 FaceEventKind
146 getEventKind() const
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700147 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700148 return this->getKind();
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700149 }
150
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700151 /** \deprecated
152 */
153 const std::string&
154 getUri() const
155 {
156 return this->getRemoteUri();
157 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700158
159private:
160 FaceEventKind m_kind;
161 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700162 std::string m_remoteUri;
163 std::string m_localUri;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700164 uint64_t m_flags;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700165
166 mutable Block m_wire;
167};
168
169inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700170FaceEventNotification::FaceEventNotification()
171 : m_kind(static_cast<FaceEventKind>(0))
172 , m_faceId(0)
173 , m_flags(0)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700174{
175}
176
177inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700178FaceEventNotification::FaceEventNotification(FaceEventKind kind,
179 uint64_t faceId,
180 const std::string& uri,
181 uint64_t flags)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700182{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700183 (*this).setKind(kind)
184 .setFaceId(faceId)
185 .setRemoteUri(uri)
186 .setFlags(flags);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700187}
188
189template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700190inline size_t
191FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700192{
193 size_t totalLength = 0;
194
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700195 totalLength += prependNonNegativeIntegerBlock(encoder,
196 tlv::nfd::FaceFlags, m_flags);
197 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
198 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
199 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
200 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
201 totalLength += prependNonNegativeIntegerBlock(encoder,
202 tlv::nfd::FaceId, m_faceId);
203 totalLength += prependNonNegativeIntegerBlock(encoder,
204 tlv::nfd::FaceEventKind, m_kind);
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700205
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700206 totalLength += encoder.prependVarNumber(totalLength);
207 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700208 return totalLength;
209}
210
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600211inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700212FaceEventNotification::wireEncode() const
213{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700214 if (m_wire.hasWire())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700215 return m_wire;
216
217 EncodingEstimator estimator;
218 size_t estimatedSize = wireEncode(estimator);
219
220 EncodingBuffer buffer(estimatedSize, 0);
221 wireEncode(buffer);
222
223 m_wire = buffer.block();
224 return m_wire;
225}
226
227inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700228FaceEventNotification::wireDecode(const Block& block)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700229{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700230 if (block.type() != tlv::nfd::FaceEventNotification) {
231 throw Error("expecting FaceEventNotification block");
232 }
233 m_wire = block;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700234 m_wire.parse();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700235 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700236
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700237 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
238 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
239 ++val;
240 }
241 else {
242 throw Error("missing required FaceEventKind field");
243 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700244
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700245 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
246 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
247 ++val;
248 }
249 else {
250 throw Error("missing required FaceId field");
251 }
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700252
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700253 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
254 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
255 ++val;
256 }
257 else {
258 throw Error("missing required Uri field");
259 }
260
261 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
262 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
263 ++val;
264 }
265 else {
266 throw Error("missing required LocalUri field");
267 }
268
269 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
270 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
271 ++val;
272 }
273 else {
274 throw Error("missing required FaceFlags field");
275 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700276}
277
278inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700279operator<<(std::ostream& os, const FaceEventNotification& notification)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700280{
281 os << "FaceEventNotification(";
282
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700283 switch (notification.getKind())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700284 {
285 case FACE_EVENT_CREATED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700286 os << "Kind: created, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700287 break;
288 case FACE_EVENT_DESTROYED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700289 os << "Kind: destroyed, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700290 break;
291 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700292
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700293 os << "FaceID: " << notification.getFaceId() << ", "
294 << "RemoteUri: " << notification.getRemoteUri() << ", "
295 << "LocalUri: " << notification.getLocalUri() << ", "
296 << "Flags: " << notification.getFlags()
297 << ")";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700298 return os;
299}
300
301} // namespace nfd
302} // namespace ndn
303
304#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP