NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2022 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 {
28 namespace lp {
29 
30 std::ostream&
31 operator<<(std::ostream& os, CachePolicyType policy)
32 {
33  switch (policy) {
35  return os << "NoCache";
36  default:
37  return os << "None";
38  }
39 }
40 
42  : m_policy(CachePolicyType::NONE)
43 {
44 }
45 
47 {
48  wireDecode(block);
49 }
50 
51 template<encoding::Tag TAG>
52 size_t
54 {
55  if (m_policy == CachePolicyType::NONE) {
56  NDN_THROW(Error("CachePolicyType must be set"));
57  }
58 
59  size_t length = 0;
60  length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType, static_cast<uint64_t>(m_policy));
61  length += encoder.prependVarNumber(length);
62  length += encoder.prependVarNumber(tlv::CachePolicy);
63  return length;
64 }
65 
67 
68 const Block&
70 {
71  if (m_policy == CachePolicyType::NONE) {
72  NDN_THROW(Error("CachePolicyType must be set"));
73  }
74 
75  if (m_wire.hasWire()) {
76  return m_wire;
77  }
78 
79  EncodingEstimator estimator;
80  size_t estimatedSize = wireEncode(estimator);
81 
82  EncodingBuffer buffer(estimatedSize, 0);
83  wireEncode(buffer);
84 
85  m_wire = buffer.block();
86 
87  return m_wire;
88 }
89 
90 void
92 {
93  if (wire.type() != tlv::CachePolicy) {
94  NDN_THROW(Error("CachePolicy", wire.type()));
95  }
96 
97  m_wire = wire;
98  m_wire.parse();
99 
100  auto it = m_wire.elements_begin();
101  if (it == m_wire.elements_end()) {
102  NDN_THROW(Error("Empty CachePolicy"));
103  }
104 
105  if (it->type() == tlv::CachePolicyType) {
106  m_policy = readNonNegativeIntegerAs<CachePolicyType>(*it);
107  if (getPolicy() == CachePolicyType::NONE) {
108  NDN_THROW(Error("Unknown CachePolicyType"));
109  }
110  }
111  else {
112  NDN_THROW(Error("CachePolicyType", it->type()));
113  }
114 }
115 
118 {
119  switch (m_policy) {
121  return m_policy;
122  default:
123  return CachePolicyType::NONE;
124  }
125 }
126 
129 {
130  m_policy = policy;
131  m_wire.reset();
132  return *this;
133 }
134 
135 } // namespace lp
136 } // namespace ndn
void wireDecode(const Block &wire)
get CachePolicyType from wire format
represents a CachePolicy header field
Copyright (c) 2011-2015 Regents of the University of California.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CachePolicy)
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
CachePolicy & setPolicy(CachePolicyType policy)
set policy type code
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:221
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:433
#define NDN_THROW(e)
Definition: exception.hpp:61
const Block & wireEncode() const
encode CachePolicy into wire format
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:441
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:254
CachePolicyType
indicates the cache policy applied to a Data packet
CachePolicyType getPolicy() const
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:277
std::ostream & operator<<(std::ostream &os, CachePolicyType policy)
EncodingImpl< EncoderTag > EncodingBuffer
EncodingImpl< EstimatorTag > EncodingEstimator