Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame^] | 1 | /* -*- 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 | |
| 26 | namespace ndn { |
| 27 | namespace lp { |
| 28 | |
| 29 | std::ostream& |
| 30 | operator<<(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 | |
| 44 | CachePolicy::CachePolicy() |
| 45 | : m_policy(CachePolicyType::NONE) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | CachePolicy::CachePolicy(const Block& block) |
| 50 | { |
| 51 | wireDecode(block); |
| 52 | } |
| 53 | |
| 54 | template<encoding::Tag TAG> |
| 55 | size_t |
| 56 | CachePolicy::wireEncode(EncodingImpl<TAG>& encoder) const |
| 57 | { |
| 58 | if (m_policy == CachePolicyType::NONE) { |
| 59 | throw Error("CachePolicyType must be set"); |
| 60 | } |
| 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 | |
| 69 | template size_t |
| 70 | CachePolicy::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
| 71 | |
| 72 | template size_t |
| 73 | CachePolicy::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
| 74 | |
| 75 | const Block& |
| 76 | CachePolicy::wireEncode() const |
| 77 | { |
| 78 | if (m_policy == CachePolicyType::NONE) { |
| 79 | throw Error("CachePolicyType must be set"); |
| 80 | } |
| 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 | |
| 97 | void |
| 98 | CachePolicy::wireDecode(const Block& wire) |
| 99 | { |
| 100 | if (wire.type() != tlv::CachePolicy) { |
| 101 | throw Error("expecting CachePolicy block"); |
| 102 | } |
| 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) { |
| 111 | throw Error("unknown CachePolicyType"); |
| 112 | } |
| 113 | } |
| 114 | else { |
| 115 | throw Error("expecting CachePolicyType block"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | CachePolicyType |
| 120 | CachePolicy::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 | |
| 130 | CachePolicy& |
| 131 | CachePolicy::setPolicy(CachePolicyType policy) |
| 132 | { |
| 133 | m_policy = policy; |
| 134 | m_wire.reset(); |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | } // namespace lp |
| 139 | } // namespace ndn |