blob: 59c44b45b345bd5227b7d4d43fc53505f4e7cb9a [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonc6a85222017-01-03 16:54:34 -06003 * Copyright (c) 2014-2017, Regents of the University of California,
Jiewen Tana0497d82015-02-02 21:59:18 -08004 * 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
32namespace nlsr {
33
Nick G97e34942016-07-11 14:46:27 -050034/*! \brief provides a publisher of Status Dataset or other segmented octet stream
dmcoomes9f936662017-03-02 10:33:09 -060035 \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset
Jiewen Tana0497d82015-02-02 21:59:18 -080036 */
37template <class FaceBase>
dmcoomes9f936662017-03-02 10:33:09 -060038class SegmentPublisher : boost::noncopyable
Jiewen Tana0497d82015-02-02 21:59:18 -080039{
40public:
41 SegmentPublisher(FaceBase& face,
Jiewen Tana0497d82015-02-02 21:59:18 -080042 ndn::KeyChain& keyChain,
43 const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness())
44 : m_face(face)
Jiewen Tana0497d82015-02-02 21:59:18 -080045 , m_keyChain(keyChain)
46 , m_freshnessPeriod(freshnessPeriod)
47 {
48 }
49
50 virtual
51 ~SegmentPublisher()
52 {
53 }
54
Nick Gordond0a7df32017-05-30 16:44:34 -050055 /*! \brief Define the max segment size as half the max NDN packet size.
56 */
Jiewen Tana0497d82015-02-02 21:59:18 -080057 static size_t
58 getMaxSegmentSize()
59 {
60 static const size_t MAX_SEGMENT_SIZE = ndn::MAX_NDN_PACKET_SIZE >> 1;
61 return MAX_SEGMENT_SIZE;
62 }
63
64 static constexpr ndn::time::milliseconds
65 getDefaultFreshness()
66 {
67 return ndn::time::milliseconds(1000);
68 }
69
Nick Gordond0a7df32017-05-30 16:44:34 -050070 /*! \brief Publish data under the provided prefix
71 *
72 * Processes whatever is provided from SegmentPublisher::generate,
73 * by breaking it into segments of MAX_SEGMENT_SIZE and sending each
74 * one individually. The last segment is distinguished by having the
75 * final block ID set to a timestamp.
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050076 */
Jiewen Tana0497d82015-02-02 21:59:18 -080077 void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050078 publish(const ndn::Name& prefix,
79 const ndn::security::SigningInfo& signingInfo = ndn::security::KeyChain::DEFAULT_SIGNING_INFO)
Jiewen Tana0497d82015-02-02 21:59:18 -080080 {
81 ndn::EncodingBuffer buffer;
82 generate(buffer);
83
84 const uint8_t* rawBuffer = buffer.buf();
85 const uint8_t* segmentBegin = rawBuffer;
86 const uint8_t* end = rawBuffer + buffer.size();
87
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050088 ndn::Name segmentPrefix(prefix);
Jiewen Tana0497d82015-02-02 21:59:18 -080089 segmentPrefix.appendVersion();
90
91 uint64_t segmentNo = 0;
92 do {
93 const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
94 if (segmentEnd > end) {
95 segmentEnd = end;
96 }
97
98 ndn::Name segmentName(segmentPrefix);
99 segmentName.appendSegment(segmentNo);
100
dmcoomes9f936662017-03-02 10:33:09 -0600101 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName);
Jiewen Tana0497d82015-02-02 21:59:18 -0800102 data->setContent(segmentBegin, segmentEnd - segmentBegin);
103 data->setFreshnessPeriod(m_freshnessPeriod);
104
105 segmentBegin = segmentEnd;
106 if (segmentBegin >= end) {
107 data->setFinalBlockId(segmentName[-1]);
108 }
109
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500110 publishSegment(data, signingInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800111 ++segmentNo;
112 } while (segmentBegin < end);
113 }
114
115protected:
Nick G97e34942016-07-11 14:46:27 -0500116 /*! \brief In a derived class, write the octets into outBuffer.
Jiewen Tana0497d82015-02-02 21:59:18 -0800117 */
118 virtual size_t
119 generate(ndn::EncodingBuffer& outBuffer) = 0;
120
121private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500122 /*! \brief Helper function to sign and put data on a Face.
123 */
Jiewen Tana0497d82015-02-02 21:59:18 -0800124 void
dmcoomes9f936662017-03-02 10:33:09 -0600125 publishSegment(std::shared_ptr<ndn::Data>& data, const ndn::security::SigningInfo& signingInfo)
Jiewen Tana0497d82015-02-02 21:59:18 -0800126 {
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500127 m_keyChain.sign(*data, signingInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800128 m_face.put(*data);
129 }
130
131private:
132 FaceBase& m_face;
Jiewen Tana0497d82015-02-02 21:59:18 -0800133 ndn::KeyChain& m_keyChain;
134 const ndn::time::milliseconds m_freshnessPeriod;
135};
136
137} // namespace nlsr
138
139#endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP