cs-info.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 
26 
27 namespace ndn::nfd {
28 
29 CsInfo::CsInfo() = default;
30 
31 CsInfo::CsInfo(const Block& block)
32 {
33  this->wireDecode(block);
34 }
35 
36 template<encoding::Tag TAG>
37 size_t
39 {
40  size_t totalLength = 0;
41 
42  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMisses, m_nMisses);
43  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NHits, m_nHits);
44  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries, m_nEntries);
45  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags.to_ullong());
46  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Capacity, m_capacity);
47 
48  totalLength += encoder.prependVarNumber(totalLength);
49  totalLength += encoder.prependVarNumber(tlv::nfd::CsInfo);
50  return totalLength;
51 }
52 
54 
55 const Block&
57 {
58  if (m_wire.hasWire())
59  return m_wire;
60 
61  EncodingEstimator estimator;
62  size_t estimatedSize = wireEncode(estimator);
63 
64  EncodingBuffer buffer(estimatedSize, 0);
65  wireEncode(buffer);
66 
67  m_wire = buffer.block();
68  return m_wire;
69 }
70 
71 void
73 {
74  if (block.type() != tlv::nfd::CsInfo) {
75  NDN_THROW(Error("CsInfo", block.type()));
76  }
77  m_wire = block;
78  m_wire.parse();
79  auto val = m_wire.elements_begin();
80 
81  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Capacity) {
82  m_capacity = readNonNegativeInteger(*val);
83  ++val;
84  }
85  else {
86  NDN_THROW(Error("missing required Capacity field"));
87  }
88 
89  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
90  m_flags = FlagsBitSet(static_cast<unsigned long long>(readNonNegativeInteger(*val)));
91  ++val;
92  }
93  else {
94  NDN_THROW(Error("missing required Flags field"));
95  }
96 
97  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
98  m_nEntries = readNonNegativeInteger(*val);
99  ++val;
100  }
101  else {
102  NDN_THROW(Error("missing required NCsEntries field"));
103  }
104 
105  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NHits) {
106  m_nHits = readNonNegativeInteger(*val);
107  ++val;
108  }
109  else {
110  NDN_THROW(Error("missing required NHits field"));
111  }
112 
113  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMisses) {
114  m_nMisses = readNonNegativeInteger(*val);
115  ++val;
116  }
117  else {
118  NDN_THROW(Error("missing required NMisses field"));
119  }
120 }
121 
122 CsInfo&
123 CsInfo::setCapacity(uint64_t capacity)
124 {
125  m_wire.reset();
126  m_capacity = capacity;
127  return *this;
128 }
129 
130 CsInfo&
131 CsInfo::setEnableAdmit(bool enableAdmit)
132 {
133  m_wire.reset();
134  m_flags[BIT_CS_ENABLE_ADMIT] = enableAdmit;
135  return *this;
136 }
137 
138 CsInfo&
139 CsInfo::setEnableServe(bool enableServe)
140 {
141  m_wire.reset();
142  m_flags[BIT_CS_ENABLE_SERVE] = enableServe;
143  return *this;
144 }
145 
146 CsInfo&
147 CsInfo::setNEntries(uint64_t nEntries)
148 {
149  m_wire.reset();
150  m_nEntries = nEntries;
151  return *this;
152 }
153 
154 CsInfo&
155 CsInfo::setNHits(uint64_t nHits)
156 {
157  m_wire.reset();
158  m_nHits = nHits;
159  return *this;
160 }
161 
162 CsInfo&
163 CsInfo::setNMisses(uint64_t nMisses)
164 {
165  m_wire.reset();
166  m_nMisses = nMisses;
167  return *this;
168 }
169 
170 bool
171 operator==(const CsInfo& a, const CsInfo& b)
172 {
173  return a.getCapacity() == b.getCapacity() &&
174  a.getEnableAdmit() == b.getEnableAdmit() &&
175  a.getEnableServe() == b.getEnableServe() &&
176  a.getNEntries() == b.getNEntries() &&
177  a.getNHits() == b.getNHits() &&
178  a.getNMisses() == b.getNMisses();
179 }
180 
181 std::ostream&
182 operator<<(std::ostream& os, const CsInfo& csi)
183 {
184  return os << "CsInfo: "
185  << csi.getNEntries() << " entries, " << csi.getCapacity() << " max, "
186  << (csi.getEnableAdmit() ? "admit enabled, " : "admit disabled, ")
187  << (csi.getEnableServe() ? "serve enabled, " : "serve disabled, ")
188  << csi.getNHits() << (csi.getNHits() == 1 ? " hit, " : " hits, ")
189  << csi.getNMisses() << (csi.getNMisses() == 1 ? " miss" : " misses");
190 }
191 
192 } // namespace ndn::nfd
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 the CS Information dataset.
Definition: cs-info.hpp:37
CsInfo & setEnableServe(bool enableServe)
Definition: cs-info.cpp:139
CsInfo & setNEntries(uint64_t nEntries)
Definition: cs-info.cpp:147
CsInfo & setEnableAdmit(bool enableAdmit)
Definition: cs-info.cpp:131
uint64_t getCapacity() const
Get CS capacity (in number of packets).
Definition: cs-info.hpp:63
CsInfo & setNMisses(uint64_t nMisses)
Definition: cs-info.cpp:163
void wireDecode(const Block &wire)
Definition: cs-info.cpp:72
const Block & wireEncode() const
Definition: cs-info.cpp:56
bool getEnableAdmit() const
Get CS_ENABLE_ADMIT flag.
Definition: cs-info.hpp:74
uint64_t getNEntries() const
Get number of stored CS entries.
Definition: cs-info.hpp:96
CsInfo & setCapacity(uint64_t capacity)
Definition: cs-info.cpp:123
uint64_t getNHits() const
Get number of CS lookup hits since NFD started.
Definition: cs-info.hpp:107
CsInfo & setNHits(uint64_t nHits)
Definition: cs-info.cpp:155
bool getEnableServe() const
Get CS_ENABLE_SERVE flag.
Definition: cs-info.hpp:85
uint64_t getNMisses() const
Get number of CS lookup misses since NFD started.
Definition: cs-info.hpp:118
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:56
@ BIT_CS_ENABLE_ADMIT
enables the CS to admit new Data
@ BIT_CS_ENABLE_SERVE
enables the CS to satisfy Interests using cached Data
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.
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)