NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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
ndn::nfd::CsInfo::wireDecode
void wireDecode(const Block &wire)
Definition: cs-info.cpp:82
ndn::nfd::CsInfo::setNMisses
CsInfo & setNMisses(uint64_t nMisses)
Definition: cs-info.cpp:173
ndn::tlv::nfd::NCsEntries
@ NCsEntries
Definition: tlv-nfd.hpp:60
ndn::nfd::operator<<
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
Definition: nfd-constants.cpp:36
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::nfd::CsInfo::setNHits
CsInfo & setNHits(uint64_t nHits)
Definition: cs-info.cpp:165
concepts.hpp
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::nfd::CsInfo
represents the CS Information dataset
Definition: cs-info.hpp:38
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::encoding::readNonNegativeInteger
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Definition: block-helpers.cpp:64
ndn::nfd::CsInfo::setEnableServe
CsInfo & setEnableServe(bool enableServe)
Definition: cs-info.cpp:149
ndn::nfd::CsInfo::setNEntries
CsInfo & setNEntries(uint64_t nEntries)
Definition: cs-info.cpp:157
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::nfd::CsInfo::Error
Definition: cs-info.hpp:41
ndn::tlv::nfd::CsInfo
@ CsInfo
Definition: tlv-nfd.hpp:90
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::nfd::CsInfo::wireEncode
const Block & wireEncode() const
Definition: cs-info.cpp:66
ndn::nfd::CsInfo::setEnableAdmit
CsInfo & setEnableAdmit(bool enableAdmit)
Definition: cs-info.cpp:141
ndn::nfd::BIT_CS_ENABLE_SERVE
@ BIT_CS_ENABLE_SERVE
enables the CS to satisfy Interests using cached Data
Definition: nfd-constants.hpp:94
ndn::tlv::nfd::NHits
@ NHits
Definition: tlv-nfd.hpp:91
cs-info.hpp
ndn::nfd::BIT_CS_ENABLE_ADMIT
@ BIT_CS_ENABLE_ADMIT
enables the CS to admit new Data
Definition: nfd-constants.hpp:93
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::encoding::prependNonNegativeIntegerBlock
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Definition: block-helpers.cpp:35
ndn::tlv::nfd::NMisses
@ NMisses
Definition: tlv-nfd.hpp:92
ndn::nfd::CsInfo::getCapacity
uint64_t getCapacity() const
get CS capacity (in number of packets)
Definition: cs-info.hpp:64
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
tlv-nfd.hpp
block-helpers.hpp
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_end
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
ndn::nfd::CsInfo::getEnableAdmit
bool getEnableAdmit() const
get CS_ENABLE_ADMIT flag
Definition: cs-info.hpp:75
ndn::nfd::CsInfo::getNEntries
uint64_t getNEntries() const
get number of stored CS entries
Definition: cs-info.hpp:97
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::nfd::CsInfo::CsInfo
CsInfo()
Definition: cs-info.cpp:33
ndn::nfd::CsInfo::getNHits
uint64_t getNHits() const
get number of CS lookup hits since NFD starts
Definition: cs-info.hpp:108
ndn::nfd::operator==
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
Definition: channel-status.cpp:97
ndn::nfd::CsInfo::getEnableServe
bool getEnableServe() const
get CS_ENABLE_SERVE flag
Definition: cs-info.hpp:86
ndn::tlv::nfd::Capacity
@ Capacity
Definition: tlv-nfd.hpp:40
ndn::nfd::CsInfo::setCapacity
CsInfo & setCapacity(uint64_t capacity)
Definition: cs-info.cpp:133
ndn::StatusDatasetItem
concept check for an item in a Status Dataset
Definition: concepts.hpp:116
ndn::nfd::CsInfo::getNMisses
uint64_t getNMisses() const
get number of CS lookup misses since NFD starts
Definition: cs-info.hpp:119
ndn::nfd::NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus)
encoding-buffer.hpp
ndn::tlv::nfd::Flags
@ Flags
Definition: tlv-nfd.hpp:42
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38