Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | 8919abe | 2015-01-28 09:34:54 -0700 | [diff] [blame] | 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. |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 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 NFD_CORE_SEGMENT_PUBLISHER_HPP |
| 27 | #define NFD_CORE_SEGMENT_PUBLISHER_HPP |
| 28 | |
| 29 | #include "common.hpp" |
| 30 | |
| 31 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 32 | #include <ndn-cxx/security/key-chain.hpp> |
| 33 | |
| 34 | namespace nfd { |
| 35 | |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 36 | /** \brief provides a publisher of Status Dataset or other segmented octet stream |
| 37 | * \sa http://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 38 | */ |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 39 | template <class FaceBase> |
| 40 | class SegmentPublisher : noncopyable |
| 41 | { |
| 42 | public: |
Steve DiBenedetto | 8919abe | 2015-01-28 09:34:54 -0700 | [diff] [blame] | 43 | SegmentPublisher(FaceBase& face, |
| 44 | const Name& prefix, |
| 45 | ndn::KeyChain& keyChain, |
| 46 | const time::milliseconds& freshnessPeriod = getDefaultFreshness()) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 47 | : m_face(face) |
| 48 | , m_prefix(prefix) |
| 49 | , m_keyChain(keyChain) |
Steve DiBenedetto | 8919abe | 2015-01-28 09:34:54 -0700 | [diff] [blame] | 50 | , m_freshnessPeriod(freshnessPeriod) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
| 54 | virtual |
| 55 | ~SegmentPublisher() |
| 56 | { |
| 57 | } |
| 58 | |
| 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 | |
Steve DiBenedetto | 8919abe | 2015-01-28 09:34:54 -0700 | [diff] [blame] | 66 | static constexpr time::milliseconds |
| 67 | getDefaultFreshness() |
| 68 | { |
| 69 | return time::milliseconds(1000); |
| 70 | } |
| 71 | |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 72 | void |
| 73 | publish() |
| 74 | { |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 75 | ndn::EncodingBuffer buffer; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 76 | generate(buffer); |
| 77 | |
| 78 | const uint8_t* rawBuffer = buffer.buf(); |
| 79 | const uint8_t* segmentBegin = rawBuffer; |
| 80 | const uint8_t* end = rawBuffer + buffer.size(); |
| 81 | |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 82 | Name segmentPrefix(m_prefix); |
| 83 | segmentPrefix.appendVersion(); |
| 84 | |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 85 | uint64_t segmentNo = 0; |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 86 | do { |
| 87 | const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize(); |
| 88 | if (segmentEnd > end) { |
| 89 | segmentEnd = end; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 90 | } |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 91 | |
| 92 | Name segmentName(segmentPrefix); |
| 93 | segmentName.appendSegment(segmentNo); |
| 94 | |
| 95 | shared_ptr<Data> data = make_shared<Data>(segmentName); |
| 96 | data->setContent(segmentBegin, segmentEnd - segmentBegin); |
Steve DiBenedetto | 8919abe | 2015-01-28 09:34:54 -0700 | [diff] [blame] | 97 | data->setFreshnessPeriod(m_freshnessPeriod); |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 98 | |
| 99 | segmentBegin = segmentEnd; |
| 100 | if (segmentBegin >= end) { |
| 101 | data->setFinalBlockId(segmentName[-1]); |
| 102 | } |
| 103 | |
| 104 | publishSegment(data); |
| 105 | ++segmentNo; |
| 106 | } while (segmentBegin < end); |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | protected: |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 110 | /** \brief In a derived class, write the octets into outBuffer. |
| 111 | */ |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 112 | virtual size_t |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 113 | generate(ndn::EncodingBuffer& outBuffer) = 0; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | void |
| 117 | publishSegment(shared_ptr<Data>& data) |
| 118 | { |
| 119 | m_keyChain.sign(*data); |
| 120 | m_face.put(*data); |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | FaceBase& m_face; |
| 125 | const Name m_prefix; |
| 126 | ndn::KeyChain& m_keyChain; |
Steve DiBenedetto | 8919abe | 2015-01-28 09:34:54 -0700 | [diff] [blame] | 127 | const time::milliseconds m_freshnessPeriod; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | } // namespace nfd |
| 131 | |
| 132 | #endif // NFD_CORE_SEGMENT_PUBLISHER_HPP |