lp: Nack abstraction
refs #2878
Change-Id: Id36c9f066ccc2e9e1807e37dc0bf6e308645f556
diff --git a/src/lp/nack-header.cpp b/src/lp/nack-header.cpp
new file mode 100644
index 0000000..22fccae
--- /dev/null
+++ b/src/lp/nack-header.cpp
@@ -0,0 +1,140 @@
+/* -*- 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 "nack-header.hpp"
+
+namespace ndn {
+namespace lp {
+
+std::ostream&
+operator<<(std::ostream& os, NackReason reason)
+{
+ switch (reason) {
+ case NackReason::CONGESTION:
+ os << "Congestion";
+ break;
+ case NackReason::DUPLICATE:
+ os << "Duplicate";
+ break;
+ case NackReason::NO_ROUTE:
+ os << "NoRoute";
+ break;
+ default:
+ os << "None";
+ break;
+ }
+ return os;
+}
+
+NackHeader::NackHeader()
+ : m_reason(NackReason::NONE)
+{
+}
+
+NackHeader::NackHeader(const Block& block)
+{
+ wireDecode(block);
+}
+
+template<encoding::Tag TAG>
+size_t
+NackHeader::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+ size_t length = 0;
+ length += prependNonNegativeIntegerBlock(encoder, tlv::NackReason,
+ static_cast<uint32_t>(m_reason));
+ length += encoder.prependVarNumber(length);
+ length += encoder.prependVarNumber(tlv::Nack);
+ return length;
+}
+
+template size_t
+NackHeader::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
+
+template size_t
+NackHeader::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
+
+const Block&
+NackHeader::wireEncode() const
+{
+ 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
+NackHeader::wireDecode(const Block& wire)
+{
+ if (wire.type() != tlv::Nack) {
+ throw ndn::tlv::Error("expecting Nack block");
+ }
+
+ m_wire = wire;
+ m_wire.parse();
+ m_reason = NackReason::NONE;
+
+ if (m_wire.elements_size() > 0) {
+ Block::element_const_iterator it = m_wire.elements_begin();
+
+ if (it->type() == tlv::NackReason) {
+ m_reason = static_cast<NackReason>(readNonNegativeInteger(*it));
+ }
+ else {
+ throw ndn::tlv::Error("expecting NackReason block");
+ }
+ }
+}
+
+NackReason
+NackHeader::getReason() const
+{
+ switch (m_reason) {
+ case NackReason::CONGESTION:
+ case NackReason::DUPLICATE:
+ case NackReason::NO_ROUTE:
+ return m_reason;
+ default:
+ return NackReason::NONE;
+ }
+}
+
+NackHeader&
+NackHeader::setReason(NackReason reason)
+{
+ m_reason = reason;
+ m_wire.reset();
+ return *this;
+}
+
+} // namespace lp
+} // namespace ndn
diff --git a/src/lp/nack-header.hpp b/src/lp/nack-header.hpp
new file mode 100644
index 0000000..4544f54
--- /dev/null
+++ b/src/lp/nack-header.hpp
@@ -0,0 +1,93 @@
+/* -*- 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_NACK_HEADER_HPP
+#define NDN_CXX_LP_NACK_HEADER_HPP
+
+#include "../common.hpp"
+#include "../encoding/encoding-buffer.hpp"
+#include "../encoding/block-helpers.hpp"
+
+#include "tlv.hpp"
+
+namespace ndn {
+namespace lp {
+
+/**
+ * \brief indicates the reason type of a network NACK
+ */
+enum class NackReason {
+ NONE = 0,
+ CONGESTION = 50,
+ DUPLICATE = 100,
+ NO_ROUTE = 150
+};
+
+std::ostream&
+operator<<(std::ostream& os, NackReason reason);
+
+/**
+ * \brief represents a Network NACK header
+ */
+class NackHeader
+{
+public:
+ NackHeader();
+
+ explicit
+ NackHeader(const Block& block);
+
+ template<encoding::Tag TAG>
+ size_t
+ wireEncode(EncodingImpl<TAG>& encoder) const;
+
+ const Block&
+ wireEncode() const;
+
+ void
+ wireDecode(const Block& wire);
+
+public: // reason
+ /**
+ * \return reason code
+ * \retval NackReason::NONE if NackReason element does not exist or has an unknown code
+ */
+ NackReason
+ getReason() const;
+
+ /**
+ * \brief set reason code
+ * \param reason a reason code; NackReason::NONE clears the reason
+ */
+ NackHeader&
+ setReason(NackReason reason);
+
+private:
+ NackReason m_reason;
+ mutable Block m_wire;
+};
+
+} // namespace lp
+} // namespace ndn
+
+#endif // NDN_CXX_LP_NACK_HEADER_HPP
\ No newline at end of file
diff --git a/src/lp/nack.cpp b/src/lp/nack.cpp
new file mode 100644
index 0000000..c31a114
--- /dev/null
+++ b/src/lp/nack.cpp
@@ -0,0 +1,40 @@
+/* -*- 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 "nack.hpp"
+
+namespace ndn {
+namespace lp {
+
+Nack::Nack(const Interest& interest)
+ : m_interest(interest)
+{
+}
+
+Nack::Nack(Interest&& interest)
+ : m_interest(interest)
+{
+}
+
+} // namespace lp
+} // namespace ndn
\ No newline at end of file
diff --git a/src/lp/nack.hpp b/src/lp/nack.hpp
new file mode 100644
index 0000000..9512749
--- /dev/null
+++ b/src/lp/nack.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_NACK_HPP
+#define NDN_CXX_LP_NACK_HPP
+
+#include "../common.hpp"
+#include "../tag-host.hpp"
+#include "../interest.hpp"
+
+#include "nack-header.hpp"
+
+namespace ndn {
+namespace lp {
+
+/**
+ * \brief represents a Network NACK
+ * \detail This type binds a NackHeader and an Interest, and is intended for use in network layer.
+ */
+class Nack : public TagHost
+{
+public:
+ Nack() = default;
+
+ explicit
+ Nack(const Interest& interest);
+
+ explicit
+ Nack(Interest&& interest);
+
+public: // getter/setter
+ const Interest&
+ getInterest() const
+ {
+ return m_interest;
+ }
+
+ Interest&
+ getInterest()
+ {
+ return m_interest;
+ }
+
+ const NackHeader&
+ getHeader() const
+ {
+ return m_header;
+ }
+
+ NackHeader&
+ getHeader()
+ {
+ return m_header;
+ }
+
+ Nack&
+ setHeader(const NackHeader& header)
+ {
+ m_header = header;
+ return *this;
+ }
+
+ Nack&
+ setHeader(NackHeader&& header)
+ {
+ m_header = header;
+ return *this;
+ }
+
+public: // NackHeader proxy
+ NackReason
+ getReason() const
+ {
+ return m_header.getReason();
+ }
+
+ Nack&
+ setReason(NackReason reason)
+ {
+ m_header.setReason(reason);
+ return *this;
+ }
+
+private:
+ Interest m_interest;
+ NackHeader m_header;
+};
+
+} // namespace lp
+} // namespace ndn
+
+#endif // NDN_CXX_LP_NACK_HPP
\ No newline at end of file
diff --git a/tests/unit-tests/lp/nack-header.t.cpp b/tests/unit-tests/lp/nack-header.t.cpp
new file mode 100644
index 0000000..515926a
--- /dev/null
+++ b/tests/unit-tests/lp/nack-header.t.cpp
@@ -0,0 +1,103 @@
+/* -*- 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 "lp/nack-header.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace lp {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(LpNackHeader)
+
+BOOST_AUTO_TEST_CASE(Encode)
+{
+ NackHeader header;
+ header.setReason(NackReason::DUPLICATE);
+
+ Block wire;
+ BOOST_REQUIRE_NO_THROW(wire = header.wireEncode());
+
+ // Sample encoded value obtained with:
+ // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+ // printf("0x%02x, ", *it);
+ // }
+
+ // Contains NackReason::DUPLICATE
+ static const uint8_t expectedBlock[] = {
+ 0xfd, 0x03, 0x20, 0x05, 0xfd, 0x03, 0x21, 0x01, 0x64,
+ };
+
+ BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
+ wire.begin(), wire.end());
+
+ BOOST_REQUIRE_NO_THROW(header.wireDecode(wire));
+}
+
+BOOST_AUTO_TEST_CASE(DecodeUnknownReasonCode)
+{
+ static const uint8_t expectedBlock[] = {
+ 0xfd, 0x03, 0x20, 0x08, 0xfd, 0x03, 0x21, 0x04, 0xff, 0xff, 0xff, 0xff,
+ };
+
+ NackHeader header;
+ Block wire(expectedBlock, sizeof(expectedBlock));
+ BOOST_REQUIRE_NO_THROW(header.wireDecode(wire));
+ Block wireEncoded;
+ BOOST_REQUIRE_NO_THROW(wireEncoded = header.wireEncode());
+ BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
+ wireEncoded.begin(), wireEncoded.end());
+ BOOST_CHECK_EQUAL(header.getReason(), NackReason::NONE);
+}
+
+BOOST_AUTO_TEST_CASE(DecodeOmitReason)
+{
+ static const uint8_t expectedBlock[] = {
+ 0xfd, 0x03, 0x20, 0x00,
+ };
+
+ NackHeader header;
+ Block wire(expectedBlock, sizeof(expectedBlock));
+ BOOST_REQUIRE_NO_THROW(header.wireDecode(wire));
+ Block wireEncoded;
+ BOOST_REQUIRE_NO_THROW(wireEncoded = header.wireEncode());
+ BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
+ wireEncoded.begin(), wireEncoded.end());
+ BOOST_CHECK_EQUAL(header.getReason(), NackReason::NONE);
+}
+
+BOOST_AUTO_TEST_CASE(Reason)
+{
+ NackHeader header;
+ BOOST_CHECK_EQUAL(header.getReason(), NackReason::NONE);
+
+ header.setReason(NackReason::DUPLICATE);
+ BOOST_CHECK_EQUAL(header.getReason(), NackReason::DUPLICATE);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace lp
+} // namespace ndn
\ No newline at end of file
diff --git a/tests/unit-tests/lp/nack.t.cpp b/tests/unit-tests/lp/nack.t.cpp
new file mode 100644
index 0000000..ad09455
--- /dev/null
+++ b/tests/unit-tests/lp/nack.t.cpp
@@ -0,0 +1,63 @@
+/* -*- 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 "lp/nack.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace lp {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(LpNack)
+
+BOOST_AUTO_TEST_CASE(Members)
+{
+ Name name("ndn:/test");
+ Interest interest(name);
+ Nack nack(interest);
+
+ BOOST_CHECK_EQUAL(nack.getInterest().getName(), name);
+
+ NackHeader header;
+ header.setReason(NackReason::CONGESTION);
+ nack.setHeader(header);
+ BOOST_CHECK_EQUAL(nack.getHeader().getReason(), header.getReason());
+
+ BOOST_CHECK_EQUAL(nack.getHeader().getReason(), nack.getReason());
+
+ nack.setReason(NackReason::DUPLICATE);
+ BOOST_CHECK_EQUAL(nack.getReason(), NackReason::DUPLICATE);
+
+ nack.setReason(NackReason::NO_ROUTE);
+ BOOST_CHECK_EQUAL(nack.getReason(), NackReason::NO_ROUTE);
+
+ Nack nack2(interest);
+ BOOST_CHECK_EQUAL(nack2.getReason(), NackReason::NONE);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace lp
+} // namespace ndn
\ No newline at end of file