blob: 954b79a4c9c60eabf551cdf6e07f22ff6bd942a9 [file] [log] [blame]
Junxiao Shid6dcd2c2014-02-16 14:49:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP
8#define NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP
9
10#include "ndnlp-parse.hpp"
11#include "core/event-emitter.hpp"
12#include "core/scheduler.hpp"
13
14namespace nfd {
15namespace ndnlp {
16
17/** \brief represents a partially received message
18 */
19class PartialMessage : noncopyable
20{
21public:
22 PartialMessage();
23
24 bool
25 add(uint16_t fragIndex, uint16_t fragCount, const Block& payload);
26
27 bool
28 isComplete() const;
29
30 /** \brief reassemble network layer packet
31 *
32 * isComplete() must be true before calling this method
33 *
34 * \exception ndn::Block::Error packet is malformated
35 * \return network layer packet
36 */
37 Block
38 reassemble();
39
40public:
41 EventId m_expiry;
42
43private:
44 size_t m_fragCount;
45 size_t m_received;
46 std::vector<Block> m_payloads;
47 size_t m_totalLength;
48};
49
50/** \brief provides reassembly feature at receiver
51 */
52class PartialMessageStore : noncopyable
53{
54public:
55 PartialMessageStore(Scheduler& scheduler,
56 time::Duration idleDuration = time::milliseconds(100));
57
58 virtual
59 ~PartialMessageStore();
60
61 /** \brief receive a NdnlpData packet
62 *
63 * \exception ParseError NDNLP packet is malformated
64 * \exception ndn::Block::Error network layer packet is malformated
65 */
66 void
67 receiveNdnlpData(const Block& pkt);
68
69 /// fires when network layer packet is received
70 EventEmitter<Block> onReceive;
71
72private:
73 void
74 scheduleCleanup(uint64_t messageIdentifier, shared_ptr<PartialMessage> partialMessage);
75
76 void
77 cleanup(uint64_t messageIdentifier);
78
79private:
80 std::map<uint64_t, shared_ptr<PartialMessage> > m_partialMessages;
81
82 Scheduler& m_scheduler;
83 time::Duration m_idleDuration;
84};
85
86} // namespace ndnlp
87} // namespace nfd
88
89#endif // NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP