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