blob: 494475e9a46b6e6a4e8f7d2b13c33f407c6b8445 [file] [log] [blame]
Alexander Afanasyev44b438a2014-03-19 12:51:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * 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
10#include "../encoding/encoding-buffer.hpp"
11#include "../encoding/tlv-nfd.hpp"
12
13namespace ndn {
14namespace nfd {
15
16enum FaceEventKind
17 {
18 FACE_EVENT_CREATED = 1,
19 FACE_EVENT_DESTROYED = 2
20 };
21
Alexander Afanasyev2c753312014-03-20 17:31:01 -070022enum FaceFlags
23 {
24 FACE_IS_LOCAL = 1,
25 FACE_IS_ON_DEMAND = 2
26 // FACE_? = 4
27 // FACE_? = 8
28 };
29
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070030class FaceEventNotification
31{
32public:
33 class Error : public Tlv::Error
34 {
35 public:
36 Error(const std::string& what) : Tlv::Error(what) { }
37 };
38
Alexander Afanasyev2c753312014-03-20 17:31:01 -070039 FaceEventNotification(FaceEventKind eventKind,
40 uint64_t faceId,
41 const std::string& uri,
42 uint64_t flags);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070043
44 explicit
45 FaceEventNotification(const Block& block);
46
47 uint64_t
48 getFaceId() const
49 {
50 return m_faceId;
51 }
52
53 const std::string&
54 getUri() const
55 {
56 return m_uri;
57 }
58
59 FaceEventKind
60 getEventKind() const
61 {
62 return m_kind;
63 }
64
Alexander Afanasyev2c753312014-03-20 17:31:01 -070065 uint64_t
66 getFlags() const
67 {
68 return m_flags;
69 }
70
71 bool
72 isLocal() const
73 {
74 return m_flags & FACE_IS_LOCAL;
75 }
76
77 bool
78 isOnDemand() const
79 {
80 return m_flags & FACE_IS_ON_DEMAND;
81 }
82
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070083 template<bool T>
84 size_t
85 wireEncode(EncodingImpl<T>& buffer) const;
86
87 const Block&
88 wireEncode() const;
89
90 void
91 wireDecode(const Block& wire);
92
93private:
94 FaceEventKind m_kind;
95 uint64_t m_faceId;
96 std::string m_uri;
Alexander Afanasyev2c753312014-03-20 17:31:01 -070097 uint64_t m_flags;
Alexander Afanasyev44b438a2014-03-19 12:51:49 -070098
99 mutable Block m_wire;
100};
101
102inline
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700103FaceEventNotification::FaceEventNotification(FaceEventKind eventKind,
104 uint64_t faceId,
105 const std::string& uri,
106 uint64_t flags)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700107 : m_kind(eventKind)
108 , m_faceId(faceId)
109 , m_uri(uri)
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700110 , m_flags(flags)
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700111{
112}
113
114inline
115FaceEventNotification::FaceEventNotification(const Block& block)
116{
117 wireDecode(block);
118}
119
120template<bool T>
121size_t
122FaceEventNotification::wireEncode(EncodingImpl<T>& buffer) const
123{
124 size_t totalLength = 0;
125
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700126 totalLength += prependNonNegativeIntegerBlock(buffer,
127 tlv::nfd::FaceFlags,
128 m_flags);
129
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700130 totalLength += prependByteArrayBlock(buffer,
131 tlv::nfd::Uri,
132 reinterpret_cast<const uint8_t*>(m_uri.c_str()),
133 m_uri.size());
134
135 totalLength += prependNonNegativeIntegerBlock(buffer,
136 tlv::nfd::FaceId,
137 m_faceId);
138
139 totalLength += prependNonNegativeIntegerBlock(buffer,
140 tlv::nfd::FaceEventKind,
141 static_cast<uint32_t>(m_kind));
142
143 totalLength += buffer.prependVarNumber(totalLength);
144 totalLength += buffer.prependVarNumber(tlv::nfd::FaceEventNotification);
145
146 return totalLength;
147}
148
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600149inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700150FaceEventNotification::wireEncode() const
151{
152 if (m_wire.hasWire ())
153 return m_wire;
154
155 EncodingEstimator estimator;
156 size_t estimatedSize = wireEncode(estimator);
157
158 EncodingBuffer buffer(estimatedSize, 0);
159 wireEncode(buffer);
160
161 m_wire = buffer.block();
162 return m_wire;
163}
164
165inline void
166FaceEventNotification::wireDecode (const Block &wire)
167{
168 m_wire = wire;
169
170 if (m_wire.type() != tlv::nfd::FaceEventNotification)
171 throw Error("Requested decoding of FaceEventNotification, but Block is of different type");
172
173 m_wire.parse();
174
175 // FaceKind
176 Block::element_const_iterator val = m_wire.elements_begin();
177 if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceEventKind)
178 throw Error("Missing required Uri block");
179 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
180
181 // FaceID
182 ++val;
183 if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceId)
184 throw Error("Missing required FaceId block");
185 m_faceId = readNonNegativeInteger(*val);
186
187 // URI
188 ++val;
189 if (val == m_wire.elements_end() || val->type() != tlv::nfd::Uri)
190 throw Error("Missing required Uri block");
191 m_uri = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700192
193 // FaceFlags
194 ++val;
195 if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceFlags)
196 throw Error("Missing required FaceFlags block");
197 m_flags = readNonNegativeInteger(*val);
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700198}
199
200inline std::ostream&
201operator << (std::ostream& os, const FaceEventNotification& event)
202{
203 os << "FaceEventNotification(";
204
205 os << "Kind: ";
206 switch (event.getEventKind())
207 {
208 case FACE_EVENT_CREATED:
209 os << "created";
210 break;
211 case FACE_EVENT_DESTROYED:
212 os << "destroyed";
213 break;
214 }
215 os << ", ";
216
217 // FaceID
218 os << "FaceID: " << event.getFaceId() << ", ";
219
220 // URI
Alexander Afanasyev2c753312014-03-20 17:31:01 -0700221 os << "Uri: " << event.getUri() << ", ";
222
223 // Flags
224 os << "Flags: " << event.getFlags();
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700225
226 os << ")";
227 return os;
228}
229
230} // namespace nfd
231} // namespace ndn
232
233#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP