blob: 25afcd86d5857f0b6d7b3033ef930381b3e479d0 [file] [log] [blame]
Junxiao Shid6dcd2c2014-02-16 14:49:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shic099ddb2014-12-25 20:53:20 -070024 */
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP
27#define NFD_DAEMON_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070028
Junxiao Shi083f7782015-02-21 12:06:26 -070029#include "ndnlp-data.hpp"
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070030#include "core/scheduler.hpp"
31
32namespace nfd {
33namespace ndnlp {
34
35/** \brief represents a partially received message
36 */
Junxiao Shia37405d2015-01-26 10:50:58 -070037class PartialMessage
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070038{
39public:
40 PartialMessage();
Junxiao Shidf3b4382014-02-23 11:28:21 -070041
Junxiao Shia37405d2015-01-26 10:50:58 -070042 PartialMessage(const PartialMessage&) = delete;
43
44 PartialMessage&
45 operator=(const PartialMessage&) = delete;
46
47 PartialMessage(PartialMessage&&) = default;
48
49 PartialMessage&
50 operator=(PartialMessage&&) = default;
51
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070052 bool
53 add(uint16_t fragIndex, uint16_t fragCount, const Block& payload);
Junxiao Shidf3b4382014-02-23 11:28:21 -070054
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070055 bool
56 isComplete() const;
Junxiao Shidf3b4382014-02-23 11:28:21 -070057
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070058 /** \brief reassemble network layer packet
Junxiao Shi083f7782015-02-21 12:06:26 -070059 * \pre isComplete() == true
60 * \return whether success, network layer packet
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070061 */
Junxiao Shi083f7782015-02-21 12:06:26 -070062 std::tuple<bool, Block>
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070063 reassemble();
Junxiao Shidf3b4382014-02-23 11:28:21 -070064
Junxiao Shi083f7782015-02-21 12:06:26 -070065 /** \brief reassemble network layer packet from a single fragment
66 * \pre fragment.fragCount == 1
67 * \return whether success, network layer packet
68 */
69 static std::tuple<bool, Block>
70 reassembleSingle(const NdnlpData& fragment);
71
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070072public:
Junxiao Shia37405d2015-01-26 10:50:58 -070073 scheduler::ScopedEventId expiry;
Junxiao Shidf3b4382014-02-23 11:28:21 -070074
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070075private:
76 size_t m_fragCount;
77 size_t m_received;
78 std::vector<Block> m_payloads;
79 size_t m_totalLength;
80};
81
82/** \brief provides reassembly feature at receiver
83 */
84class PartialMessageStore : noncopyable
85{
86public:
Junxiao Shi98e29f42014-03-31 10:27:26 -070087 explicit
88 PartialMessageStore(const time::nanoseconds& idleDuration = time::milliseconds(100));
Junxiao Shidf3b4382014-02-23 11:28:21 -070089
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070090 /** \brief receive a NdnlpData packet
Junxiao Shidf3b4382014-02-23 11:28:21 -070091 *
Junxiao Shi083f7782015-02-21 12:06:26 -070092 * Reassembly errors will be ignored.
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070093 */
94 void
Junxiao Shi083f7782015-02-21 12:06:26 -070095 receive(const NdnlpData& pkt);
Junxiao Shidf3b4382014-02-23 11:28:21 -070096
Junxiao Shia37405d2015-01-26 10:50:58 -070097 /** \brief fires when network layer packet is received
98 */
Junxiao Shic099ddb2014-12-25 20:53:20 -070099 signal::Signal<PartialMessageStore, Block> onReceive;
Junxiao Shidf3b4382014-02-23 11:28:21 -0700100
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700101private:
102 void
Junxiao Shia37405d2015-01-26 10:50:58 -0700103 scheduleCleanup(uint64_t messageIdentifier, PartialMessage& partialMessage);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700104
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700105 void
106 cleanup(uint64_t messageIdentifier);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700107
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700108private:
Junxiao Shia37405d2015-01-26 10:50:58 -0700109 std::unordered_map<uint64_t, PartialMessage> m_partialMessages;
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700110
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700111 time::nanoseconds m_idleDuration;
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700112};
113
114} // namespace ndnlp
115} // namespace nfd
116
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700117#endif // NFD_DAEMON_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP