Skip to content

Commit

Permalink
refactor: make mapping-provider defn and impl consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Nov 3, 2023
1 parent fca0d7b commit 55ddd21
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
110 changes: 55 additions & 55 deletions ndn-svs/mapping-provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions ndn-svs/mapping-provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace ndn::svs {

/**
* @brief TLV type for mapping list
*/
class MappingList
{
public:
Expand Down

0 comments on commit 55ddd21

Please sign in to comment.