Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 24 | |
| 25 | #include "ndnlp-partial-message-store.hpp" |
| 26 | |
| 27 | namespace nfd { |
| 28 | namespace ndnlp { |
| 29 | |
| 30 | PartialMessage::PartialMessage() |
| 31 | : m_fragCount(0) |
| 32 | , m_received(0) |
| 33 | , m_totalLength(0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | bool |
| 38 | PartialMessage::add(uint16_t fragIndex, uint16_t fragCount, const Block& payload) |
| 39 | { |
| 40 | if (m_received == 0) { // first packet |
| 41 | m_fragCount = fragCount; |
| 42 | m_payloads.resize(fragCount); |
| 43 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 44 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 45 | if (m_fragCount != fragCount || fragIndex >= m_fragCount) { |
| 46 | return false; |
| 47 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 49 | if (!m_payloads[fragIndex].empty()) { // duplicate |
| 50 | return false; |
| 51 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 53 | m_payloads[fragIndex] = payload; |
| 54 | ++m_received; |
| 55 | m_totalLength += payload.value_size(); |
Junxiao Shi | c0d0591 | 2014-02-17 19:06:21 -0700 | [diff] [blame] | 56 | return true; |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool |
| 60 | PartialMessage::isComplete() const |
| 61 | { |
| 62 | return m_received == m_fragCount; |
| 63 | } |
| 64 | |
| 65 | Block |
| 66 | PartialMessage::reassemble() |
| 67 | { |
| 68 | BOOST_ASSERT(this->isComplete()); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 70 | ndn::BufferPtr buffer = make_shared<ndn::Buffer>(m_totalLength); |
| 71 | uint8_t* buf = buffer->get(); |
| 72 | for (std::vector<Block>::const_iterator it = m_payloads.begin(); |
| 73 | it != m_payloads.end(); ++it) { |
| 74 | const Block& payload = *it; |
| 75 | memcpy(buf, payload.value(), payload.value_size()); |
| 76 | buf += payload.value_size(); |
| 77 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 79 | return Block(buffer); |
| 80 | } |
| 81 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 82 | PartialMessageStore::PartialMessageStore(const time::nanoseconds& idleDuration) |
| 83 | : m_idleDuration(idleDuration) |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
| 87 | PartialMessageStore::~PartialMessageStore() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | PartialMessageStore::receiveNdnlpData(const Block& pkt) |
| 93 | { |
| 94 | NdnlpData parsed; |
| 95 | parsed.wireDecode(pkt); |
| 96 | if (parsed.m_fragCount == 1) { // single fragment |
| 97 | this->onReceive(parsed.m_payload.blockFromValue()); |
| 98 | return; |
| 99 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 101 | uint64_t messageIdentifier = parsed.m_seq - parsed.m_fragIndex; |
| 102 | shared_ptr<PartialMessage> pm = m_partialMessages[messageIdentifier]; |
| 103 | if (!static_cast<bool>(pm)) { |
| 104 | m_partialMessages[messageIdentifier] = pm = make_shared<PartialMessage>(); |
| 105 | } |
| 106 | this->scheduleCleanup(messageIdentifier, pm); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 108 | pm->add(parsed.m_fragIndex, parsed.m_fragCount, parsed.m_payload); |
| 109 | if (pm->isComplete()) { |
| 110 | this->onReceive(pm->reassemble()); |
| 111 | this->cleanup(messageIdentifier); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | PartialMessageStore::scheduleCleanup(uint64_t messageIdentifier, |
| 117 | shared_ptr<PartialMessage> partialMessage) |
| 118 | { |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 119 | partialMessage->m_expiry = scheduler::schedule(m_idleDuration, |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 120 | bind(&PartialMessageStore::cleanup, this, messageIdentifier)); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | PartialMessageStore::cleanup(uint64_t messageIdentifier) |
| 125 | { |
| 126 | std::map<uint64_t, shared_ptr<PartialMessage> >::iterator it = |
| 127 | m_partialMessages.find(messageIdentifier); |
| 128 | if (it == m_partialMessages.end()) { |
| 129 | return; |
| 130 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 131 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 132 | scheduler::cancel(it->second->m_expiry); |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 133 | m_partialMessages.erase(it); |
| 134 | } |
| 135 | |
| 136 | } // namespace ndnlp |
| 137 | } // namespace nfd |