blob: cb311e90877b8f96787d2c66cce45672779bf692 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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.
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
34/** \brief provides a publisher of Status Dataset or other segmented octet stream
35 * \sa http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
36 */
37template <class FaceBase>
38class SegmentPublisher : ndn::noncopyable
39{
40public:
41 SegmentPublisher(FaceBase& face,
42 const ndn::Name& prefix,
43 ndn::KeyChain& keyChain,
44 const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness())
45 : m_face(face)
46 , m_prefix(prefix)
47 , m_keyChain(keyChain)
48 , m_freshnessPeriod(freshnessPeriod)
49 {
50 }
51
52 virtual
53 ~SegmentPublisher()
54 {
55 }
56
57 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
70 void
71 publish()
72 {
73 ndn::EncodingBuffer buffer;
74 generate(buffer);
75
76 const uint8_t* rawBuffer = buffer.buf();
77 const uint8_t* segmentBegin = rawBuffer;
78 const uint8_t* end = rawBuffer + buffer.size();
79
80 ndn::Name segmentPrefix(m_prefix);
81 segmentPrefix.appendVersion();
82
83 uint64_t segmentNo = 0;
84 do {
85 const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
86 if (segmentEnd > end) {
87 segmentEnd = end;
88 }
89
90 ndn::Name segmentName(segmentPrefix);
91 segmentName.appendSegment(segmentNo);
92
93 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>(segmentName);
94 data->setContent(segmentBegin, segmentEnd - segmentBegin);
95 data->setFreshnessPeriod(m_freshnessPeriod);
96
97 segmentBegin = segmentEnd;
98 if (segmentBegin >= end) {
99 data->setFinalBlockId(segmentName[-1]);
100 }
101
102 publishSegment(data);
103 ++segmentNo;
104 } while (segmentBegin < end);
105 }
106
107protected:
108 /** \brief In a derived class, write the octets into outBuffer.
109 */
110 virtual size_t
111 generate(ndn::EncodingBuffer& outBuffer) = 0;
112
113private:
114 void
115 publishSegment(ndn::shared_ptr<ndn::Data>& data)
116 {
117 m_keyChain.sign(*data);
118 m_face.put(*data);
119 }
120
121private:
122 FaceBase& m_face;
123 const ndn::Name m_prefix;
124 ndn::KeyChain& m_keyChain;
125 const ndn::time::milliseconds m_freshnessPeriod;
126};
127
128} // namespace nlsr
129
130#endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP