meta-info: Add support for application-defined meta information blocks
Change-Id: If9cdef10728d2c285f3f1021cec57d90d0c04e9b
Refs: #2021
diff --git a/src/meta-info.cpp b/src/meta-info.cpp
index d829ed3..e3ba2be 100644
--- a/src/meta-info.cpp
+++ b/src/meta-info.cpp
@@ -22,10 +22,15 @@
#include "meta-info.hpp"
#include "encoding/block-helpers.hpp"
#include "encoding/encoding-buffer.hpp"
-#include "util/time.hpp"
+
+#include <boost/concept_check.hpp>
+#include <boost/type_traits.hpp>
namespace ndn {
+BOOST_CONCEPT_ASSERT((boost::EqualityComparable<MetaInfo>));
+BOOST_STATIC_ASSERT((boost::is_base_of<tlv::Error, MetaInfo::Error>::value));
+
MetaInfo::MetaInfo()
: m_type(TYPE_DEFAULT)
, m_freshnessPeriod(-1)
@@ -61,6 +66,62 @@
return *this;
}
+const std::list<Block>&
+MetaInfo::getAppMetaInfo() const
+{
+ return m_appMetaInfo;
+}
+
+MetaInfo&
+MetaInfo::setAppMetaInfo(const std::list<Block>& info)
+{
+ for (std::list<Block>::const_iterator i = info.begin(); i != info.end(); ++i) {
+ if (!(128 <= i->type() && i->type() <= 252))
+ throw Error("AppMetaInfo block has type outside the application range [128, 252]");
+ }
+
+ m_wire.reset();
+ m_appMetaInfo = info;
+ return *this;
+}
+
+MetaInfo&
+MetaInfo::addAppMetaInfo(const Block& block)
+{
+ if (!(128 <= block.type() && block.type() <= 252))
+ throw Error("AppMetaInfo block has type outside the application range [128, 252]");
+
+ m_wire.reset();
+ m_appMetaInfo.push_back(block);
+ return *this;
+}
+
+bool
+MetaInfo::removeAppMetaInfo(uint32_t tlvType)
+{
+ for (std::list<Block>::iterator iter = m_appMetaInfo.begin();
+ iter != m_appMetaInfo.end(); ++iter) {
+ if (iter->type() == tlvType) {
+ m_wire.reset();
+ m_appMetaInfo.erase(iter);
+ return true;
+ }
+ }
+ return false;
+}
+
+const Block*
+MetaInfo::findAppMetaInfo(uint32_t tlvType) const
+{
+ for (std::list<Block>::const_iterator iter = m_appMetaInfo.begin();
+ iter != m_appMetaInfo.end(); ++iter) {
+ if (iter->type() == tlvType) {
+ return &*iter;
+ }
+ }
+ return 0;
+}
+
template<bool T>
size_t
MetaInfo::wireEncode(EncodingImpl<T>& blk) const
@@ -69,9 +130,15 @@
// ContentType?
// FreshnessPeriod?
// FinalBlockId?
+ // AppMetaInfo*
size_t totalLength = 0;
+ for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin();
+ appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) {
+ totalLength += prependBlock(blk, *appMetaInfoItem);
+ }
+
// FinalBlockId
if (!m_finalBlockId.empty())
{
@@ -127,38 +194,48 @@
// MetaInfo ::= META-INFO-TYPE TLV-LENGTH
// ContentType?
// FreshnessPeriod?
+ // FinalBlockId?
+ // AppMetaInfo*
+
+
+ Block::element_const_iterator val = m_wire.elements_begin();
// ContentType
- Block::element_const_iterator val = m_wire.find(tlv::ContentType);
- if (val != m_wire.elements().end())
- {
- m_type = readNonNegativeInteger(*val);
- }
- else
+ if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
+ m_type = readNonNegativeInteger(*val);
+ ++val;
+ }
+ else {
m_type = TYPE_DEFAULT;
+ }
// FreshnessPeriod
- val = m_wire.find(tlv::FreshnessPeriod);
- if (val != m_wire.elements().end())
- {
- m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
- }
- else
+ if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) {
+ m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
+ ++val;
+ }
+ else {
m_freshnessPeriod = time::milliseconds::min();
+ }
// FinalBlockId
- val = m_wire.find(tlv::FinalBlockId);
- if (val != m_wire.elements().end())
- {
- m_finalBlockId = val->blockFromValue();
- if (m_finalBlockId.type() != tlv::NameComponent)
- {
- /// @todo May or may not throw exception later...
- m_finalBlockId.reset();
- }
- }
- else
+ if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) {
+ m_finalBlockId = val->blockFromValue();
+ if (m_finalBlockId.type() != tlv::NameComponent)
+ {
+ /// @todo May or may not throw exception later...
+ m_finalBlockId.reset();
+ }
+ ++val;
+ }
+ else {
m_finalBlockId.reset();
+ }
+
+ // AppMetaInfo (if any)
+ for (; val != m_wire.elements().end(); ++val) {
+ m_appMetaInfo.push_back(*val);
+ }
}
std::ostream&
@@ -172,10 +249,18 @@
os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
}
+ // FinalBlockId
if (!info.getFinalBlockId().empty()) {
os << ", FinalBlockId: ";
info.getFinalBlockId().toUri(os);
}
+
+ // App-defined MetaInfo items
+ for (std::list<Block>::const_iterator iter = info.getAppMetaInfo().begin();
+ iter != info.getAppMetaInfo().end(); ++iter) {
+ os << ", AppMetaInfoTlvType: " << iter->type();
+ }
+
return os;
}
diff --git a/src/meta-info.hpp b/src/meta-info.hpp
index 9958ba3..d73014e 100644
--- a/src/meta-info.hpp
+++ b/src/meta-info.hpp
@@ -27,15 +27,45 @@
#include "encoding/encoding-buffer.hpp"
#include "util/time.hpp"
#include "name-component.hpp"
+#include <list>
namespace ndn {
/**
* An MetaInfo holds the meta info which is signed inside the data packet.
+ *
+ * The class allows experimentation with application-defined meta information blocks,
+ * which slightly violates NDN-TLV specification. When using the application-defined
+ * meta information blocks be aware that this may result in packet drop (NFD and
+ * previous versions of ndn-cxx will gracefully accept such packet).
+ *
+ * The following definition of MetaInfo block is assumed in this implementation (compared
+ * to the NDN-TLV spec, definition extended to allow optional AppMetaInfo TLV blocks):
+ *
+ * MetaInfo ::= META-INFO-TYPE TLV-LENGTH
+ * ContentType?
+ * FreshnessPeriod?
+ * FinalBlockId?
+ * AppMetaInfo*
+ *
+ * AppMetaInfo ::= any TLV block with type in the restricted application range [128, 252]
+ *
+ * Note that AppMetaInfo blocks are application-defined and must have TLV type from
+ * the restricted application range [128, 252].
*/
class MetaInfo
{
public:
+ class Error : public tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : tlv::Error(what)
+ {
+ }
+ };
+
enum {
TYPE_DEFAULT = 0,
TYPE_LINK = 1,
@@ -81,6 +111,76 @@
MetaInfo&
setFinalBlockId(const name::Component& finalBlockId);
+ /**
+ * @brief Get all app-defined MetaInfo items
+ *
+ * @note Warning: Experimental API, which may change or disappear in the future
+ *
+ * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
+ * is called before *AppMetaInfo, all app-defined blocks will be lost
+ */
+ const std::list<Block>&
+ getAppMetaInfo() const;
+
+ /**
+ * @brief Set app-defined MetaInfo items
+ *
+ * This method will replace all existing app-defined MetaInfo items, if they existed.
+ *
+ * @throw Error if some block in @p info has type not in the application range
+ * (http://named-data.net/doc/ndn-tlv/types.html)
+ *
+ * @note Warning: Experimental API, which may change or disappear in the future
+ *
+ * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
+ * is called before *AppMetaInfo, all app-defined blocks will be lost
+ */
+ MetaInfo&
+ setAppMetaInfo(const std::list<Block>& info);
+
+ /**
+ * @brief Add an app-defined MetaInfo item
+ *
+ * @throw Error if @p block has type not in the application range
+ * (http://named-data.net/doc/ndn-tlv/types.html)
+ *
+ * @note Warning: Experimental API, which may change or disappear in the future
+ *
+ * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
+ * is called before *AppMetaInfo, all app-defined blocks will be lost
+ */
+ MetaInfo&
+ addAppMetaInfo(const Block& block);
+
+ /**
+ * @brief Remove a first app-defined MetaInfo item with type @p tlvType
+ *
+ * @return true if an item was deleted
+ *
+ * @note Warning: Experimental API, which may change or disappear in the future
+ *
+ * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
+ * is called before *AppMetaInfo, all app-defined blocks will be lost
+ */
+ bool
+ removeAppMetaInfo(uint32_t tlvType);
+
+ /**
+ * @brief Find a first app-defined MetaInfo item of type @p tlvType
+ *
+ * @return NULL if an item is not found, otherwise const pointer to the item
+ *
+ * @throw Error if @p tlvType is not in the application range
+ * (http://named-data.net/doc/ndn-tlv/types.html)
+ *
+ * @note Warning: Experimental API, which may change or disappear in the future
+ *
+ * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
+ * is called before *AppMetaInfo, all app-defined blocks will be lost
+ */
+ const Block*
+ findAppMetaInfo(uint32_t tlvType) const;
+
public: // EqualityComparable concept
bool
operator==(const MetaInfo& other) const;
@@ -92,6 +192,7 @@
uint32_t m_type;
time::milliseconds m_freshnessPeriod;
name::Component m_finalBlockId;
+ std::list<Block> m_appMetaInfo;
mutable Block m_wire;
};