Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 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" |
Davide Pesavento | b6355e8 | 2017-08-10 21:27:08 -0400 | [diff] [blame^] | 25 | #include "../encoding/block-helpers.hpp" |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace lp { |
| 29 | |
| 30 | std::ostream& |
| 31 | operator<<(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 | |
| 45 | CachePolicy::CachePolicy() |
| 46 | : m_policy(CachePolicyType::NONE) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | CachePolicy::CachePolicy(const Block& block) |
| 51 | { |
| 52 | wireDecode(block); |
| 53 | } |
| 54 | |
| 55 | template<encoding::Tag TAG> |
| 56 | size_t |
| 57 | CachePolicy::wireEncode(EncodingImpl<TAG>& encoder) const |
| 58 | { |
| 59 | if (m_policy == CachePolicyType::NONE) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 60 | BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 61 | } |
| 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 | |
| 70 | template size_t |
| 71 | CachePolicy::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
| 72 | |
| 73 | template size_t |
| 74 | CachePolicy::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
| 75 | |
| 76 | const Block& |
| 77 | CachePolicy::wireEncode() const |
| 78 | { |
| 79 | if (m_policy == CachePolicyType::NONE) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 80 | BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 81 | } |
| 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 | |
| 98 | void |
| 99 | CachePolicy::wireDecode(const Block& wire) |
| 100 | { |
| 101 | if (wire.type() != tlv::CachePolicy) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 102 | BOOST_THROW_EXCEPTION(Error("expecting CachePolicy block")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 103 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 112 | BOOST_THROW_EXCEPTION(Error("unknown CachePolicyType")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 116 | BOOST_THROW_EXCEPTION(Error("expecting CachePolicyType block")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | CachePolicyType |
| 121 | CachePolicy::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 | |
| 131 | CachePolicy& |
| 132 | CachePolicy::setPolicy(CachePolicyType policy) |
| 133 | { |
| 134 | m_policy = policy; |
| 135 | m_wire.reset(); |
| 136 | return *this; |
| 137 | } |
| 138 | |
| 139 | } // namespace lp |
| 140 | } // namespace ndn |