blob: 37dad5ebc196a993757e69909d7d2e52ad01a6b1 [file] [log] [blame]
Eric Newberry4b5a5cf2015-08-06 22:00:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob6355e82017-08-10 21:27:08 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Eric Newberry4b5a5cf2015-08-06 22:00:43 -07004 *
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
24#include "cache-policy.hpp"
Davide Pesaventob6355e82017-08-10 21:27:08 -040025#include "../encoding/block-helpers.hpp"
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070026
27namespace ndn {
28namespace lp {
29
30std::ostream&
31operator<<(std::ostream& os, CachePolicyType policy)
32{
33 switch (policy) {
34 case CachePolicyType::NO_CACHE:
35 os << "NoCache";
36 break;
37 default:
38 os << "None";
39 break;
40 }
41
42 return os;
43}
44
45CachePolicy::CachePolicy()
46 : m_policy(CachePolicyType::NONE)
47{
48}
49
50CachePolicy::CachePolicy(const Block& block)
51{
52 wireDecode(block);
53}
54
55template<encoding::Tag TAG>
56size_t
57CachePolicy::wireEncode(EncodingImpl<TAG>& encoder) const
58{
59 if (m_policy == CachePolicyType::NONE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070060 BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070061 }
62 size_t length = 0;
63 length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType,
64 static_cast<uint32_t>(m_policy));
65 length += encoder.prependVarNumber(length);
66 length += encoder.prependVarNumber(tlv::CachePolicy);
67 return length;
68}
69
70template size_t
71CachePolicy::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
72
73template size_t
74CachePolicy::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
75
76const Block&
77CachePolicy::wireEncode() const
78{
79 if (m_policy == CachePolicyType::NONE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070080 BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070081 }
82
83 if (m_wire.hasWire()) {
84 return m_wire;
85 }
86
87 EncodingEstimator estimator;
88 size_t estimatedSize = wireEncode(estimator);
89
90 EncodingBuffer buffer(estimatedSize, 0);
91 wireEncode(buffer);
92
93 m_wire = buffer.block();
94
95 return m_wire;
96}
97
98void
99CachePolicy::wireDecode(const Block& wire)
100{
101 if (wire.type() != tlv::CachePolicy) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700102 BOOST_THROW_EXCEPTION(Error("expecting CachePolicy block"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -0700103 }
104
105 m_wire = wire;
106 m_wire.parse();
107
108 Block::element_const_iterator it = m_wire.elements_begin();
109 if (it != m_wire.elements_end() && it->type() == tlv::CachePolicyType) {
110 m_policy = static_cast<CachePolicyType>(readNonNegativeInteger(*it));
111 if (this->getPolicy() == CachePolicyType::NONE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700112 BOOST_THROW_EXCEPTION(Error("unknown CachePolicyType"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -0700113 }
114 }
115 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700116 BOOST_THROW_EXCEPTION(Error("expecting CachePolicyType block"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -0700117 }
118}
119
120CachePolicyType
121CachePolicy::getPolicy() const
122{
123 switch (m_policy) {
124 case CachePolicyType::NO_CACHE:
125 return m_policy;
126 default:
127 return CachePolicyType::NONE;
128 }
129}
130
131CachePolicy&
132CachePolicy::setPolicy(CachePolicyType policy)
133{
134 m_policy = policy;
135 m_wire.reset();
136 return *this;
137}
138
139} // namespace lp
140} // namespace ndn