blob: ccb34265358d6e3f0cb6721460e546ccbf7bb097 [file] [log] [blame]
Vince Lehman5144f822014-07-23 15:12:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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 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:
43 SegmentPublisher(FaceBase& face, const Name& prefix, ndn::KeyChain& keyChain)
44 : m_face(face)
45 , m_prefix(prefix)
46 , m_keyChain(keyChain)
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 void
63 publish()
64 {
Vince Lehman5144f822014-07-23 15:12:56 -070065 ndn::EncodingBuffer buffer;
Vince Lehman5144f822014-07-23 15:12:56 -070066 generate(buffer);
67
68 const uint8_t* rawBuffer = buffer.buf();
69 const uint8_t* segmentBegin = rawBuffer;
70 const uint8_t* end = rawBuffer + buffer.size();
71
Junxiao Shi15b12e72014-08-09 19:56:24 -070072 Name segmentPrefix(m_prefix);
73 segmentPrefix.appendVersion();
74
Vince Lehman5144f822014-07-23 15:12:56 -070075 uint64_t segmentNo = 0;
Junxiao Shi15b12e72014-08-09 19:56:24 -070076 do {
77 const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
78 if (segmentEnd > end) {
79 segmentEnd = end;
Vince Lehman5144f822014-07-23 15:12:56 -070080 }
Junxiao Shi15b12e72014-08-09 19:56:24 -070081
82 Name segmentName(segmentPrefix);
83 segmentName.appendSegment(segmentNo);
84
85 shared_ptr<Data> data = make_shared<Data>(segmentName);
86 data->setContent(segmentBegin, segmentEnd - segmentBegin);
87
88 segmentBegin = segmentEnd;
89 if (segmentBegin >= end) {
90 data->setFinalBlockId(segmentName[-1]);
91 }
92
93 publishSegment(data);
94 ++segmentNo;
95 } while (segmentBegin < end);
Vince Lehman5144f822014-07-23 15:12:56 -070096 }
97
98protected:
Junxiao Shi15b12e72014-08-09 19:56:24 -070099 /** \brief In a derived class, write the octets into outBuffer.
100 */
Vince Lehman5144f822014-07-23 15:12:56 -0700101 virtual size_t
Junxiao Shi15b12e72014-08-09 19:56:24 -0700102 generate(ndn::EncodingBuffer& outBuffer) = 0;
Vince Lehman5144f822014-07-23 15:12:56 -0700103
104private:
105 void
106 publishSegment(shared_ptr<Data>& data)
107 {
108 m_keyChain.sign(*data);
109 m_face.put(*data);
110 }
111
112private:
113 FaceBase& m_face;
114 const Name m_prefix;
115 ndn::KeyChain& m_keyChain;
116};
117
118} // namespace nfd
119
120#endif // NFD_CORE_SEGMENT_PUBLISHER_HPP