blob: d80c90eb402e1b924c9c5e41d98ddaa135201607 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawanded65786a2018-03-30 01:17:58 -05003 * Copyright (c) 2014-2018, 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,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050043 const ndn::security::SigningInfo& signingInfo,
Jiewen Tana0497d82015-02-02 21:59:18 -080044 const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness())
45 : m_face(face)
Jiewen Tana0497d82015-02-02 21:59:18 -080046 , m_keyChain(keyChain)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050047 , m_signingInfo(signingInfo)
Jiewen Tana0497d82015-02-02 21:59:18 -080048 , m_freshnessPeriod(freshnessPeriod)
49 {
50 }
51
52 virtual
53 ~SegmentPublisher()
54 {
55 }
56
Nick Gordond0a7df32017-05-30 16:44:34 -050057 /*! \brief Define the max segment size as half the max NDN packet size.
58 */
Jiewen Tana0497d82015-02-02 21:59:18 -080059 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 Gordond0a7df32017-05-30 16:44:34 -050072 /*! \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 Lehmand6bb3fa2015-04-24 14:21:39 -050078 */
Jiewen Tana0497d82015-02-02 21:59:18 -080079 void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080 publish(const ndn::Name& prefix)
Jiewen Tana0497d82015-02-02 21:59:18 -080081 {
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 Lehmand6bb3fa2015-04-24 14:21:39 -050089 ndn::Name segmentPrefix(prefix);
Jiewen Tana0497d82015-02-02 21:59:18 -080090 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
dmcoomes9f936662017-03-02 10:33:09 -0600102 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName);
Jiewen Tana0497d82015-02-02 21:59:18 -0800103 data->setContent(segmentBegin, segmentEnd - segmentBegin);
104 data->setFreshnessPeriod(m_freshnessPeriod);
105
106 segmentBegin = segmentEnd;
107 if (segmentBegin >= end) {
Ashlesh Gawanded65786a2018-03-30 01:17:58 -0500108 data->setFinalBlock(segmentName[-1]);
Jiewen Tana0497d82015-02-02 21:59:18 -0800109 }
110
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500111 publishSegment(data);
Jiewen Tana0497d82015-02-02 21:59:18 -0800112 ++segmentNo;
113 } while (segmentBegin < end);
114 }
115
116protected:
Nick G97e34942016-07-11 14:46:27 -0500117 /*! \brief In a derived class, write the octets into outBuffer.
Jiewen Tana0497d82015-02-02 21:59:18 -0800118 */
119 virtual size_t
120 generate(ndn::EncodingBuffer& outBuffer) = 0;
121
122private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500123 /*! \brief Helper function to sign and put data on a Face.
124 */
Jiewen Tana0497d82015-02-02 21:59:18 -0800125 void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500126 publishSegment(std::shared_ptr<ndn::Data>& data)
Jiewen Tana0497d82015-02-02 21:59:18 -0800127 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500128 m_keyChain.sign(*data, m_signingInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800129 m_face.put(*data);
130 }
131
132private:
133 FaceBase& m_face;
Jiewen Tana0497d82015-02-02 21:59:18 -0800134 ndn::KeyChain& m_keyChain;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500135 const ndn::security::SigningInfo& m_signingInfo;
Jiewen Tana0497d82015-02-02 21:59:18 -0800136 const ndn::time::milliseconds m_freshnessPeriod;
137};
138
139} // namespace nlsr
140
141#endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP