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