blob: 8e311037f7241e4c0c4d5c94dc03dad0e8d09651 [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
22class FaceEventNotification
23{
24public:
25 class Error : public Tlv::Error
26 {
27 public:
28 Error(const std::string& what) : Tlv::Error(what) { }
29 };
30
31 FaceEventNotification(const FaceEventKind eventKind,
32 const uint64_t faceId,
33 const std::string& uri);
34
35 explicit
36 FaceEventNotification(const Block& block);
37
38 uint64_t
39 getFaceId() const
40 {
41 return m_faceId;
42 }
43
44 const std::string&
45 getUri() const
46 {
47 return m_uri;
48 }
49
50 FaceEventKind
51 getEventKind() const
52 {
53 return m_kind;
54 }
55
56 template<bool T>
57 size_t
58 wireEncode(EncodingImpl<T>& buffer) const;
59
60 const Block&
61 wireEncode() const;
62
63 void
64 wireDecode(const Block& wire);
65
66private:
67 FaceEventKind m_kind;
68 uint64_t m_faceId;
69 std::string m_uri;
70
71 mutable Block m_wire;
72};
73
74inline
75FaceEventNotification::FaceEventNotification(const FaceEventKind eventKind,
76 const uint64_t faceId,
77 const std::string& uri)
78 : m_kind(eventKind)
79 , m_faceId(faceId)
80 , m_uri(uri)
81{
82}
83
84inline
85FaceEventNotification::FaceEventNotification(const Block& block)
86{
87 wireDecode(block);
88}
89
90template<bool T>
91size_t
92FaceEventNotification::wireEncode(EncodingImpl<T>& buffer) const
93{
94 size_t totalLength = 0;
95
96 totalLength += prependByteArrayBlock(buffer,
97 tlv::nfd::Uri,
98 reinterpret_cast<const uint8_t*>(m_uri.c_str()),
99 m_uri.size());
100
101 totalLength += prependNonNegativeIntegerBlock(buffer,
102 tlv::nfd::FaceId,
103 m_faceId);
104
105 totalLength += prependNonNegativeIntegerBlock(buffer,
106 tlv::nfd::FaceEventKind,
107 static_cast<uint32_t>(m_kind));
108
109 totalLength += buffer.prependVarNumber(totalLength);
110 totalLength += buffer.prependVarNumber(tlv::nfd::FaceEventNotification);
111
112 return totalLength;
113}
114
Steve DiBenedetto5ce55ae2014-03-20 12:23:32 -0600115inline const Block&
Alexander Afanasyev44b438a2014-03-19 12:51:49 -0700116FaceEventNotification::wireEncode() const
117{
118 if (m_wire.hasWire ())
119 return m_wire;
120
121 EncodingEstimator estimator;
122 size_t estimatedSize = wireEncode(estimator);
123
124 EncodingBuffer buffer(estimatedSize, 0);
125 wireEncode(buffer);
126
127 m_wire = buffer.block();
128 return m_wire;
129}
130
131inline void
132FaceEventNotification::wireDecode (const Block &wire)
133{
134 m_wire = wire;
135
136 if (m_wire.type() != tlv::nfd::FaceEventNotification)
137 throw Error("Requested decoding of FaceEventNotification, but Block is of different type");
138
139 m_wire.parse();
140
141 // FaceKind
142 Block::element_const_iterator val = m_wire.elements_begin();
143 if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceEventKind)
144 throw Error("Missing required Uri block");
145 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
146
147 // FaceID
148 ++val;
149 if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceId)
150 throw Error("Missing required FaceId block");
151 m_faceId = readNonNegativeInteger(*val);
152
153 // URI
154 ++val;
155 if (val == m_wire.elements_end() || val->type() != tlv::nfd::Uri)
156 throw Error("Missing required Uri block");
157 m_uri = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
158}
159
160inline std::ostream&
161operator << (std::ostream& os, const FaceEventNotification& event)
162{
163 os << "FaceEventNotification(";
164
165 os << "Kind: ";
166 switch (event.getEventKind())
167 {
168 case FACE_EVENT_CREATED:
169 os << "created";
170 break;
171 case FACE_EVENT_DESTROYED:
172 os << "destroyed";
173 break;
174 }
175 os << ", ";
176
177 // FaceID
178 os << "FaceID: " << event.getFaceId() << ", ";
179
180 // URI
181 os << "Uri: " << event.getUri();
182
183 os << ")";
184 return os;
185}
186
187} // namespace nfd
188} // namespace ndn
189
190#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP