Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 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 | |
| 28 | namespace ndn { |
| 29 | namespace nfd { |
| 30 | |
| 31 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<CsInfo>)); |
| 32 | |
| 33 | CsInfo::CsInfo() |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 34 | : m_capacity(0) |
| 35 | , m_nEntries(0) |
| 36 | , m_nHits(0) |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 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 |
| 48 | CsInfo::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 Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 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); |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 57 | |
| 58 | totalLength += encoder.prependVarNumber(totalLength); |
| 59 | totalLength += encoder.prependVarNumber(tlv::nfd::CsInfo); |
| 60 | return totalLength; |
| 61 | } |
| 62 | |
| 63 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CsInfo); |
| 64 | |
| 65 | const Block& |
| 66 | CsInfo::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 | |
| 81 | void |
| 82 | CsInfo::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 Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 91 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Capacity) { |
Junxiao Shi | be86d98 | 2018-02-07 14:20:29 +0000 | [diff] [blame] | 92 | m_capacity = readNonNegativeInteger(*val); |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 93 | ++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 Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 115 | 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 | |
| 132 | CsInfo& |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 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& |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 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 | { |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 183 | return a.wireEncode() == b.wireEncode(); |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | std::ostream& |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 187 | operator<<(std::ostream& os, const CsInfo& csi) |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 188 | { |
| 189 | os << "CS: " |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 190 | << 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 Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 195 | return os; |
| 196 | } |
| 197 | |
| 198 | } // namespace nfd |
| 199 | } // namespace ndn |