blob: 54a553745d594fa4e478808eb67d98e5e5729506 [file] [log] [blame]
Junxiao Shid6dcd2c2014-02-16 14:49:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Shid6dcd2c2014-02-16 14:49:54 -070024
25#include "ndnlp-partial-message-store.hpp"
26
27namespace nfd {
28namespace ndnlp {
29
30PartialMessage::PartialMessage()
31 : m_fragCount(0)
32 , m_received(0)
33 , m_totalLength(0)
34{
35}
36
37bool
38PartialMessage::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 Shidf3b4382014-02-23 11:28:21 -070044
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070045 if (m_fragCount != fragCount || fragIndex >= m_fragCount) {
46 return false;
47 }
Junxiao Shidf3b4382014-02-23 11:28:21 -070048
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070049 if (!m_payloads[fragIndex].empty()) { // duplicate
50 return false;
51 }
Junxiao Shidf3b4382014-02-23 11:28:21 -070052
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070053 m_payloads[fragIndex] = payload;
54 ++m_received;
55 m_totalLength += payload.value_size();
Junxiao Shic0d05912014-02-17 19:06:21 -070056 return true;
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070057}
58
59bool
60PartialMessage::isComplete() const
61{
62 return m_received == m_fragCount;
63}
64
65Block
66PartialMessage::reassemble()
67{
68 BOOST_ASSERT(this->isComplete());
Junxiao Shidf3b4382014-02-23 11:28:21 -070069
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070070 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 Shidf3b4382014-02-23 11:28:21 -070078
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070079 return Block(buffer);
80}
81
Junxiao Shi98e29f42014-03-31 10:27:26 -070082PartialMessageStore::PartialMessageStore(const time::nanoseconds& idleDuration)
83 : m_idleDuration(idleDuration)
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070084{
85}
86
87PartialMessageStore::~PartialMessageStore()
88{
89}
90
91void
92PartialMessageStore::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 Shidf3b4382014-02-23 11:28:21 -0700100
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700101 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 Shidf3b4382014-02-23 11:28:21 -0700107
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700108 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
115void
116PartialMessageStore::scheduleCleanup(uint64_t messageIdentifier,
117 shared_ptr<PartialMessage> partialMessage)
118{
Junxiao Shi98e29f42014-03-31 10:27:26 -0700119 partialMessage->m_expiry = scheduler::schedule(m_idleDuration,
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700120 bind(&PartialMessageStore::cleanup, this, messageIdentifier));
121}
122
123void
124PartialMessageStore::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 Shidf3b4382014-02-23 11:28:21 -0700131
Junxiao Shi98e29f42014-03-31 10:27:26 -0700132 scheduler::cancel(it->second->m_expiry);
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700133 m_partialMessages.erase(it);
134}
135
136} // namespace ndnlp
137} // namespace nfd