lp: add NonDiscovery field and tag
refs #4355
Change-Id: Ib3a64589d7ea3bbf204e295a75d91748ef9c85b9
diff --git a/src/lp/empty-value.hpp b/src/lp/empty-value.hpp
new file mode 100644
index 0000000..1587046
--- /dev/null
+++ b/src/lp/empty-value.hpp
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 Teng Liang <philoliang@email.arizona.edu>
+ */
+
+#ifndef NDN_CXX_LP_TLV_EMPTY_VALUE_HPP
+#define NDN_CXX_LP_TLV_EMPTY_VALUE_HPP
+
+namespace ndn {
+namespace lp {
+
+/**
+ * \brief represents a zero-length TLV-VALUE
+ */
+struct EmptyValue
+{
+};
+
+} // namespace lp
+} // namespace ndn
+
+#endif // NDN_CXX_LP_TLV_EMPTY_VALUE_HPP
\ No newline at end of file
diff --git a/src/lp/field-decl.hpp b/src/lp/field-decl.hpp
index a71aceb..755f595 100644
--- a/src/lp/field-decl.hpp
+++ b/src/lp/field-decl.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -22,6 +22,7 @@
#ifndef NDN_CXX_LP_FIELD_DECL_HPP
#define NDN_CXX_LP_FIELD_DECL_HPP
+#include "empty-value.hpp"
#include "field.hpp"
#include "tlv.hpp"
@@ -46,6 +47,21 @@
};
template<typename TlvType>
+struct DecodeHelper<TlvType, EmptyValue>
+{
+ static EmptyValue
+ decode(const Block& wire)
+ {
+ if (wire.value_size() != 0) {
+ BOOST_THROW_EXCEPTION(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
+ " must be empty"));
+ }
+
+ return EmptyValue{};
+ }
+};
+
+template<typename TlvType>
struct DecodeHelper<TlvType, uint64_t>
{
static uint64_t
@@ -62,7 +78,8 @@
decode(const Block& wire)
{
if (wire.value_size() == 0) {
- BOOST_THROW_EXCEPTION(ndn::tlv::Error(to_string(wire.type()) + " must not be empty"));
+ BOOST_THROW_EXCEPTION(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
+ " cannot be empty"));
}
return std::make_pair(wire.value_begin(), wire.value_end());
@@ -81,6 +98,19 @@
};
template<typename encoding::Tag TAG, typename TlvType>
+struct EncodeHelper<TAG, TlvType, EmptyValue>
+{
+ static size_t
+ encode(EncodingImpl<TAG>& encoder, const EmptyValue value)
+ {
+ size_t length = 0;
+ length += encoder.prependVarNumber(0);
+ length += encoder.prependVarNumber(TlvType::value);
+ return length;
+ }
+};
+
+template<typename encoding::Tag TAG, typename TlvType>
struct EncodeHelper<TAG, TlvType, uint64_t>
{
static size_t
diff --git a/src/lp/fields.hpp b/src/lp/fields.hpp
index 5612b16..c8cd0d9 100644
--- a/src/lp/fields.hpp
+++ b/src/lp/fields.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -84,6 +84,11 @@
tlv::TxSequence> TxSequenceField;
BOOST_CONCEPT_ASSERT((Field<TxSequenceField>));
+typedef FieldDecl<field_location_tags::Header,
+ EmptyValue,
+ tlv::NonDiscovery> NonDiscoveryField;
+BOOST_CONCEPT_ASSERT((Field<NonDiscoveryField>));
+
/**
* The value of the wire encoded field is the data between the provided iterators. During
* encoding, the data is copied from the Buffer into the wire buffer.
@@ -107,7 +112,8 @@
IncomingFaceIdField,
CongestionMarkField,
AckField,
- TxSequenceField
+ TxSequenceField,
+ NonDiscoveryField
> FieldSet;
} // namespace lp
diff --git a/src/lp/tags.hpp b/src/lp/tags.hpp
index 4bcadae..b4f430b 100644
--- a/src/lp/tags.hpp
+++ b/src/lp/tags.hpp
@@ -23,6 +23,7 @@
#define NDN_CXX_LP_TAGS_HPP
#include "cache-policy.hpp"
+#include "empty-value.hpp"
#include "../tag.hpp"
namespace ndn {
@@ -56,6 +57,13 @@
*/
typedef SimpleTag<uint64_t, 13> CongestionMarkTag;
+/** \class NonDiscoveryTag
+ * \brief a packet tag for NonDiscovery field
+ *
+ * This tag can be attached to Interest.
+ */
+typedef SimpleTag<EmptyValue, 14> NonDiscoveryTag;
+
} // namespace lp
} // namespace ndn
diff --git a/src/lp/tlv.hpp b/src/lp/tlv.hpp
index 5ab1a92..fb24ab3 100644
--- a/src/lp/tlv.hpp
+++ b/src/lp/tlv.hpp
@@ -38,12 +38,13 @@
Nack = 800,
NackReason = 801,
NextHopFaceId = 816,
+ IncomingFaceId = 817,
CachePolicy = 820,
CachePolicyType = 821,
- IncomingFaceId = 817,
CongestionMark = 832,
Ack = 836,
- TxSequence = 840
+ TxSequence = 840,
+ NonDiscovery = 844,
};
enum {
diff --git a/tests/unit-tests/lp/packet.t.cpp b/tests/unit-tests/lp/packet.t.cpp
index 8d4e78d..bd6b671 100644
--- a/tests/unit-tests/lp/packet.t.cpp
+++ b/tests/unit-tests/lp/packet.t.cpp
@@ -105,6 +105,26 @@
wire.begin(), wire.end());
}
+BOOST_AUTO_TEST_CASE(EncodeZeroLengthTlv)
+{
+ static const uint8_t expectedBlock[] = {
+ 0x64, 0x04, // LpPacket
+ 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
+ };
+
+ Packet packet1, packet2;
+ BOOST_CHECK_NO_THROW(packet1.set<NonDiscoveryField>(EmptyValue{}));
+ Block wire;
+ BOOST_REQUIRE_NO_THROW(wire = packet1.wireEncode());
+ BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
+ wire.begin(), wire.end());
+
+ BOOST_CHECK_NO_THROW(packet2.add<NonDiscoveryField>(EmptyValue{}));
+ BOOST_REQUIRE_NO_THROW(wire = packet2.wireEncode());
+ BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
+ wire.begin(), wire.end());
+}
+
BOOST_AUTO_TEST_CASE(EncodeSortOrder)
{
static const uint8_t expectedBlock[] = {
@@ -211,6 +231,20 @@
BOOST_CHECK_EQUAL(0xe8, *(last - 1));
}
+BOOST_AUTO_TEST_CASE(DecodeNonDiscoveryHeader)
+{
+ static const uint8_t inputBlock[] = {
+ 0x64, 0x04, // LpPacket
+ 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
+ };
+
+ Packet packet;
+ Block wire(inputBlock, sizeof(inputBlock));
+ BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
+ BOOST_CHECK_EQUAL(true, packet.has<NonDiscoveryField>());
+ BOOST_CHECK_NO_THROW(packet.get<NonDiscoveryField>());
+}
+
BOOST_AUTO_TEST_CASE(DecodeEmpty)
{
static const uint8_t inputBlock[] = {
@@ -222,6 +256,7 @@
BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
+ BOOST_CHECK_EQUAL(false, packet.has<NonDiscoveryField>());
}
BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)