lp: implement CachePolicy and CachePolicyType
refs #2963
Change-Id: I343ff95eb4ae61a4bc29603e63647b219661542c
diff --git a/src/lp/cache-policy.cpp b/src/lp/cache-policy.cpp
new file mode 100644
index 0000000..6bd1be9
--- /dev/null
+++ b/src/lp/cache-policy.cpp
@@ -0,0 +1,139 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#include "cache-policy.hpp"
+
+namespace ndn {
+namespace lp {
+
+std::ostream&
+operator<<(std::ostream& os, CachePolicyType policy)
+{
+ switch (policy) {
+ case CachePolicyType::NO_CACHE:
+ os << "NoCache";
+ break;
+ default:
+ os << "None";
+ break;
+ }
+
+ return os;
+}
+
+CachePolicy::CachePolicy()
+ : m_policy(CachePolicyType::NONE)
+{
+}
+
+CachePolicy::CachePolicy(const Block& block)
+{
+ wireDecode(block);
+}
+
+template<encoding::Tag TAG>
+size_t
+CachePolicy::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+ if (m_policy == CachePolicyType::NONE) {
+ throw Error("CachePolicyType must be set");
+ }
+ size_t length = 0;
+ length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType,
+ static_cast<uint32_t>(m_policy));
+ length += encoder.prependVarNumber(length);
+ length += encoder.prependVarNumber(tlv::CachePolicy);
+ return length;
+}
+
+template size_t
+CachePolicy::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
+
+template size_t
+CachePolicy::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
+
+const Block&
+CachePolicy::wireEncode() const
+{
+ if (m_policy == CachePolicyType::NONE) {
+ throw Error("CachePolicyType must be set");
+ }
+
+ if (m_wire.hasWire()) {
+ return m_wire;
+ }
+
+ EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_wire = buffer.block();
+
+ return m_wire;
+}
+
+void
+CachePolicy::wireDecode(const Block& wire)
+{
+ if (wire.type() != tlv::CachePolicy) {
+ throw Error("expecting CachePolicy block");
+ }
+
+ m_wire = wire;
+ m_wire.parse();
+
+ Block::element_const_iterator it = m_wire.elements_begin();
+ if (it != m_wire.elements_end() && it->type() == tlv::CachePolicyType) {
+ m_policy = static_cast<CachePolicyType>(readNonNegativeInteger(*it));
+ if (this->getPolicy() == CachePolicyType::NONE) {
+ throw Error("unknown CachePolicyType");
+ }
+ }
+ else {
+ throw Error("expecting CachePolicyType block");
+ }
+}
+
+CachePolicyType
+CachePolicy::getPolicy() const
+{
+ switch (m_policy) {
+ case CachePolicyType::NO_CACHE:
+ return m_policy;
+ default:
+ return CachePolicyType::NONE;
+ }
+}
+
+CachePolicy&
+CachePolicy::setPolicy(CachePolicyType policy)
+{
+ m_policy = policy;
+ m_wire.reset();
+ return *this;
+}
+
+} // namespace lp
+} // namespace ndn
diff --git a/src/lp/cache-policy.hpp b/src/lp/cache-policy.hpp
new file mode 100644
index 0000000..a67a9c3
--- /dev/null
+++ b/src/lp/cache-policy.hpp
@@ -0,0 +1,112 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#ifndef NDN_CXX_LP_CACHE_POLICY_HPP
+#define NDN_CXX_LP_CACHE_POLICY_HPP
+
+#include "../common.hpp"
+#include "../encoding/encoding-buffer.hpp"
+#include "../encoding/block-helpers.hpp"
+
+#include "tlv.hpp"
+
+namespace ndn {
+namespace lp {
+
+/**
+ * \brief indicates the cache policy applied to a Data packet
+ */
+enum class CachePolicyType {
+ NONE = 0,
+ NO_CACHE = 1
+};
+
+std::ostream&
+operator<<(std::ostream& os, CachePolicyType policy);
+
+/**
+ * \brief represents a CachePolicy header field
+ */
+class CachePolicy
+{
+public:
+ class Error : public ndn::tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : ndn::tlv::Error(what)
+ {
+ }
+ };
+
+ CachePolicy();
+
+ explicit
+ CachePolicy(const Block& block);
+
+ /**
+ * \brief prepend CachePolicy to encoder
+ * \pre getPolicy() != CachePolicyType::NONE
+ * \throw Error policy type is unset
+ */
+ template<encoding::Tag TAG>
+ size_t
+ wireEncode(EncodingImpl<TAG>& encoder) const;
+
+ /**
+ * \brief encode CachePolicy into wire format
+ */
+ const Block&
+ wireEncode() const;
+
+ /**
+ * \brief get CachePolicyType from wire format
+ */
+ void
+ wireDecode(const Block& wire);
+
+public: // policy type
+ /**
+ * \return policy type code
+ * \retval CachePolicyType::NONE if policy type is unset or has an unknown code
+ */
+ CachePolicyType
+ getPolicy() const;
+
+ /**
+ * \brief set policy type code
+ * \param policy a policy type code; CachePolicyType::NONE clears the policy
+ */
+ CachePolicy&
+ setPolicy(CachePolicyType policy);
+
+private:
+ CachePolicyType m_policy;
+ mutable Block m_wire;
+};
+
+} // namespace lp
+} // namespace ndn
+
+#endif // NDN_CXX_LP_CACHE_POLICY_HPP
\ No newline at end of file