blob: 43c2efbd6960bbf04a953b54cc15afb2fc671072 [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
35 \sa http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
Jiewen Tana0497d82015-02-02 21:59:18 -080036 */
37template <class FaceBase>
38class SegmentPublisher : ndn::noncopyable
39{
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
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
Nick G97e34942016-07-11 14:46:27 -050068 /*! \brief Publish data under provided prefix
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050069 */
Jiewen Tana0497d82015-02-02 21:59:18 -080070 void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050071 publish(const ndn::Name& prefix,
72 const ndn::security::SigningInfo& signingInfo = ndn::security::KeyChain::DEFAULT_SIGNING_INFO)
Jiewen Tana0497d82015-02-02 21:59:18 -080073 {
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 Lehmand6bb3fa2015-04-24 14:21:39 -050081 ndn::Name segmentPrefix(prefix);
Jiewen Tana0497d82015-02-02 21:59:18 -080082 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 Chowdhuryaa3b0852015-08-06 13:08:56 -0500103 publishSegment(data, signingInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800104 ++segmentNo;
105 } while (segmentBegin < end);
106 }
107
108protected:
Nick G97e34942016-07-11 14:46:27 -0500109 /*! \brief In a derived class, write the octets into outBuffer.
Jiewen Tana0497d82015-02-02 21:59:18 -0800110 */
111 virtual size_t
112 generate(ndn::EncodingBuffer& outBuffer) = 0;
113
114private:
115 void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500116 publishSegment(ndn::shared_ptr<ndn::Data>& data, const ndn::security::SigningInfo& signingInfo)
Jiewen Tana0497d82015-02-02 21:59:18 -0800117 {
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500118 m_keyChain.sign(*data, signingInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800119 m_face.put(*data);
120 }
121
122private:
123 FaceBase& m_face;
Jiewen Tana0497d82015-02-02 21:59:18 -0800124 ndn::KeyChain& m_keyChain;
125 const ndn::time::milliseconds m_freshnessPeriod;
126};
127
128} // namespace nlsr
129
130#endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP