Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * 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. |
| 10 | * |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP |
| 27 | #define NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP |
| 28 | |
| 29 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 30 | #include <ndn-cxx/security/key-chain.hpp> |
| 31 | |
| 32 | namespace nlsr { |
| 33 | |
| 34 | /** \brief provides a publisher of Status Dataset or other segmented octet stream |
| 35 | * \sa http://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 36 | */ |
| 37 | template <class FaceBase> |
| 38 | class SegmentPublisher : ndn::noncopyable |
| 39 | { |
| 40 | public: |
| 41 | SegmentPublisher(FaceBase& face, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 42 | ndn::KeyChain& keyChain, |
| 43 | const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness()) |
| 44 | : m_face(face) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 45 | , m_keyChain(keyChain) |
| 46 | , m_freshnessPeriod(freshnessPeriod) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | virtual |
| 51 | ~SegmentPublisher() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | static size_t |
| 56 | getMaxSegmentSize() |
| 57 | { |
| 58 | static const size_t MAX_SEGMENT_SIZE = ndn::MAX_NDN_PACKET_SIZE >> 1; |
| 59 | return MAX_SEGMENT_SIZE; |
| 60 | } |
| 61 | |
| 62 | static constexpr ndn::time::milliseconds |
| 63 | getDefaultFreshness() |
| 64 | { |
| 65 | return ndn::time::milliseconds(1000); |
| 66 | } |
| 67 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 68 | /** \brief Publish data under provided prefix |
| 69 | */ |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 70 | void |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 71 | publish(const ndn::Name& prefix, |
| 72 | const ndn::security::SigningInfo& signingInfo = ndn::security::KeyChain::DEFAULT_SIGNING_INFO) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 73 | { |
| 74 | ndn::EncodingBuffer buffer; |
| 75 | generate(buffer); |
| 76 | |
| 77 | const uint8_t* rawBuffer = buffer.buf(); |
| 78 | const uint8_t* segmentBegin = rawBuffer; |
| 79 | const uint8_t* end = rawBuffer + buffer.size(); |
| 80 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 81 | ndn::Name segmentPrefix(prefix); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 82 | segmentPrefix.appendVersion(); |
| 83 | |
| 84 | uint64_t segmentNo = 0; |
| 85 | do { |
| 86 | const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize(); |
| 87 | if (segmentEnd > end) { |
| 88 | segmentEnd = end; |
| 89 | } |
| 90 | |
| 91 | ndn::Name segmentName(segmentPrefix); |
| 92 | segmentName.appendSegment(segmentNo); |
| 93 | |
| 94 | ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>(segmentName); |
| 95 | data->setContent(segmentBegin, segmentEnd - segmentBegin); |
| 96 | data->setFreshnessPeriod(m_freshnessPeriod); |
| 97 | |
| 98 | segmentBegin = segmentEnd; |
| 99 | if (segmentBegin >= end) { |
| 100 | data->setFinalBlockId(segmentName[-1]); |
| 101 | } |
| 102 | |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 103 | publishSegment(data, signingInfo); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 104 | ++segmentNo; |
| 105 | } while (segmentBegin < end); |
| 106 | } |
| 107 | |
| 108 | protected: |
| 109 | /** \brief In a derived class, write the octets into outBuffer. |
| 110 | */ |
| 111 | virtual size_t |
| 112 | generate(ndn::EncodingBuffer& outBuffer) = 0; |
| 113 | |
| 114 | private: |
| 115 | void |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 116 | publishSegment(ndn::shared_ptr<ndn::Data>& data, const ndn::security::SigningInfo& signingInfo) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 117 | { |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 118 | m_keyChain.sign(*data, signingInfo); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 119 | m_face.put(*data); |
| 120 | } |
| 121 | |
| 122 | private: |
| 123 | FaceBase& m_face; |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 124 | ndn::KeyChain& m_keyChain; |
| 125 | const ndn::time::milliseconds m_freshnessPeriod; |
| 126 | }; |
| 127 | |
| 128 | } // namespace nlsr |
| 129 | |
| 130 | #endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP |