meta-info: allow typed name component in FinalBlockId

refs #4526

Change-Id: I3b6667928fa47c631e45ff5f0ca2c5030c5cc2ad
This commit is contained in:
Junxiao Shi committed 2018-04-01 23:53:40 +00:00
1 parent b849f61b6b
commit ebfe4a207f
11 files changed
+165 -182

No files matched your search

+16 -2
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2017 Regents of the University of California.
* Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -246,13 +246,27 @@ Data::setContentType(uint32_t type)
}
Data&
Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
Data::setFreshnessPeriod(time::milliseconds freshnessPeriod)
{
resetWire();
m_metaInfo.setFreshnessPeriod(freshnessPeriod);
return *this;
}
Data&
Data::setFinalBlock(optional<name::Component> finalBlockId)
{
resetWire();
m_metaInfo.setFinalBlock(std::move(finalBlockId));
return *this;
}
name::Component
Data::getFinalBlockId() const
{
return m_metaInfo.getFinalBlockId();
}
Data&
Data::setFinalBlockId(const name::Component& finalBlockId)
{
+20 -6
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2017 Regents of the University of California.
* Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -206,21 +206,35 @@ public: // MetaInfo fields
Data&
setContentType(uint32_t type);
const time::milliseconds&
time::milliseconds
getFreshnessPeriod() const
{
return m_metaInfo.getFreshnessPeriod();
}
Data&
setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
setFreshnessPeriod(time::milliseconds freshnessPeriod);
const name::Component&
getFinalBlockId() const
const optional<name::Component>&
getFinalBlock() const
{
return m_metaInfo.getFinalBlockId();
return m_metaInfo.getFinalBlock();
}
Data&
setFinalBlock(optional<name::Component> finalBlockId);
/** @deprecated Use @c getFinalBlock
* @sa MetaInfo::getFinalBlockId
*/
NDN_CXX_DEPRECATED
name::Component
getFinalBlockId() const;
/** @deprecated Use @c setFinalBlock
* @sa MetaInfo::setFinalBlockId
*/
NDN_CXX_DEPRECATED
Data&
setFinalBlockId(const name::Component& finalBlockId);
+18 -14
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2017 Regents of the University of California.
* Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -63,13 +63,22 @@ MetaInfo::setFreshnessPeriod(time::milliseconds freshnessPeriod)
}
MetaInfo&
MetaInfo::setFinalBlockId(const name::Component& finalBlockId)
MetaInfo::setFinalBlock(optional<name::Component> finalBlockId)
{
m_wire.reset();
m_finalBlockId = finalBlockId;
m_finalBlockId = std::move(finalBlockId);
return *this;
}
MetaInfo&
MetaInfo::setFinalBlockId(const name::Component& finalBlockId)
{
if (finalBlockId.isGeneric() && finalBlockId.empty()) {
return setFinalBlock(nullopt);
}
return setFinalBlock(finalBlockId);
}
const std::list<Block>&
MetaInfo::getAppMetaInfo() const
{
@@ -146,8 +155,8 @@ MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const
}
// FinalBlockId
if (!m_finalBlockId.empty()) {
totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, m_finalBlockId);
if (m_finalBlockId) {
totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, *m_finalBlockId);
}
// FreshnessPeriod
@@ -218,16 +227,11 @@ MetaInfo::wireDecode(const Block& wire)
// FinalBlockId
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 = name::Component();
}
m_finalBlockId.emplace(val->blockFromValue());
++val;
}
else {
m_finalBlockId = name::Component();
m_finalBlockId = nullopt;
}
// AppMetaInfo (if any)
@@ -248,9 +252,9 @@ operator<<(std::ostream& os, const MetaInfo& info)
}
// FinalBlockId
if (!info.getFinalBlockId().empty()) {
if (info.getFinalBlock()) {
os << ", FinalBlockId: ";
info.getFinalBlockId().toUri(os);
info.getFinalBlock()->toUri(os);
}
// App-defined MetaInfo items
+58 -32
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2017 Regents of the University of California.
* Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -87,27 +87,71 @@ public:
wireDecode(const Block& wire);
public: // getter/setter
/** @brief return ContentType
*
* If ContentType element is omitted, returns \c tlv::ContentType_Blob.
*/
uint32_t
getType() const;
getType() const
{
return m_type;
}
/** @brief set ContentType
* @param type a code defined in tlv::ContentTypeValue
* @param type a number defined in \c tlv::ContentTypeValue
*/
MetaInfo&
setType(uint32_t type);
const time::milliseconds&
getFreshnessPeriod() const;
/** @brief return FreshnessPeriod
*
* If FreshnessPeriod element is omitted, returns \c DEFAULT_FRESHNESS_PERIOD.
*/
time::milliseconds
getFreshnessPeriod() const
{
return m_freshnessPeriod;
}
/** @brief set FreshnessPeriod
* @throw std::invalid_argument specified FreshnessPeriod is < 0
* @throw std::invalid_argument specified FreshnessPeriod is negative
*/
MetaInfo&
setFreshnessPeriod(time::milliseconds freshnessPeriod);
const name::Component&
getFinalBlockId() const;
/** @brief return FinalBlockId
*/
const optional<name::Component>&
getFinalBlock() const
{
return m_finalBlockId;
}
/** @brief set FinalBlockId
*/
MetaInfo&
setFinalBlock(optional<name::Component> finalBlockId);
/** @brief return FinalBlockId
* @deprecated use @c getFinalBlock
*
* If FinalBlockId element is omitted, returns a default-constructed @c name::Component.
* This is indistinguishable from having an empty GenericNameComponent as FinalBlockId.
*/
NDN_CXX_DEPRECATED
name::Component
getFinalBlockId() const
{
return getFinalBlock().value_or(name::Component());
}
/** @brief set FinalBlockId
* @deprecated use @c setFinalBlock
*
* Passing a default-constructed @c name::Component removes FinalBlockId element.
* This API does not support adding an empty GenericNameComponent as FinalBlockId.
*/
NDN_CXX_DEPRECATED
MetaInfo&
setFinalBlockId(const name::Component& finalBlockId);
@@ -117,7 +161,7 @@ public: // 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
* @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock
* is called before *AppMetaInfo, all app-defined blocks will be lost
*/
const std::list<Block>&
@@ -133,7 +177,7 @@ public: // 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
* @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock
* is called before *AppMetaInfo, all app-defined blocks will be lost
*/
MetaInfo&
@@ -147,7 +191,7 @@ public: // 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
* @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock
* is called before *AppMetaInfo, all app-defined blocks will be lost
*/
MetaInfo&
@@ -160,7 +204,7 @@ public: // 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
* @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock
* is called before *AppMetaInfo, all app-defined blocks will be lost
*/
bool
@@ -176,7 +220,7 @@ public: // 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
* @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock
* is called before *AppMetaInfo, all app-defined blocks will be lost
*/
const Block*
@@ -192,7 +236,7 @@ public: // EqualityComparable concept
private:
uint32_t m_type;
time::milliseconds m_freshnessPeriod;
name::Component m_finalBlockId;
optional<name::Component> m_finalBlockId;
std::list<Block> m_appMetaInfo;
mutable Block m_wire;
@@ -203,24 +247,6 @@ NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(MetaInfo);
std::ostream&
operator<<(std::ostream& os, const MetaInfo& info);
inline uint32_t
MetaInfo::getType() const
{
return m_type;
}
inline const time::milliseconds&
MetaInfo::getFreshnessPeriod() const
{
return m_freshnessPeriod;
}
inline const name::Component&
MetaInfo::getFinalBlockId() const
{
return m_finalBlockId;
}
inline bool
MetaInfo::operator==(const MetaInfo& other) const
{
+1 -1
View File
@@ -313,7 +313,7 @@ Dispatcher::sendStatusDatasetSegment(const Name& dataName, const Block& content,
MetaInfo metaInfo;
if (isFinalBlock) {
metaInfo.setFinalBlockId(dataName[-1]);
metaInfo.setFinalBlock(dataName[-1]);
}
sendData(dataName, content, metaInfo, destination, imsFresh);
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2017 Regents of the University of California.
/*
* Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -160,9 +160,9 @@ CertificateBundleFetcher::dataCallback(const Data& bundleData,
else {
state->setTag(make_shared<BundleNameTag>(bundleData.getName()));
const name::Component& finalBlockId = bundleData.getMetaInfo().getFinalBlockId();
if (!finalBlockId.empty()) {
state->setTag(make_shared<FinalBlockIdTag>(finalBlockId));
const auto& finalBlockId = bundleData.getFinalBlock();
if (!finalBlockId) {
state->setTag(make_shared<FinalBlockIdTag>(*finalBlockId));
}
Block bundleContent = bundleData.getContent();
+2 -2
View File
@@ -131,8 +131,8 @@ SegmentFetcher::afterValidationSuccess(const Data& data,
m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
data.getContent().value_size());
afterSegmentValidated(data);
const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
if (finalBlockId.empty() || (finalBlockId > currentSegment)) {
const auto& finalBlockId = data.getFinalBlock();
if (!finalBlockId || (*finalBlockId > currentSegment)) {
fetchNextSegment(origInterest, data.getName(), currentSegment.toSegment() + 1, self);
}
else {
+42 -117
View File
@@ -22,6 +22,7 @@
#include "meta-info.hpp"
#include "data.hpp"
#include "block-literal.hpp"
#include "boost-test.hpp"
namespace ndn {
@@ -29,79 +30,53 @@ namespace tests {
BOOST_AUTO_TEST_SUITE(TestMetaInfo)
const uint8_t MetaInfo1[] = {0x14, 0x04, 0x19, 0x02, 0x27, 0x10};
const uint8_t MetaInfo2[] = {0x14, 0x14, 0x19, 0x02, 0x27, 0x10, 0x1a, 0x0e, 0x08, 0x0c,
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77, 0x6f, 0x72, 0x6c,
0x64, 0x21};
const uint8_t MetaInfo3[] = {0x14, 0x17, 0x18, 0x01, 0x01, 0x19, 0x02, 0x27, 0x10, 0x1a,
0x0e, 0x08, 0x0c, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77,
0x6f, 0x72, 0x6c, 0x64, 0x21};
BOOST_AUTO_TEST_CASE(Encode)
BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(EncodeDecodeEquality, 1)
BOOST_AUTO_TEST_CASE(EncodeDecodeEquality)
{
MetaInfo meta;
meta.setType(tlv::ContentType_Blob);
meta.setFreshnessPeriod(10_s);
// default values
MetaInfo a("1406 type=180100 freshness=190100"_block);
BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob);
BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms);
BOOST_CHECK(!a.getFinalBlock());
BOOST_CHECK_EQUAL(a.getFinalBlockId(), name::Component());
BOOST_CHECK_EQUAL(a, a);
BOOST_REQUIRE_NO_THROW(meta.wireEncode());
BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo1, MetaInfo1+sizeof(MetaInfo1),
meta.wireEncode().begin(), meta.wireEncode().end());
meta.setFinalBlockId(name::Component("hello,world!"));
BOOST_REQUIRE_NO_THROW(meta.wireEncode());
BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo2, MetaInfo2+sizeof(MetaInfo2),
meta.wireEncode().begin(), meta.wireEncode().end());
meta.setType(tlv::ContentType_Link);
BOOST_REQUIRE_NO_THROW(meta.wireEncode());
BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo3, MetaInfo3+sizeof(MetaInfo3),
meta.wireEncode().begin(), meta.wireEncode().end());
}
BOOST_AUTO_TEST_CASE(Decode)
{
MetaInfo meta(Block(MetaInfo1, sizeof(MetaInfo1)));
BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob));
BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s);
BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component());
meta.wireDecode(Block(MetaInfo2, sizeof(MetaInfo2)));
BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob));
BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s);
BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!"));
meta.wireDecode(Block(MetaInfo3, sizeof(MetaInfo3)));
BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Link));
BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s);
BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!"));
}
BOOST_AUTO_TEST_CASE(EqualityChecks)
{
MetaInfo a;
MetaInfo b;
BOOST_CHECK_EQUAL(a == b, true);
BOOST_CHECK_EQUAL(a != b, false);
BOOST_CHECK_NE(a, b);
b.setType(a.getType());
b.setFreshnessPeriod(a.getFreshnessPeriod());
b.setFinalBlock(a.getFinalBlock());
BOOST_CHECK(b.wireEncode() == "1400"_block);
BOOST_CHECK_EQUAL(a, b); // expected failure #4569
a.setFreshnessPeriod(10_s);
BOOST_CHECK_EQUAL(a == b, false);
BOOST_CHECK_EQUAL(a != b, true);
// non-default values
Block wire2 = "140C type=180101 freshness=190266B2 finalblock=1A03080141"_block;
a.wireDecode(wire2);
BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Link);
BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 26290_ms);
BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component("A"));
BOOST_CHECK_EQUAL(a.getFinalBlockId(), name::Component("A"));
BOOST_CHECK_NE(a, b);
b.setFreshnessPeriod(90_s);
BOOST_CHECK_EQUAL(a == b, false);
BOOST_CHECK_EQUAL(a != b, true);
b.setType(a.getType());
b.setFreshnessPeriod(a.getFreshnessPeriod());
b.setFinalBlockId(a.getFinalBlockId());
BOOST_CHECK(b.wireEncode() == wire2);
BOOST_CHECK_EQUAL(a, b);
b.setFreshnessPeriod(10_s);
BOOST_CHECK_EQUAL(a == b, true);
BOOST_CHECK_EQUAL(a != b, false);
// FinalBlockId is typed name component
Block wire3 = "1405 finalblock=1A03DD0141"_block;
a.wireDecode(wire3);
BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob);
BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms);
BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component::fromEscapedString("221=A"));
BOOST_CHECK_NE(a, b);
a.setType(10);
BOOST_CHECK_EQUAL(a == b, false);
BOOST_CHECK_EQUAL(a != b, true);
b.setType(10);
BOOST_CHECK_EQUAL(a == b, true);
BOOST_CHECK_EQUAL(a != b, false);
b.setType(a.getType());
b.setFreshnessPeriod(a.getFreshnessPeriod());
b.setFinalBlockId(a.getFinalBlockId());
BOOST_CHECK(b.wireEncode() == wire3);
BOOST_CHECK_EQUAL(a, b);
}
BOOST_AUTO_TEST_CASE(AppMetaInfo)
@@ -109,7 +84,7 @@ BOOST_AUTO_TEST_CASE(AppMetaInfo)
MetaInfo info1;
info1.setType(196);
info1.setFreshnessPeriod(3600_ms);
info1.setFinalBlockId(name::Component("/att/final"));
info1.setFinalBlock(name::Component("/att/final"));
uint32_t ints[5] = {128, 129, 130, 131, 132};
std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex",
@@ -188,56 +163,6 @@ BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange)
BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
}
BOOST_AUTO_TEST_CASE(EncodeDecodeFreshnessPeriod)
{
const uint8_t expectedDefault[] = {
0x14, 0x00, // MetaInfo
};
const uint8_t expected1000ms[] = {
0x14, 0x04, // MetaInfo
0x19, 0x02, // FreshnessPeriod
0x03, 0xe8 // 1000ms
};
MetaInfo info;
BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
expectedDefault, expectedDefault + sizeof(expectedDefault));
info.setFreshnessPeriod(time::milliseconds::zero());
BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
expectedDefault, expectedDefault + sizeof(expectedDefault));
info.setFreshnessPeriod(1000_ms);
BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
expected1000ms, expected1000ms + sizeof(expected1000ms));
const uint8_t inputDefault[] = {
0x14, 0x03, // MetaInfo
0x19, 0x01, // FreshnessPeriod
0x00 // 0ms
};
const uint8_t input2000ms[] = {
0x14, 0x04, // MetaInfo
0x19, 0x02, // FreshnessPeriod
0x07, 0xd0 // 2000ms
};
Block inputDefaultBlock(inputDefault, sizeof(inputDefault));
BOOST_CHECK_NO_THROW(info.wireDecode(inputDefaultBlock));
BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds::zero());
Block input2000msBlock(input2000ms, sizeof(input2000ms));
BOOST_CHECK_NO_THROW(info.wireDecode(input2000msBlock));
BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 2000_ms);
BOOST_CHECK_NO_THROW(info.setFreshnessPeriod(10000_ms));
BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 10000_ms);
BOOST_CHECK_THROW(info.setFreshnessPeriod(time::milliseconds(-1)), std::invalid_argument);
BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 10000_ms);
}
BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo
} // namespace tests
@@ -98,7 +98,7 @@ private:
}
auto data = make_shared<Data>(name);
data->setFinalBlockId(data->getName()[-1]);
data->setFinalBlock(name[-1]);
return data;
}
};
@@ -128,7 +128,7 @@ CertificateBundleFetcherFixture<Bundle>::makeResponse(const Interest& interest)
certBundle->setName(bundleName);
certBundle->setFreshnessPeriod(100_s);
certBundle->setContent(certList);
certBundle->setFinalBlockId(name::Component::fromSegment(1));
certBundle->setFinalBlock(name::Component::fromSegment(1));
m_keyChain.sign(*certBundle, signingWithSha256());
+1 -1
View File
@@ -55,7 +55,7 @@ public:
auto data = make_shared<Data>(Name(baseName).appendSegment(segment));
data->setContent(buffer, sizeof(buffer));
if (isFinal) {
data->setFinalBlockId(data->getName()[-1]);
data->setFinalBlock(data->getName()[-1]);
}
return signData(data);