Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 14 | namespace nfd { |
| 15 | namespace ndnlp { |
| 16 | |
| 17 | /** \brief represents a partially received message |
| 18 | */ |
| 19 | class PartialMessage : noncopyable |
| 20 | { |
| 21 | public: |
| 22 | PartialMessage(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 23 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 24 | bool |
| 25 | add(uint16_t fragIndex, uint16_t fragCount, const Block& payload); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 26 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 27 | bool |
| 28 | isComplete() const; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 30 | /** \brief reassemble network layer packet |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 31 | * |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 32 | * isComplete() must be true before calling this method |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 33 | * |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 34 | * \exception ndn::Block::Error packet is malformated |
| 35 | * \return network layer packet |
| 36 | */ |
| 37 | Block |
| 38 | reassemble(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 40 | public: |
| 41 | EventId m_expiry; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 43 | private: |
| 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 | */ |
| 52 | class PartialMessageStore : noncopyable |
| 53 | { |
| 54 | public: |
| 55 | PartialMessageStore(Scheduler& scheduler, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 56 | const time::nanoseconds& idleDuration = time::milliseconds(100)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 57 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 58 | virtual |
| 59 | ~PartialMessageStore(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 61 | /** \brief receive a NdnlpData packet |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 62 | * |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 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); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 68 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 69 | /// fires when network layer packet is received |
| 70 | EventEmitter<Block> onReceive; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 72 | private: |
| 73 | void |
| 74 | scheduleCleanup(uint64_t messageIdentifier, shared_ptr<PartialMessage> partialMessage); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 75 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 76 | void |
| 77 | cleanup(uint64_t messageIdentifier); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 79 | private: |
| 80 | std::map<uint64_t, shared_ptr<PartialMessage> > m_partialMessages; |
| 81 | |
| 82 | Scheduler& m_scheduler; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 83 | time::nanoseconds m_idleDuration; |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace ndnlp |
| 87 | } // namespace nfd |
| 88 | |
| 89 | #endif // NFD_FACE_NDNLP_PARTIAL_MESSAGE_STORE_HPP |