blob: b776c532dd0d3cb87797ce0078895e50d989b72d [file] [log] [blame]
Junxiao Shi7f012472017-12-07 20:40:47 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2017 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
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()
34 : m_nHits(0)
35 , m_nMisses(0)
36{
37}
38
39CsInfo::CsInfo(const Block& block)
40{
41 this->wireDecode(block);
42}
43
44template<encoding::Tag TAG>
45size_t
46CsInfo::wireEncode(EncodingImpl<TAG>& encoder) const
47{
48 size_t totalLength = 0;
49
50 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMisses, m_nMisses);
51 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NHits, m_nHits);
52
53 totalLength += encoder.prependVarNumber(totalLength);
54 totalLength += encoder.prependVarNumber(tlv::nfd::CsInfo);
55 return totalLength;
56}
57
58NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CsInfo);
59
60const Block&
61CsInfo::wireEncode() const
62{
63 if (m_wire.hasWire())
64 return m_wire;
65
66 EncodingEstimator estimator;
67 size_t estimatedSize = wireEncode(estimator);
68
69 EncodingBuffer buffer(estimatedSize, 0);
70 wireEncode(buffer);
71
72 m_wire = buffer.block();
73 return m_wire;
74}
75
76void
77CsInfo::wireDecode(const Block& block)
78{
79 if (block.type() != tlv::nfd::CsInfo) {
80 BOOST_THROW_EXCEPTION(Error("expecting CsInfo block, got " + to_string(block.type())));
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::NHits) {
87 m_nHits = readNonNegativeInteger(*val);
88 ++val;
89 }
90 else {
91 BOOST_THROW_EXCEPTION(Error("missing required NHits field"));
92 }
93
94 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMisses) {
95 m_nMisses = readNonNegativeInteger(*val);
96 ++val;
97 }
98 else {
99 BOOST_THROW_EXCEPTION(Error("missing required NMisses field"));
100 }
101}
102
103CsInfo&
104CsInfo::setNHits(uint64_t nHits)
105{
106 m_wire.reset();
107 m_nHits = nHits;
108 return *this;
109}
110
111CsInfo&
112CsInfo::setNMisses(uint64_t nMisses)
113{
114 m_wire.reset();
115 m_nMisses = nMisses;
116 return *this;
117}
118
119bool
120operator==(const CsInfo& a, const CsInfo& b)
121{
122 return a.getNHits() == b.getNHits() &&
123 a.getNMisses() == b.getNMisses();
124}
125
126std::ostream&
127operator<<(std::ostream& os, const CsInfo& status)
128{
129 os << "CS: "
130 << status.getNHits() << (status.getNHits() == 1 ? " hit" : " hits")
131 << ", "
132 << status.getNMisses() << (status.getNMisses() == 1 ? " miss" : " misses");
133 return os;
134}
135
136} // namespace nfd
137} // namespace ndn