face-event-notification.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
28 
29 namespace ndn {
30 namespace nfd {
31 
32 BOOST_CONCEPT_ASSERT((NotificationStreamItem<FaceEventNotification>));
33 
35  : m_kind(FACE_EVENT_NONE)
36 {
37 }
38 
40 {
41  this->wireDecode(block);
42 }
43 
44 template<encoding::Tag TAG>
45 size_t
47 {
48  size_t totalLength = 0;
49 
50  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
54  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
55  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
57  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceEventKind, m_kind);
58 
59  totalLength += encoder.prependVarNumber(totalLength);
60  totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
61  return totalLength;
62 }
63 
65 
66 const Block&
68 {
69  if (m_wire.hasWire())
70  return m_wire;
71 
72  EncodingEstimator estimator;
73  size_t estimatedSize = wireEncode(estimator);
74 
75  EncodingBuffer buffer(estimatedSize, 0);
76  wireEncode(buffer);
77 
78  m_wire = buffer.block();
79  return m_wire;
80 }
81 
82 void
84 {
85  if (block.type() != tlv::nfd::FaceEventNotification) {
86  NDN_THROW(Error("FaceEventNotification", block.type()));
87  }
88 
89  m_wire = block;
90  m_wire.parse();
91  auto val = m_wire.elements_begin();
92 
93  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
94  m_kind = readNonNegativeIntegerAs<FaceEventKind>(*val);
95  ++val;
96  }
97  else {
98  NDN_THROW(Error("missing required FaceEventKind field"));
99  }
100 
101  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
103  ++val;
104  }
105  else {
106  NDN_THROW(Error("missing required FaceId field"));
107  }
108 
109  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
110  m_remoteUri = readString(*val);
111  ++val;
112  }
113  else {
114  NDN_THROW(Error("missing required Uri field"));
115  }
116 
117  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
118  m_localUri = readString(*val);
119  ++val;
120  }
121  else {
122  NDN_THROW(Error("missing required LocalUri field"));
123  }
124 
125  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
126  m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
127  ++val;
128  }
129  else {
130  NDN_THROW(Error("missing required FaceScope field"));
131  }
132 
133  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
134  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
135  ++val;
136  }
137  else {
138  NDN_THROW(Error("missing required FacePersistency field"));
139  }
140 
141  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
142  m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
143  ++val;
144  }
145  else {
146  NDN_THROW(Error("missing required LinkType field"));
147  }
148 
149  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
151  ++val;
152  }
153  else {
154  NDN_THROW(Error("missing required Flags field"));
155  }
156 }
157 
160 {
161  m_wire.reset();
162  m_kind = kind;
163  return *this;
164 }
165 
166 bool
168 {
169  return a.getFaceId() == b.getFaceId() &&
170  a.getRemoteUri() == b.getRemoteUri() &&
171  a.getLocalUri() == b.getLocalUri() &&
172  a.getFaceScope() == b.getFaceScope() &&
174  a.getLinkType() == b.getLinkType() &&
175  a.getFlags() == b.getFlags() &&
176  a.getKind() == b.getKind();
177 }
178 
179 std::ostream&
180 operator<<(std::ostream& os, const FaceEventNotification& notification)
181 {
182  os << "FaceEvent(Kind: " << notification.getKind() << ",\n"
183  << " FaceId: " << notification.getFaceId() << ",\n"
184  << " RemoteUri: " << notification.getRemoteUri() << ",\n"
185  << " LocalUri: " << notification.getLocalUri() << ",\n"
186  << " FaceScope: " << notification.getFaceScope() << ",\n"
187  << " FacePersistency: " << notification.getFacePersistency() << ",\n"
188  << " LinkType: " << notification.getLinkType() << ",\n"
189  << " Flags: " << AsHex{notification.getFlags()} << "\n";
190 
191  return os << " )";
192 }
193 
194 } // namespace nfd
195 } // namespace ndn
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:418
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:410
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:265
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:341
represents a Face status change notification
const Block & wireEncode() const
encode FaceEventNotification
FaceEventNotification & setKind(FaceEventKind kind)
void wireDecode(const Block &wire)
decode FaceEventNotification
uint64_t getFlags() const
const std::string & getLocalUri() const
Definition: face-traits.hpp:75
const std::string & getRemoteUri() const
Definition: face-traits.hpp:61
FacePersistency getFacePersistency() const
uint64_t getFaceId() const
Definition: face-traits.hpp:47
FaceScope getFaceScope() const
Definition: face-traits.hpp:89
LinkType getLinkType() const
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:61
EncodingImpl< EstimatorTag > EncodingEstimator
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
EncodingImpl< EncoderTag > EncodingBuffer
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
@ FaceEventNotification
Definition: tlv-nfd.hpp:74
Definition: data.cpp:25