Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "ndnlp-partial-message-store.hpp" |
| 8 | |
| 9 | namespace nfd { |
| 10 | namespace ndnlp { |
| 11 | |
| 12 | PartialMessage::PartialMessage() |
| 13 | : m_fragCount(0) |
| 14 | , m_received(0) |
| 15 | , m_totalLength(0) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | bool |
| 20 | PartialMessage::add(uint16_t fragIndex, uint16_t fragCount, const Block& payload) |
| 21 | { |
| 22 | if (m_received == 0) { // first packet |
| 23 | m_fragCount = fragCount; |
| 24 | m_payloads.resize(fragCount); |
| 25 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 26 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 27 | if (m_fragCount != fragCount || fragIndex >= m_fragCount) { |
| 28 | return false; |
| 29 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 31 | if (!m_payloads[fragIndex].empty()) { // duplicate |
| 32 | return false; |
| 33 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 35 | m_payloads[fragIndex] = payload; |
| 36 | ++m_received; |
| 37 | m_totalLength += payload.value_size(); |
Junxiao Shi | c0d0591 | 2014-02-17 19:06:21 -0700 | [diff] [blame] | 38 | return true; |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool |
| 42 | PartialMessage::isComplete() const |
| 43 | { |
| 44 | return m_received == m_fragCount; |
| 45 | } |
| 46 | |
| 47 | Block |
| 48 | PartialMessage::reassemble() |
| 49 | { |
| 50 | BOOST_ASSERT(this->isComplete()); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 52 | ndn::BufferPtr buffer = make_shared<ndn::Buffer>(m_totalLength); |
| 53 | uint8_t* buf = buffer->get(); |
| 54 | for (std::vector<Block>::const_iterator it = m_payloads.begin(); |
| 55 | it != m_payloads.end(); ++it) { |
| 56 | const Block& payload = *it; |
| 57 | memcpy(buf, payload.value(), payload.value_size()); |
| 58 | buf += payload.value_size(); |
| 59 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 61 | return Block(buffer); |
| 62 | } |
| 63 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 64 | PartialMessageStore::PartialMessageStore(const time::nanoseconds& idleDuration) |
| 65 | : m_idleDuration(idleDuration) |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
| 69 | PartialMessageStore::~PartialMessageStore() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | PartialMessageStore::receiveNdnlpData(const Block& pkt) |
| 75 | { |
| 76 | NdnlpData parsed; |
| 77 | parsed.wireDecode(pkt); |
| 78 | if (parsed.m_fragCount == 1) { // single fragment |
| 79 | this->onReceive(parsed.m_payload.blockFromValue()); |
| 80 | return; |
| 81 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 83 | uint64_t messageIdentifier = parsed.m_seq - parsed.m_fragIndex; |
| 84 | shared_ptr<PartialMessage> pm = m_partialMessages[messageIdentifier]; |
| 85 | if (!static_cast<bool>(pm)) { |
| 86 | m_partialMessages[messageIdentifier] = pm = make_shared<PartialMessage>(); |
| 87 | } |
| 88 | this->scheduleCleanup(messageIdentifier, pm); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 90 | pm->add(parsed.m_fragIndex, parsed.m_fragCount, parsed.m_payload); |
| 91 | if (pm->isComplete()) { |
| 92 | this->onReceive(pm->reassemble()); |
| 93 | this->cleanup(messageIdentifier); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | PartialMessageStore::scheduleCleanup(uint64_t messageIdentifier, |
| 99 | shared_ptr<PartialMessage> partialMessage) |
| 100 | { |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 101 | partialMessage->m_expiry = scheduler::schedule(m_idleDuration, |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 102 | bind(&PartialMessageStore::cleanup, this, messageIdentifier)); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | PartialMessageStore::cleanup(uint64_t messageIdentifier) |
| 107 | { |
| 108 | std::map<uint64_t, shared_ptr<PartialMessage> >::iterator it = |
| 109 | m_partialMessages.find(messageIdentifier); |
| 110 | if (it == m_partialMessages.end()) { |
| 111 | return; |
| 112 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 114 | scheduler::cancel(it->second->m_expiry); |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 115 | m_partialMessages.erase(it); |
| 116 | } |
| 117 | |
| 118 | } // namespace ndnlp |
| 119 | } // namespace nfd |