blob: e5de5b1033311a43ec6ccf526b0d3dbf5864185e [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();
Junxiao Shidf3b4382014-02-23 11:28:21 -070023
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070024 bool
25 add(uint16_t fragIndex, uint16_t fragCount, const Block& payload);
Junxiao Shidf3b4382014-02-23 11:28:21 -070026
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070027 bool
28 isComplete() const;
Junxiao Shidf3b4382014-02-23 11:28:21 -070029
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070030 /** \brief reassemble network layer packet
Junxiao Shidf3b4382014-02-23 11:28:21 -070031 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070032 * isComplete() must be true before calling this method
Junxiao Shidf3b4382014-02-23 11:28:21 -070033 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070034 * \exception ndn::Block::Error packet is malformated
35 * \return network layer packet
36 */
37 Block
38 reassemble();
Junxiao Shidf3b4382014-02-23 11:28:21 -070039
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070040public:
41 EventId m_expiry;
Junxiao Shidf3b4382014-02-23 11:28:21 -070042
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070043private:
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));
Junxiao Shidf3b4382014-02-23 11:28:21 -070057
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070058 virtual
59 ~PartialMessageStore();
Junxiao Shidf3b4382014-02-23 11:28:21 -070060
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070061 /** \brief receive a NdnlpData packet
Junxiao Shidf3b4382014-02-23 11:28:21 -070062 *
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070063 * \exception ParseError NDNLP packet is malformated
64 * \exception ndn::Block::Error network layer packet is malformated
65 */
66 void
67 receiveNdnlpData(const Block& pkt);
Junxiao Shidf3b4382014-02-23 11:28:21 -070068
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070069 /// fires when network layer packet is received
70 EventEmitter<Block> onReceive;
Junxiao Shidf3b4382014-02-23 11:28:21 -070071
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070072private:
73 void
74 scheduleCleanup(uint64_t messageIdentifier, shared_ptr<PartialMessage> partialMessage);
Junxiao Shidf3b4382014-02-23 11:28:21 -070075
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070076 void
77 cleanup(uint64_t messageIdentifier);
Junxiao Shidf3b4382014-02-23 11:28:21 -070078
Junxiao Shid6dcd2c2014-02-16 14:49:54 -070079private:
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