blob: 6fbf0f7391f5b2eb2c3f023b826e2757c91861e8 [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
29#include "ndnlp-parse.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 Shidf3b4382014-02-23 11:28:21 -070059 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070060 * isComplete() must be true before calling this method
Junxiao Shidf3b4382014-02-23 11:28:21 -070061 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070062 * \exception ndn::Block::Error packet is malformated
63 * \return network layer packet
64 */
65 Block
66 reassemble();
Junxiao Shidf3b4382014-02-23 11:28:21 -070067
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070068public:
Junxiao Shia37405d2015-01-26 10:50:58 -070069 scheduler::ScopedEventId expiry;
Junxiao Shidf3b4382014-02-23 11:28:21 -070070
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070071private:
72 size_t m_fragCount;
73 size_t m_received;
74 std::vector<Block> m_payloads;
75 size_t m_totalLength;
76};
77
78/** \brief provides reassembly feature at receiver
79 */
80class PartialMessageStore : noncopyable
81{
82public:
Junxiao Shi98e29f42014-03-31 10:27:26 -070083 explicit
84 PartialMessageStore(const time::nanoseconds& idleDuration = time::milliseconds(100));
Junxiao Shidf3b4382014-02-23 11:28:21 -070085
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070086 virtual
87 ~PartialMessageStore();
Junxiao Shidf3b4382014-02-23 11:28:21 -070088
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070089 /** \brief receive a NdnlpData packet
Junxiao Shidf3b4382014-02-23 11:28:21 -070090 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070091 * \exception ParseError NDNLP packet is malformated
92 * \exception ndn::Block::Error network layer packet is malformated
93 */
94 void
95 receiveNdnlpData(const Block& 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