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