All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nfd-face-event-notification.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
8 #define NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
9 
10 // 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 
13 #include "../encoding/tlv-nfd.hpp"
14 #include "../encoding/encoding-buffer.hpp"
15 
16 namespace ndn {
17 namespace nfd {
18 
22 };
23 
27 class FaceEventNotification : public FaceFlagsTraits<FaceEventNotification>
28 {
29 public:
30  class Error : public Tlv::Error
31  {
32  public:
33  explicit
34  Error(const std::string& what)
35  : Tlv::Error(what)
36  {
37  }
38  };
39 
41 
42  explicit
44  {
45  this->wireDecode(block);
46  }
47 
50  template<bool T>
51  size_t
52  wireEncode(EncodingImpl<T>& encoder) const;
53 
56  const Block&
57  wireEncode() const;
58 
61  void
62  wireDecode(const Block& wire);
63 
64 public: // getters & setters
66  getKind() const
67  {
68  return m_kind;
69  }
70 
73  {
74  m_wire.reset();
75  m_kind = kind;
76  return *this;
77  }
78 
79  uint64_t
80  getFaceId() const
81  {
82  return m_faceId;
83  }
84 
86  setFaceId(uint64_t faceId)
87  {
88  m_wire.reset();
89  m_faceId = faceId;
90  return *this;
91  }
92 
93  const std::string&
94  getRemoteUri() const
95  {
96  return m_remoteUri;
97  }
98 
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 
114  setLocalUri(const std::string& localUri)
115  {
116  m_wire.reset();
117  m_localUri = localUri;
118  return *this;
119  }
120 
121  uint64_t
122  getFlags() const
123  {
124  return m_flags;
125  }
126 
128  setFlags(uint64_t flags)
129  {
130  m_wire.reset();
131  m_flags = flags;
132  return *this;
133  }
134 
135 private:
136  FaceEventKind m_kind;
137  uint64_t m_faceId;
138  std::string m_remoteUri;
139  std::string m_localUri;
140  uint64_t m_flags;
141 
142  mutable Block m_wire;
143 };
144 
145 inline
147  : m_kind(static_cast<FaceEventKind>(0))
148  , m_faceId(0)
149  , m_flags(0)
150 {
151 }
152 
153 template<bool T>
154 inline size_t
156 {
157  size_t totalLength = 0;
158 
159  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);
169 
170  totalLength += encoder.prependVarNumber(totalLength);
171  totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
172  return totalLength;
173 }
174 
175 inline const Block&
177 {
178  if (m_wire.hasWire())
179  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 
191 inline void
193 {
194  if (block.type() != tlv::nfd::FaceEventNotification) {
195  throw Error("expecting FaceEventNotification block");
196  }
197  m_wire = block;
198  m_wire.parse();
200 
201  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  }
208 
209  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  }
216 
217  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  }
240 }
241 
242 inline std::ostream&
243 operator<<(std::ostream& os, const FaceEventNotification& notification)
244 {
245  os << "FaceEventNotification(";
246 
247  switch (notification.getKind())
248  {
249  case FACE_EVENT_CREATED:
250  os << "Kind: created, ";
251  break;
253  os << "Kind: destroyed, ";
254  break;
255  }
256 
257  os << "FaceID: " << notification.getFaceId() << ", "
258  << "RemoteUri: " << notification.getRemoteUri() << ", "
259  << "LocalUri: " << notification.getLocalUri() << ", "
260  << "Flags: " << notification.getFlags()
261  << ")";
262  return os;
263 }
264 
265 } // namespace nfd
266 } // namespace ndn
267 
268 #endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP
const std::string & getRemoteUri() const
std::ostream & operator<<(std::ostream &os, const ControlParameters &parameters)
FaceEventNotification & setLocalUri(const std::string &localUri)
FaceEventNotification & setFlags(uint64_t flags)
implements getters to each face flag
represents a Face status change notification
Class representing wire element of the NDN packet.
Definition: block.hpp:26
FaceEventNotification & setRemoteUri(const std::string &remoteUri)
uint64_t readNonNegativeInteger(const Block &block)
element_const_iterator elements_end() const
Definition: block.hpp:476
element_const_iterator elements_begin() const
Definition: block.hpp:470
FaceEventNotification & setKind(FaceEventKind kind)
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
FaceEventNotification & setFaceId(uint64_t faceId)
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
uint32_t type() const
Definition: block.hpp:320
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
size_t prependNonNegativeIntegerBlock(EncodingImpl< P > &encoder, uint32_t type, uint64_t number)
helper methods
void wireDecode(const Block &wire)
decode FaceEventNotification
const Block & wireEncode() const
encode FaceEventNotification
size_t prependByteArrayBlock(EncodingImpl< P > &encoder, uint32_t type, const uint8_t *array, size_t arraySize)
Class representing wire element of the NDN packet.