blob: 89531035d8fb0e79e0bfe3a8af9fed22ea162393 [file] [log] [blame]
Junxiao Shi7f012472017-12-07 20:40:47 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shia7ab06d2018-01-29 22:28:47 +00003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi7f012472017-12-07 20:40:47 +00004 *
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
22#include "cs-info.hpp"
23#include "encoding/block-helpers.hpp"
24#include "encoding/encoding-buffer.hpp"
25#include "encoding/tlv-nfd.hpp"
26#include "util/concepts.hpp"
27
28namespace ndn {
29namespace nfd {
30
31BOOST_CONCEPT_ASSERT((StatusDatasetItem<CsInfo>));
32
33CsInfo::CsInfo()
Junxiao Shia7ab06d2018-01-29 22:28:47 +000034 : m_capacity(0)
35 , m_nEntries(0)
36 , m_nHits(0)
Junxiao Shi7f012472017-12-07 20:40:47 +000037 , m_nMisses(0)
38{
39}
40
41CsInfo::CsInfo(const Block& block)
42{
43 this->wireDecode(block);
44}
45
46template<encoding::Tag TAG>
47size_t
48CsInfo::wireEncode(EncodingImpl<TAG>& encoder) const
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);
Junxiao Shia7ab06d2018-01-29 22:28:47 +000054 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);
Junxiao Shi7f012472017-12-07 20:40:47 +000057
58 totalLength += encoder.prependVarNumber(totalLength);
59 totalLength += encoder.prependVarNumber(tlv::nfd::CsInfo);
60 return totalLength;
61}
62
63NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CsInfo);
64
65const Block&
66CsInfo::wireEncode() const
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
81void
82CsInfo::wireDecode(const Block& block)
83{
84 if (block.type() != tlv::nfd::CsInfo) {
85 BOOST_THROW_EXCEPTION(Error("expecting CsInfo block, got " + to_string(block.type())));
86 }
87 m_wire = block;
88 m_wire.parse();
89 auto val = m_wire.elements_begin();
90
Junxiao Shia7ab06d2018-01-29 22:28:47 +000091 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Capacity) {
Junxiao Shibe86d982018-02-07 14:20:29 +000092 m_capacity = readNonNegativeInteger(*val);
Junxiao Shia7ab06d2018-01-29 22:28:47 +000093 ++val;
94 }
95 else {
96 BOOST_THROW_EXCEPTION(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 BOOST_THROW_EXCEPTION(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 BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field"));
113 }
114
Junxiao Shi7f012472017-12-07 20:40:47 +0000115 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NHits) {
116 m_nHits = readNonNegativeInteger(*val);
117 ++val;
118 }
119 else {
120 BOOST_THROW_EXCEPTION(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 BOOST_THROW_EXCEPTION(Error("missing required NMisses field"));
129 }
130}
131
132CsInfo&
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000133CsInfo::setCapacity(uint64_t capacity)
134{
135 m_wire.reset();
136 m_capacity = capacity;
137 return *this;
138}
139
140CsInfo&
141CsInfo::setEnableAdmit(bool enableAdmit)
142{
143 m_wire.reset();
144 m_flags[BIT_CS_ENABLE_ADMIT] = enableAdmit;
145 return *this;
146}
147
148CsInfo&
149CsInfo::setEnableServe(bool enableServe)
150{
151 m_wire.reset();
152 m_flags[BIT_CS_ENABLE_SERVE] = enableServe;
153 return *this;
154}
155
156CsInfo&
157CsInfo::setNEntries(uint64_t nEntries)
158{
159 m_wire.reset();
160 m_nEntries = nEntries;
161 return *this;
162}
163
164CsInfo&
Junxiao Shi7f012472017-12-07 20:40:47 +0000165CsInfo::setNHits(uint64_t nHits)
166{
167 m_wire.reset();
168 m_nHits = nHits;
169 return *this;
170}
171
172CsInfo&
173CsInfo::setNMisses(uint64_t nMisses)
174{
175 m_wire.reset();
176 m_nMisses = nMisses;
177 return *this;
178}
179
180bool
181operator==(const CsInfo& a, const CsInfo& b)
182{
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000183 return a.wireEncode() == b.wireEncode();
Junxiao Shi7f012472017-12-07 20:40:47 +0000184}
185
186std::ostream&
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000187operator<<(std::ostream& os, const CsInfo& csi)
Junxiao Shi7f012472017-12-07 20:40:47 +0000188{
189 os << "CS: "
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000190 << csi.getNEntries() << " entries, " << csi.getCapacity() << " max, "
191 << (csi.getEnableAdmit() ? "admit enabled, " : "admit disabled, ")
192 << (csi.getEnableServe() ? "serve enabled, " : "serve disabled, ")
193 << csi.getNHits() << (csi.getNHits() == 1 ? " hit, " : " hits, ")
194 << csi.getNMisses() << (csi.getNMisses() == 1 ? " miss" : " misses");
Junxiao Shi7f012472017-12-07 20:40:47 +0000195 return os;
196}
197
198} // namespace nfd
199} // namespace ndn