From 55ddd210e88f249d85cd66a2a8626e663c3c38d7 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 3 Nov 2023 09:33:43 -0700 Subject: [PATCH] refactor: make mapping-provider defn and impl consistent --- ndn-svs/mapping-provider.cpp | 110 +++++++++++++++++------------------ ndn-svs/mapping-provider.hpp | 3 + 2 files changed, 58 insertions(+), 55 deletions(-) diff --git a/ndn-svs/mapping-provider.cpp b/ndn-svs/mapping-provider.cpp index 7d92d08..9b49d78 100644 --- a/ndn-svs/mapping-provider.cpp +++ b/ndn-svs/mapping-provider.cpp @@ -19,6 +19,61 @@ namespace ndn::svs { +MappingList::MappingList() = default; + +MappingList::MappingList(const NodeID& nid) + : nodeId(nid) +{} + +MappingList::MappingList(const Block& block) +{ + block.parse(); + + for (auto it = block.elements_begin(); it != block.elements_end(); it++) { + if (it->type() == ndn::tlv::Name) + { + nodeId = NodeID(*it); + continue; + } + + if (it->type() == tlv::MappingEntry) + { + it->parse(); + + SeqNo seqNo = ndn::encoding::readNonNegativeInteger(it->elements().at(0)); + Name name(it->elements().at(1)); + pairs.emplace_back(seqNo, name); + continue; + } + } +} + +Block +MappingList::encode() const +{ + ndn::encoding::EncodingBuffer enc; + size_t totalLength = 0; + + for (const auto& [seq, name] : pairs) + { + // Name + size_t entryLength = ndn::encoding::prependBlock(enc, name.wireEncode()); + + // SeqNo + entryLength += ndn::encoding::prependNonNegativeIntegerBlock(enc, tlv::SeqNo, seq); + + totalLength += enc.prependVarNumber(entryLength); + totalLength += enc.prependVarNumber(tlv::MappingEntry); + totalLength += entryLength; + } + + totalLength += ndn::encoding::prependBlock(enc, nodeId.wireEncode()); + + enc.prependVarNumber(totalLength); + enc.prependVarNumber(tlv::MappingData); + return enc.block(); +} + MappingProvider::MappingProvider(const Name& syncPrefix, const NodeID& id, ndn::Face& face, @@ -140,59 +195,4 @@ MappingProvider::parseMappingQueryDataName(const Name& name) return info; } -Block -MappingList::encode() const -{ - ndn::encoding::EncodingBuffer enc; - size_t totalLength = 0; - - for (const auto& [seq, name] : pairs) - { - // Name - size_t entryLength = ndn::encoding::prependBlock(enc, name.wireEncode()); - - // SeqNo - entryLength += ndn::encoding::prependNonNegativeIntegerBlock(enc, tlv::SeqNo, seq); - - totalLength += enc.prependVarNumber(entryLength); - totalLength += enc.prependVarNumber(tlv::MappingEntry); - totalLength += entryLength; - } - - totalLength += ndn::encoding::prependBlock(enc, nodeId.wireEncode()); - - enc.prependVarNumber(totalLength); - enc.prependVarNumber(tlv::MappingData); - return enc.block(); -} - -MappingList::MappingList() = default; - -MappingList::MappingList(const NodeID& nid) - : nodeId(nid) -{} - -MappingList::MappingList(const Block& block) -{ - block.parse(); - - for (auto it = block.elements_begin(); it != block.elements_end(); it++) { - if (it->type() == ndn::tlv::Name) - { - nodeId = NodeID(*it); - continue; - } - - if (it->type() == tlv::MappingEntry) - { - it->parse(); - - SeqNo seqNo = ndn::encoding::readNonNegativeInteger(it->elements().at(0)); - Name name(it->elements().at(1)); - pairs.emplace_back(seqNo, name); - continue; - } - } -} - } // namespace ndn::svs diff --git a/ndn-svs/mapping-provider.hpp b/ndn-svs/mapping-provider.hpp index 18daa34..0f363b2 100644 --- a/ndn-svs/mapping-provider.hpp +++ b/ndn-svs/mapping-provider.hpp @@ -24,6 +24,9 @@ namespace ndn::svs { +/** + * @brief TLV type for mapping list + */ class MappingList { public: