blob: 64b2d009927d7d12ecb2cc79668ea80cd836f2be [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#ifndef NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP
26#define NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP
27
28#include "ndnlp-parse.hpp"
29#include "core/event-emitter.hpp"
30#include "core/scheduler.hpp"
31
32namespace nfd {
33namespace ndnlp {
34
35/** \brief represents a partially received message
36 */
37class PartialMessage : noncopyable
38{
39public:
40 PartialMessage();
Junxiao Shidf3b4382014-02-23 11:28:21 -070041
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070042 bool
43 add(uint16_t fragIndex, uint16_t fragCount, const Block& payload);
Junxiao Shidf3b4382014-02-23 11:28:21 -070044
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070045 bool
46 isComplete() const;
Junxiao Shidf3b4382014-02-23 11:28:21 -070047
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070048 /** \brief reassemble network layer packet
Junxiao Shidf3b4382014-02-23 11:28:21 -070049 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070050 * isComplete() must be true before calling this method
Junxiao Shidf3b4382014-02-23 11:28:21 -070051 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070052 * \exception ndn::Block::Error packet is malformated
53 * \return network layer packet
54 */
55 Block
56 reassemble();
Junxiao Shidf3b4382014-02-23 11:28:21 -070057
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070058public:
59 EventId m_expiry;
Junxiao Shidf3b4382014-02-23 11:28:21 -070060
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070061private:
62 size_t m_fragCount;
63 size_t m_received;
64 std::vector<Block> m_payloads;
65 size_t m_totalLength;
66};
67
68/** \brief provides reassembly feature at receiver
69 */
70class PartialMessageStore : noncopyable
71{
72public:
Junxiao Shi98e29f42014-03-31 10:27:26 -070073 explicit
74 PartialMessageStore(const time::nanoseconds& idleDuration = time::milliseconds(100));
Junxiao Shidf3b4382014-02-23 11:28:21 -070075
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070076 virtual
77 ~PartialMessageStore();
Junxiao Shidf3b4382014-02-23 11:28:21 -070078
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070079 /** \brief receive a NdnlpData packet
Junxiao Shidf3b4382014-02-23 11:28:21 -070080 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070081 * \exception ParseError NDNLP packet is malformated
82 * \exception ndn::Block::Error network layer packet is malformated
83 */
84 void
85 receiveNdnlpData(const Block& pkt);
Junxiao Shidf3b4382014-02-23 11:28:21 -070086
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070087 /// fires when network layer packet is received
88 EventEmitter<Block> onReceive;
Junxiao Shidf3b4382014-02-23 11:28:21 -070089
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070090private:
91 void
92 scheduleCleanup(uint64_t messageIdentifier, shared_ptr<PartialMessage> partialMessage);
Junxiao Shidf3b4382014-02-23 11:28:21 -070093
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070094 void
95 cleanup(uint64_t messageIdentifier);
Junxiao Shidf3b4382014-02-23 11:28:21 -070096
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070097private:
98 std::map<uint64_t, shared_ptr<PartialMessage> > m_partialMessages;
99
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700100 time::nanoseconds m_idleDuration;
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700101};
102
103} // namespace ndnlp
104} // namespace nfd
105
106#endif // NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP