ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
cache-policy.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2025 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 * @author Eric Newberry <[email protected]>
22 */
23
26
27namespace ndn::lp {
28
29std::ostream&
30operator<<(std::ostream& os, CachePolicyType policy)
31{
32 switch (policy) {
34 return os << "NoCache";
35 default:
36 return os << "None";
37 }
38}
39
41{
42 wireDecode(block);
43}
44
45template<encoding::Tag TAG>
46size_t
48{
49 if (m_policy == CachePolicyType::NONE) {
50 NDN_THROW(Error("CachePolicyType must be set"));
51 }
52
53 size_t length = 0;
54 length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType, static_cast<uint64_t>(m_policy));
55 length += encoder.prependVarNumber(length);
56 length += encoder.prependVarNumber(tlv::CachePolicy);
57 return length;
58}
59
61
62const Block&
64{
65 if (m_policy == CachePolicyType::NONE) {
66 NDN_THROW(Error("CachePolicyType must be set"));
67 }
68
69 if (m_wire.hasWire()) {
70 return m_wire;
71 }
72
73 EncodingEstimator estimator;
74 size_t estimatedSize = wireEncode(estimator);
75
76 EncodingBuffer buffer(estimatedSize, 0);
77 wireEncode(buffer);
78
79 m_wire = buffer.block();
80 return m_wire;
81}
82
83void
85{
86 if (wire.type() != tlv::CachePolicy) {
87 NDN_THROW(Error("CachePolicy", wire.type()));
88 }
89 m_wire = wire;
90 m_wire.parse();
91
92 m_policy = CachePolicyType::NONE;
93
94 auto it = m_wire.elements_begin();
95 if (it != m_wire.elements_end() && it->type() == tlv::CachePolicyType) {
96 m_policy = readNonNegativeIntegerAs<CachePolicyType>(*it);
98 NDN_THROW(Error("Unknown CachePolicyType " + std::to_string(to_underlying(m_policy))));
99 }
100 }
101 else {
102 NDN_THROW(Error("CachePolicyType is missing or out of order"));
103 }
104}
105
108{
109 switch (m_policy) {
111 return m_policy;
112 default:
114 }
115}
116
119{
120 m_policy = policy;
121 m_wire.reset();
122 return *this;
123}
124
125} // namespace ndn::lp
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition block.hpp:433
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition block.hpp:442
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition block.hpp:205
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
void reset() noexcept
Reset the Block to a default-constructed state.
Definition block.cpp:293
void parse() const
Parse TLV-VALUE into sub-elements.
Definition block.cpp:326
Represents a CachePolicy header field.
CachePolicyType getPolicy() const
Get policy type code.
void wireDecode(const Block &wire)
Get CachePolicyType from wire format.
const Block & wireEncode() const
Encode CachePolicy into wire format.
CachePolicy & setPolicy(CachePolicyType policy)
Set policy type code.
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition exception.hpp:56
@ CachePolicy
Definition tlv.hpp:47
@ CachePolicyType
Definition tlv.hpp:48
Contains classes and functions related to NDNLPv2.
CachePolicyType
Indicates the cache policy applied to a Data packet.
std::ostream & operator<<(std::ostream &os, CachePolicyType policy)
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition backports.hpp:44