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 | } |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 62 | |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 63 | size_t length = 0; |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 64 | length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType, static_cast<uint32_t>(m_policy)); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 65 | length += encoder.prependVarNumber(length); |
| 66 | length += encoder.prependVarNumber(tlv::CachePolicy); |
| 67 | return length; |
| 68 | } |
| 69 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 70 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CachePolicy); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 71 | |
| 72 | const Block& |
| 73 | CachePolicy::wireEncode() const |
| 74 | { |
| 75 | if (m_policy == CachePolicyType::NONE) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 76 | BOOST_THROW_EXCEPTION(Error("CachePolicyType must be set")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 77 | } |
| 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 | |
| 94 | void |
| 95 | CachePolicy::wireDecode(const Block& wire) |
| 96 | { |
| 97 | if (wire.type() != tlv::CachePolicy) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 98 | BOOST_THROW_EXCEPTION(Error("expecting CachePolicy block")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 99 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 108 | BOOST_THROW_EXCEPTION(Error("unknown CachePolicyType")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 112 | BOOST_THROW_EXCEPTION(Error("expecting CachePolicyType block")); |
Eric Newberry | 4b5a5cf | 2015-08-06 22:00:43 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | CachePolicyType |
| 117 | CachePolicy::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 | |
| 127 | CachePolicy& |
| 128 | CachePolicy::setPolicy(CachePolicyType policy) |
| 129 | { |
| 130 | m_policy = policy; |
| 131 | m_wire.reset(); |
| 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | } // namespace lp |
| 136 | } // namespace ndn |