Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | c6a8522 | 2017-01-03 16:54:34 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 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 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 34 | /*! \brief provides a publisher of Status Dataset or other segmented octet stream |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 35 | \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 36 | */ |
| 37 | template <class FaceBase> |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 38 | class SegmentPublisher : boost::noncopyable |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 39 | { |
| 40 | public: |
| 41 | SegmentPublisher(FaceBase& face, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 42 | ndn::KeyChain& keyChain, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 43 | const ndn::security::SigningInfo& signingInfo, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 44 | const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness()) |
| 45 | : m_face(face) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 46 | , m_keyChain(keyChain) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 47 | , m_signingInfo(signingInfo) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 48 | , m_freshnessPeriod(freshnessPeriod) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | virtual |
| 53 | ~SegmentPublisher() |
| 54 | { |
| 55 | } |
| 56 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 57 | /*! \brief Define the max segment size as half the max NDN packet size. |
| 58 | */ |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 59 | static size_t |
| 60 | getMaxSegmentSize() |
| 61 | { |
| 62 | static const size_t MAX_SEGMENT_SIZE = ndn::MAX_NDN_PACKET_SIZE >> 1; |
| 63 | return MAX_SEGMENT_SIZE; |
| 64 | } |
| 65 | |
| 66 | static constexpr ndn::time::milliseconds |
| 67 | getDefaultFreshness() |
| 68 | { |
| 69 | return ndn::time::milliseconds(1000); |
| 70 | } |
| 71 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 72 | /*! \brief Publish data under the provided prefix |
| 73 | * |
| 74 | * Processes whatever is provided from SegmentPublisher::generate, |
| 75 | * by breaking it into segments of MAX_SEGMENT_SIZE and sending each |
| 76 | * one individually. The last segment is distinguished by having the |
| 77 | * final block ID set to a timestamp. |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 78 | */ |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 79 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 80 | publish(const ndn::Name& prefix) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 81 | { |
| 82 | ndn::EncodingBuffer buffer; |
| 83 | generate(buffer); |
| 84 | |
| 85 | const uint8_t* rawBuffer = buffer.buf(); |
| 86 | const uint8_t* segmentBegin = rawBuffer; |
| 87 | const uint8_t* end = rawBuffer + buffer.size(); |
| 88 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 89 | ndn::Name segmentPrefix(prefix); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 90 | segmentPrefix.appendVersion(); |
| 91 | |
| 92 | uint64_t segmentNo = 0; |
| 93 | do { |
| 94 | const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize(); |
| 95 | if (segmentEnd > end) { |
| 96 | segmentEnd = end; |
| 97 | } |
| 98 | |
| 99 | ndn::Name segmentName(segmentPrefix); |
| 100 | segmentName.appendSegment(segmentNo); |
| 101 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 102 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 103 | data->setContent(segmentBegin, segmentEnd - segmentBegin); |
| 104 | data->setFreshnessPeriod(m_freshnessPeriod); |
| 105 | |
| 106 | segmentBegin = segmentEnd; |
| 107 | if (segmentBegin >= end) { |
| 108 | data->setFinalBlockId(segmentName[-1]); |
| 109 | } |
| 110 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 111 | publishSegment(data); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 112 | ++segmentNo; |
| 113 | } while (segmentBegin < end); |
| 114 | } |
| 115 | |
| 116 | protected: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 117 | /*! \brief In a derived class, write the octets into outBuffer. |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 118 | */ |
| 119 | virtual size_t |
| 120 | generate(ndn::EncodingBuffer& outBuffer) = 0; |
| 121 | |
| 122 | private: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 123 | /*! \brief Helper function to sign and put data on a Face. |
| 124 | */ |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 125 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 126 | publishSegment(std::shared_ptr<ndn::Data>& data) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 127 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 128 | m_keyChain.sign(*data, m_signingInfo); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 129 | m_face.put(*data); |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | FaceBase& m_face; |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 134 | ndn::KeyChain& m_keyChain; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 135 | const ndn::security::SigningInfo& m_signingInfo; |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 136 | const ndn::time::milliseconds m_freshnessPeriod; |
| 137 | }; |
| 138 | |
| 139 | } // namespace nlsr |
| 140 | |
| 141 | #endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP |