diff --git a/daemon/face/ndnlp-parse.cpp b/daemon/face/ndnlp-parse.cpp new file mode 100644 index 00000000..1162f0e0 --- /dev/null +++ b/daemon/face/ndnlp-parse.cpp @@ -0,0 +1,68 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "ndnlp-parse.hpp" + +namespace nfd { +namespace ndnlp { + +void +NdnlpData::wireDecode(const Block& wire) +{ + if (wire.type() != tlv::NdnlpData) { + throw ParseError("top element is not NdnlpData"); + } + wire.parse(); + const Block::element_container& elements = wire.elements(); + if (elements.size() < 2) { + throw ParseError("NdnlpData element has incorrect number of children"); + } + + const Block& sequenceElement = elements.front(); + if (sequenceElement.type() != tlv::NdnlpSequence) { + throw ParseError("NdnlpSequence element is missing"); + } + if (sequenceElement.value_size() != sizeof(uint64_t)) { + throw ParseError("NdnlpSequence element has incorrect length"); + } + m_seq = be64toh(*reinterpret_cast(&*sequenceElement.value_begin())); + + const Block& payloadElement = elements.back(); + if (payloadElement.type() != tlv::NdnlpPayload) { + throw ParseError("NdnlpPayload element is missing"); + } + m_payload = payloadElement; + + if (elements.size() == 2) { // single wire packet + m_fragIndex = 0; + m_fragCount = 1; + return; + } + if (elements.size() != 4) { + throw ParseError("NdnlpData element has incorrect number of children"); + } + + const Block& fragIndexElement = elements.at(1); + if (fragIndexElement.type() != tlv::NdnlpFragIndex) { + throw ParseError("NdnlpFragIndex element is missing"); + } + if (fragIndexElement.value_size() != sizeof(uint16_t)) { + throw ParseError("NdnlpFragIndex element has incorrect length"); + } + m_fragIndex = be16toh(*reinterpret_cast(&*fragIndexElement.value_begin())); + + const Block& fragCountElement = elements.at(2); + if (fragCountElement.type() != tlv::NdnlpFragCount) { + throw ParseError("NdnlpFragCount element is missing"); + } + if (fragCountElement.value_size() != sizeof(uint16_t)) { + throw ParseError("NdnlpFragCount element has incorrect length"); + } + m_fragCount = be16toh(*reinterpret_cast(&*fragCountElement.value_begin())); +} + +} // namespace ndnlp +} // namespace nfd diff --git a/daemon/face/ndnlp-parse.hpp b/daemon/face/ndnlp-parse.hpp new file mode 100644 index 00000000..27905690 --- /dev/null +++ b/daemon/face/ndnlp-parse.hpp @@ -0,0 +1,52 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_FACE_NDNLP_PARSE_HPP +#define NFD_FACE_NDNLP_PARSE_HPP + +#include "common.hpp" +#include "ndnlp-tlv.hpp" + +namespace nfd { +namespace ndnlp { + +struct ParseError : public std::runtime_error +{ + ParseError(const std::string& what) + : std::runtime_error(what) + { + } +}; + +/** \brief represents a NdnlpData packet + * + * NdnlpData ::= NDNLP-DATA-TYPE TLV-LENGTH + * NdnlpSequence + * NdnlpFragIndex? + * NdnlpFragCount? + * NdnlpPayload + */ +class NdnlpData +{ +public: + /** \brief parse a NdnlpData packet + * + * \exception ParseError packet is malformated + */ + void + wireDecode(const Block& wire); + +public: + uint64_t m_seq; + uint16_t m_fragIndex; + uint16_t m_fragCount; + Block m_payload; +}; + +} // namespace ndnlp +} // namespace nfd + +#endif // NFD_FACE_NDNLP_PARSE_HPP diff --git a/daemon/face/ndnlp-partial-message-store.cpp b/daemon/face/ndnlp-partial-message-store.cpp new file mode 100644 index 00000000..22b493b1 --- /dev/null +++ b/daemon/face/ndnlp-partial-message-store.cpp @@ -0,0 +1,119 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "ndnlp-partial-message-store.hpp" + +namespace nfd { +namespace ndnlp { + +PartialMessage::PartialMessage() + : m_fragCount(0) + , m_received(0) + , m_totalLength(0) +{ +} + +bool +PartialMessage::add(uint16_t fragIndex, uint16_t fragCount, const Block& payload) +{ + if (m_received == 0) { // first packet + m_fragCount = fragCount; + m_payloads.resize(fragCount); + } + + if (m_fragCount != fragCount || fragIndex >= m_fragCount) { + return false; + } + + if (!m_payloads[fragIndex].empty()) { // duplicate + return false; + } + + m_payloads[fragIndex] = payload; + ++m_received; + m_totalLength += payload.value_size(); +} + +bool +PartialMessage::isComplete() const +{ + return m_received == m_fragCount; +} + +Block +PartialMessage::reassemble() +{ + BOOST_ASSERT(this->isComplete()); + + ndn::BufferPtr buffer = make_shared(m_totalLength); + uint8_t* buf = buffer->get(); + for (std::vector::const_iterator it = m_payloads.begin(); + it != m_payloads.end(); ++it) { + const Block& payload = *it; + memcpy(buf, payload.value(), payload.value_size()); + buf += payload.value_size(); + } + + return Block(buffer); +} + +PartialMessageStore::PartialMessageStore(Scheduler& scheduler, time::Duration idleDuration) + : m_scheduler(scheduler) + , m_idleDuration(idleDuration) +{ +} + +PartialMessageStore::~PartialMessageStore() +{ +} + +void +PartialMessageStore::receiveNdnlpData(const Block& pkt) +{ + NdnlpData parsed; + parsed.wireDecode(pkt); + if (parsed.m_fragCount == 1) { // single fragment + this->onReceive(parsed.m_payload.blockFromValue()); + return; + } + + uint64_t messageIdentifier = parsed.m_seq - parsed.m_fragIndex; + shared_ptr pm = m_partialMessages[messageIdentifier]; + if (!static_cast(pm)) { + m_partialMessages[messageIdentifier] = pm = make_shared(); + } + this->scheduleCleanup(messageIdentifier, pm); + + pm->add(parsed.m_fragIndex, parsed.m_fragCount, parsed.m_payload); + if (pm->isComplete()) { + this->onReceive(pm->reassemble()); + this->cleanup(messageIdentifier); + } +} + +void +PartialMessageStore::scheduleCleanup(uint64_t messageIdentifier, + shared_ptr partialMessage) +{ + partialMessage->m_expiry = m_scheduler.scheduleEvent(m_idleDuration, + bind(&PartialMessageStore::cleanup, this, messageIdentifier)); +} + +void +PartialMessageStore::cleanup(uint64_t messageIdentifier) +{ + std::map >::iterator it = + m_partialMessages.find(messageIdentifier); + if (it == m_partialMessages.end()) { + return; + } + + m_scheduler.cancelEvent(it->second->m_expiry); + m_partialMessages.erase(it); +} + +} // namespace ndnlp +} // namespace nfd diff --git a/daemon/face/ndnlp-partial-message-store.hpp b/daemon/face/ndnlp-partial-message-store.hpp new file mode 100644 index 00000000..954b79a4 --- /dev/null +++ b/daemon/face/ndnlp-partial-message-store.hpp @@ -0,0 +1,89 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP +#define NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP + +#include "ndnlp-parse.hpp" +#include "core/event-emitter.hpp" +#include "core/scheduler.hpp" + +namespace nfd { +namespace ndnlp { + +/** \brief represents a partially received message + */ +class PartialMessage : noncopyable +{ +public: + PartialMessage(); + + bool + add(uint16_t fragIndex, uint16_t fragCount, const Block& payload); + + bool + isComplete() const; + + /** \brief reassemble network layer packet + * + * isComplete() must be true before calling this method + * + * \exception ndn::Block::Error packet is malformated + * \return network layer packet + */ + Block + reassemble(); + +public: + EventId m_expiry; + +private: + size_t m_fragCount; + size_t m_received; + std::vector m_payloads; + size_t m_totalLength; +}; + +/** \brief provides reassembly feature at receiver + */ +class PartialMessageStore : noncopyable +{ +public: + PartialMessageStore(Scheduler& scheduler, + time::Duration idleDuration = time::milliseconds(100)); + + virtual + ~PartialMessageStore(); + + /** \brief receive a NdnlpData packet + * + * \exception ParseError NDNLP packet is malformated + * \exception ndn::Block::Error network layer packet is malformated + */ + void + receiveNdnlpData(const Block& pkt); + + /// fires when network layer packet is received + EventEmitter onReceive; + +private: + void + scheduleCleanup(uint64_t messageIdentifier, shared_ptr partialMessage); + + void + cleanup(uint64_t messageIdentifier); + +private: + std::map > m_partialMessages; + + Scheduler& m_scheduler; + time::Duration m_idleDuration; +}; + +} // namespace ndnlp +} // namespace nfd + +#endif // NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP diff --git a/tests/face/ndnlp.cpp b/tests/face/ndnlp.cpp index f3b8ab60..1160cf32 100644 --- a/tests/face/ndnlp.cpp +++ b/tests/face/ndnlp.cpp @@ -6,6 +6,7 @@ #include "face/ndnlp-sequence-generator.hpp" #include "face/ndnlp-slicer.hpp" +#include "face/ndnlp-partial-message-store.hpp" #include @@ -76,7 +77,7 @@ BOOST_AUTO_TEST_CASE(Slice1) } // slice a Block to four NDNLP packets -BOOST_AUTO_TEST_CASE(SliceData4) +BOOST_AUTO_TEST_CASE(Slice4) { uint8_t blockValue[5050]; memset(blockValue, 0xcc, sizeof(blockValue)); @@ -132,6 +133,85 @@ BOOST_AUTO_TEST_CASE(SliceData4) BOOST_CHECK_EQUAL(totalPayloadSize, block.size()); } +class ReassembleFixture +{ +protected: + ReassembleFixture() + : m_slicer(1500) + , m_scheduler(m_io) + , m_partialMessageStore(m_scheduler) + { + m_partialMessageStore.onReceive += bind(&std::vector::push_back, + &m_received, _1); + } + + Block + makeBlock(size_t valueLength) + { + uint8_t blockValue[valueLength]; + memset(blockValue, 0xcc, sizeof(blockValue)); + return ndn::dataBlock(0x01, blockValue, sizeof(blockValue)); + } + +protected: + boost::asio::io_service m_io; + Scheduler m_scheduler; + + ndnlp::Slicer m_slicer; + ndnlp::PartialMessageStore m_partialMessageStore; + + // received network layer packets + std::vector m_received; +}; + +// reassemble one NDNLP packets into one Block +BOOST_FIXTURE_TEST_CASE(Reassemble1, ReassembleFixture) +{ + Block block = makeBlock(60); + ndnlp::PacketArray pa = m_slicer.slice(block); + BOOST_REQUIRE_EQUAL(pa->size(), 1); + + BOOST_CHECK_EQUAL(m_received.size(), 0); + m_partialMessageStore.receiveNdnlpData(pa->at(0)); + + BOOST_REQUIRE_EQUAL(m_received.size(), 1); + BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(), + block.begin(), block.end()); +} + +// reassemble four and two NDNLP packets into two Blocks +BOOST_FIXTURE_TEST_CASE(Reassemble4and2, ReassembleFixture) +{ + Block block = makeBlock(5050); + ndnlp::PacketArray pa = m_slicer.slice(block); + BOOST_REQUIRE_EQUAL(pa->size(), 4); + + Block block2 = makeBlock(2000); + ndnlp::PacketArray pa2 = m_slicer.slice(block2); + BOOST_REQUIRE_EQUAL(pa2->size(), 2); + + BOOST_CHECK_EQUAL(m_received.size(), 0); + m_partialMessageStore.receiveNdnlpData(pa->at(0)); + BOOST_CHECK_EQUAL(m_received.size(), 0); + m_partialMessageStore.receiveNdnlpData(pa->at(1)); + BOOST_CHECK_EQUAL(m_received.size(), 0); + m_partialMessageStore.receiveNdnlpData(pa2->at(1)); + BOOST_CHECK_EQUAL(m_received.size(), 0); + m_partialMessageStore.receiveNdnlpData(pa->at(1)); + BOOST_CHECK_EQUAL(m_received.size(), 0); + m_partialMessageStore.receiveNdnlpData(pa2->at(0)); + BOOST_CHECK_EQUAL(m_received.size(), 1); + m_partialMessageStore.receiveNdnlpData(pa->at(3)); + BOOST_CHECK_EQUAL(m_received.size(), 1); + m_partialMessageStore.receiveNdnlpData(pa->at(2)); + + BOOST_REQUIRE_EQUAL(m_received.size(), 2); + BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(1).begin(), m_received.at(1).end(), + block.begin(), block.end()); + BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(), + block2.begin(), block2.end()); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace nfd