blob: d79586d7aad7d9ca1e5592a1510ff566a60e41bc [file] [log] [blame]
Vince Lehman5144f822014-07-23 15:12:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedetto8919abe2015-01-28 09:34:54 -07003 * 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 Lehman5144f822014-07-23 15:12:56 -070010 *
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
34namespace nfd {
35
Junxiao Shi15b12e72014-08-09 19:56:24 -070036/** \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 Lehman5144f822014-07-23 15:12:56 -070039template <class FaceBase>
40class SegmentPublisher : noncopyable
41{
42public:
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070043 SegmentPublisher(FaceBase& face,
44 const Name& prefix,
45 ndn::KeyChain& keyChain,
46 const time::milliseconds& freshnessPeriod = getDefaultFreshness())
Vince Lehman5144f822014-07-23 15:12:56 -070047 : m_face(face)
48 , m_prefix(prefix)
49 , m_keyChain(keyChain)
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070050 , m_freshnessPeriod(freshnessPeriod)
Vince Lehman5144f822014-07-23 15:12:56 -070051 {
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 DiBenedetto8919abe2015-01-28 09:34:54 -070066 static constexpr time::milliseconds
67 getDefaultFreshness()
68 {
69 return time::milliseconds(1000);
70 }
71
Vince Lehman5144f822014-07-23 15:12:56 -070072 void
73 publish()
74 {
Vince Lehman5144f822014-07-23 15:12:56 -070075 ndn::EncodingBuffer buffer;
Vince Lehman5144f822014-07-23 15:12:56 -070076 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 Shi15b12e72014-08-09 19:56:24 -070082 Name segmentPrefix(m_prefix);
83 segmentPrefix.appendVersion();
84
Vince Lehman5144f822014-07-23 15:12:56 -070085 uint64_t segmentNo = 0;
Junxiao Shi15b12e72014-08-09 19:56:24 -070086 do {
87 const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
88 if (segmentEnd > end) {
89 segmentEnd = end;
Vince Lehman5144f822014-07-23 15:12:56 -070090 }
Junxiao Shi15b12e72014-08-09 19:56:24 -070091
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 DiBenedetto8919abe2015-01-28 09:34:54 -070097 data->setFreshnessPeriod(m_freshnessPeriod);
Junxiao Shi15b12e72014-08-09 19:56:24 -070098
99 segmentBegin = segmentEnd;
100 if (segmentBegin >= end) {
101 data->setFinalBlockId(segmentName[-1]);
102 }
103
104 publishSegment(data);
105 ++segmentNo;
106 } while (segmentBegin < end);
Vince Lehman5144f822014-07-23 15:12:56 -0700107 }
108
109protected:
Junxiao Shi15b12e72014-08-09 19:56:24 -0700110 /** \brief In a derived class, write the octets into outBuffer.
111 */
Vince Lehman5144f822014-07-23 15:12:56 -0700112 virtual size_t
Junxiao Shi15b12e72014-08-09 19:56:24 -0700113 generate(ndn::EncodingBuffer& outBuffer) = 0;
Vince Lehman5144f822014-07-23 15:12:56 -0700114
115private:
116 void
117 publishSegment(shared_ptr<Data>& data)
118 {
119 m_keyChain.sign(*data);
120 m_face.put(*data);
121 }
122
123private:
124 FaceBase& m_face;
125 const Name m_prefix;
126 ndn::KeyChain& m_keyChain;
Steve DiBenedetto8919abe2015-01-28 09:34:54 -0700127 const time::milliseconds m_freshnessPeriod;
Vince Lehman5144f822014-07-23 15:12:56 -0700128};
129
130} // namespace nfd
131
132#endif // NFD_CORE_SEGMENT_PUBLISHER_HPP