blob: aea7d05c29e01ef12223fdd9b28548d54334ca53 [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 }
Davide Pesavento88a0d812017-08-19 21:31:42 -040062
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070063 size_t length = 0;
Davide Pesavento88a0d812017-08-19 21:31:42 -040064 length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType, static_cast<uint32_t>(m_policy));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070065 length += encoder.prependVarNumber(length);
66 length += encoder.prependVarNumber(tlv::CachePolicy);
67 return length;
68}
69
Davide Pesavento88a0d812017-08-19 21:31:42 -040070NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CachePolicy);
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070071
72const Block&
73CachePolicy::wireEncode() const
74{
75 if (m_policy == CachePolicyType::NONE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070076 BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070077 }
78
79 if (m_wire.hasWire()) {
80 return m_wire;
81 }
82
83 EncodingEstimator estimator;
84 size_t estimatedSize = wireEncode(estimator);
85
86 EncodingBuffer buffer(estimatedSize, 0);
87 wireEncode(buffer);
88
89 m_wire = buffer.block();
90
91 return m_wire;
92}
93
94void
95CachePolicy::wireDecode(const Block& wire)
96{
97 if (wire.type() != tlv::CachePolicy) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070098 BOOST_THROW_EXCEPTION(Error("expecting CachePolicy block"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -070099 }
100
101 m_wire = wire;
102 m_wire.parse();
103
104 Block::element_const_iterator it = m_wire.elements_begin();
105 if (it != m_wire.elements_end() && it->type() == tlv::CachePolicyType) {
106 m_policy = static_cast<CachePolicyType>(readNonNegativeInteger(*it));
107 if (this->getPolicy() == CachePolicyType::NONE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700108 BOOST_THROW_EXCEPTION(Error("unknown CachePolicyType"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -0700109 }
110 }
111 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700112 BOOST_THROW_EXCEPTION(Error("expecting CachePolicyType block"));
Eric Newberry4b5a5cf2015-08-06 22:00:43 -0700113 }
114}
115
116CachePolicyType
117CachePolicy::getPolicy() const
118{
119 switch (m_policy) {
120 case CachePolicyType::NO_CACHE:
121 return m_policy;
122 default:
123 return CachePolicyType::NONE;
124 }
125}
126
127CachePolicy&
128CachePolicy::setPolicy(CachePolicyType policy)
129{
130 m_policy = policy;
131 m_wire.reset();
132 return *this;
133}
134
135} // namespace lp
136} // namespace ndn