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-2023 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 <enewberry@email.arizona.edu>
22  */
23 
26 
27 namespace ndn::lp {
28 
29 std::ostream&
30 operator<<(std::ostream& os, CachePolicyType policy)
31 {
32  switch (policy) {
34  return os << "NoCache";
35  default:
36  return os << "None";
37  }
38 }
39 
41  : m_policy(CachePolicyType::NONE)
42 {
43 }
44 
46 {
47  wireDecode(block);
48 }
49 
50 template<encoding::Tag TAG>
51 size_t
53 {
54  if (m_policy == CachePolicyType::NONE) {
55  NDN_THROW(Error("CachePolicyType must be set"));
56  }
57 
58  size_t length = 0;
59  length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType, static_cast<uint64_t>(m_policy));
60  length += encoder.prependVarNumber(length);
61  length += encoder.prependVarNumber(tlv::CachePolicy);
62  return length;
63 }
64 
66 
67 const Block&
69 {
70  if (m_policy == CachePolicyType::NONE) {
71  NDN_THROW(Error("CachePolicyType must be set"));
72  }
73 
74  if (m_wire.hasWire()) {
75  return m_wire;
76  }
77 
78  EncodingEstimator estimator;
79  size_t estimatedSize = wireEncode(estimator);
80 
81  EncodingBuffer buffer(estimatedSize, 0);
82  wireEncode(buffer);
83 
84  m_wire = buffer.block();
85 
86  return m_wire;
87 }
88 
89 void
91 {
92  if (wire.type() != tlv::CachePolicy) {
93  NDN_THROW(Error("CachePolicy", wire.type()));
94  }
95 
96  m_wire = wire;
97  m_wire.parse();
98 
99  auto it = m_wire.elements_begin();
100  if (it == m_wire.elements_end()) {
101  NDN_THROW(Error("Empty CachePolicy"));
102  }
103 
104  if (it->type() == tlv::CachePolicyType) {
105  m_policy = readNonNegativeIntegerAs<CachePolicyType>(*it);
106  if (getPolicy() == CachePolicyType::NONE) {
107  NDN_THROW(Error("Unknown CachePolicyType"));
108  }
109  }
110  else {
111  NDN_THROW(Error("CachePolicyType", it->type()));
112  }
113 }
114 
117 {
118  switch (m_policy) {
120  return m_policy;
121  default:
122  return CachePolicyType::NONE;
123  }
124 }
125 
128 {
129  m_policy = policy;
130  m_wire.reset();
131  return *this;
132 }
133 
134 } // 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
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
EncodingImpl< EncoderTag > EncodingBuffer
@ 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)