blob: 3d06ea733508bfb253d7545e39d6546f549f0b55 [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
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700135private:
136 FaceEventKind m_kind;
137 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700138 std::string m_remoteUri;
139 std::string m_localUri;
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700140 uint64_t m_flags;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700141
142 mutable Block m_wire;
143};
144
145inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700146FaceEventNotification::FaceEventNotification()
147 : m_kind(static_cast<FaceEventKind>(0))
148 , m_faceId(0)
149 , m_flags(0)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700150{
151}
152
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700153template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700154inline size_t
155FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700156{
157 size_t totalLength = 0;
158
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700159 totalLength += prependNonNegativeIntegerBlock(encoder,
160 tlv::nfd::FaceFlags, m_flags);
161 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
162 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
163 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
164 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
165 totalLength += prependNonNegativeIntegerBlock(encoder,
166 tlv::nfd::FaceId, m_faceId);
167 totalLength += prependNonNegativeIntegerBlock(encoder,
168 tlv::nfd::FaceEventKind, m_kind);
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700169
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700170 totalLength += encoder.prependVarNumber(totalLength);
171 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700172 return totalLength;
173}
174
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600175inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700176FaceEventNotification::wireEncode() const
177{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700178 if (m_wire.hasWire())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700179 return m_wire;
180
181 EncodingEstimator estimator;
182 size_t estimatedSize = wireEncode(estimator);
183
184 EncodingBuffer buffer(estimatedSize, 0);
185 wireEncode(buffer);
186
187 m_wire = buffer.block();
188 return m_wire;
189}
190
191inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700192FaceEventNotification::wireDecode(const Block& block)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700193{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700194 if (block.type() != tlv::nfd::FaceEventNotification) {
195 throw Error("expecting FaceEventNotification block");
196 }
197 m_wire = block;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700198 m_wire.parse();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700199 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700200
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700201 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
202 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
203 ++val;
204 }
205 else {
206 throw Error("missing required FaceEventKind field");
207 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700208
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700209 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
210 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
211 ++val;
212 }
213 else {
214 throw Error("missing required FaceId field");
215 }
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700216
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700217 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
218 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
219 ++val;
220 }
221 else {
222 throw Error("missing required Uri field");
223 }
224
225 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
226 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
227 ++val;
228 }
229 else {
230 throw Error("missing required LocalUri field");
231 }
232
233 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
234 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
235 ++val;
236 }
237 else {
238 throw Error("missing required FaceFlags field");
239 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700240}
241
242inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700243operator<<(std::ostream& os, const FaceEventNotification& notification)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700244{
245 os << "FaceEventNotification(";
246
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700247 switch (notification.getKind())
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700248 {
249 case FACE_EVENT_CREATED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700250 os << "Kind: created, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700251 break;
252 case FACE_EVENT_DESTROYED:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700253 os << "Kind: destroyed, ";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700254 break;
255 }
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700256
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700257 os << "FaceID: " << notification.getFaceId() << ", "
258 << "RemoteUri: " << notification.getRemoteUri() << ", "
259 << "LocalUri: " << notification.getLocalUri() << ", "
260 << "Flags: " << notification.getFlags()
261 << ")";
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700262 return os;
263}
264
265} // namespace nfd
266} // namespace ndn
267
268#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP