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-2023 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 
27 
28 namespace ndn::nfd {
29 
31 
33 {
34  this->wireDecode(block);
35 }
36 
37 template<encoding::Tag TAG>
38 size_t
40 {
41  size_t totalLength = 0;
42 
43  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
47  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
48  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
50  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceEventKind, m_kind);
51 
52  totalLength += encoder.prependVarNumber(totalLength);
53  totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
54  return totalLength;
55 }
56 
58 
59 const Block&
61 {
62  if (m_wire.hasWire())
63  return m_wire;
64 
65  EncodingEstimator estimator;
66  size_t estimatedSize = wireEncode(estimator);
67 
68  EncodingBuffer buffer(estimatedSize, 0);
69  wireEncode(buffer);
70 
71  m_wire = buffer.block();
72  return m_wire;
73 }
74 
75 void
77 {
78  if (block.type() != tlv::nfd::FaceEventNotification) {
79  NDN_THROW(Error("FaceEventNotification", block.type()));
80  }
81 
82  m_wire = block;
83  m_wire.parse();
84  auto val = m_wire.elements_begin();
85 
86  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
87  m_kind = readNonNegativeIntegerAs<FaceEventKind>(*val);
88  ++val;
89  }
90  else {
91  NDN_THROW(Error("missing required FaceEventKind field"));
92  }
93 
94  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
96  ++val;
97  }
98  else {
99  NDN_THROW(Error("missing required FaceId field"));
100  }
101 
102  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
103  m_remoteUri = readString(*val);
104  ++val;
105  }
106  else {
107  NDN_THROW(Error("missing required Uri field"));
108  }
109 
110  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
111  m_localUri = readString(*val);
112  ++val;
113  }
114  else {
115  NDN_THROW(Error("missing required LocalUri field"));
116  }
117 
118  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
119  m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
120  ++val;
121  }
122  else {
123  NDN_THROW(Error("missing required FaceScope field"));
124  }
125 
126  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
127  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
128  ++val;
129  }
130  else {
131  NDN_THROW(Error("missing required FacePersistency field"));
132  }
133 
134  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
135  m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
136  ++val;
137  }
138  else {
139  NDN_THROW(Error("missing required LinkType field"));
140  }
141 
142  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
144  ++val;
145  }
146  else {
147  NDN_THROW(Error("missing required Flags field"));
148  }
149 }
150 
153 {
154  m_wire.reset();
155  m_kind = kind;
156  return *this;
157 }
158 
159 bool
161 {
162  return a.getFaceId() == b.getFaceId() &&
163  a.getRemoteUri() == b.getRemoteUri() &&
164  a.getLocalUri() == b.getLocalUri() &&
165  a.getFaceScope() == b.getFaceScope() &&
167  a.getLinkType() == b.getLinkType() &&
168  a.getFlags() == b.getFlags() &&
169  a.getKind() == b.getKind();
170 }
171 
172 std::ostream&
173 operator<<(std::ostream& os, const FaceEventNotification& notification)
174 {
175  os << "FaceEvent(Kind: " << notification.getKind() << ",\n"
176  << " FaceId: " << notification.getFaceId() << ",\n"
177  << " RemoteUri: " << notification.getRemoteUri() << ",\n"
178  << " LocalUri: " << notification.getLocalUri() << ",\n"
179  << " FaceScope: " << notification.getFaceScope() << ",\n"
180  << " FacePersistency: " << notification.getFacePersistency() << ",\n"
181  << " LinkType: " << notification.getLinkType() << ",\n"
182  << " Flags: " << AsHex{notification.getFlags()} << "\n";
183 
184  return os << " )";
185 }
186 
187 } // namespace ndn::nfd
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
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition: block.hpp:433
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition: block.hpp:442
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:205
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:275
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:293
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:326
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:74
const std::string & getRemoteUri() const
Definition: face-traits.hpp:60
FacePersistency getFacePersistency() const
uint64_t getFaceId() const
Definition: face-traits.hpp:46
FaceScope getFaceScope() const
Definition: face-traits.hpp:88
LinkType getLinkType() const
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:56
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, std::string_view value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read the TLV-VALUE of a TLV element as a string.
EncodingImpl< EncoderTag > EncodingBuffer
Contains classes and functions related to the NFD Management protocol.
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
@ FaceEventNotification
Definition: tlv-nfd.hpp:74