blob: b1aa4a9c2f248ccaed9d3b9c0e9f29103f6994a6 [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
36template <class FaceBase>
37class SegmentPublisher : noncopyable
38{
39public:
40 SegmentPublisher(FaceBase& face, const Name& prefix, ndn::KeyChain& keyChain)
41 : m_face(face)
42 , m_prefix(prefix)
43 , m_keyChain(keyChain)
44 {
45 }
46
47 virtual
48 ~SegmentPublisher()
49 {
50 }
51
52 static size_t
53 getMaxSegmentSize()
54 {
55 static const size_t MAX_SEGMENT_SIZE = ndn::MAX_NDN_PACKET_SIZE >> 1;
56 return MAX_SEGMENT_SIZE;
57 }
58
59 void
60 publish()
61 {
62 Name segmentPrefix(m_prefix);
63 segmentPrefix.appendVersion();
64
65 ndn::EncodingBuffer buffer;
66
67 generate(buffer);
68
69 const uint8_t* rawBuffer = buffer.buf();
70 const uint8_t* segmentBegin = rawBuffer;
71 const uint8_t* end = rawBuffer + buffer.size();
72
73 uint64_t segmentNo = 0;
Alexander Afanasyev4e9a98f2014-07-23 11:12:30 -070074 do
Vince Lehman5144f822014-07-23 15:12:56 -070075 {
76 const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
77 if (segmentEnd > end)
78 {
79 segmentEnd = end;
80 }
81
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 {
91 data->setFinalBlockId(segmentName[-1]);
92 }
93
94 publishSegment(data);
95 segmentNo++;
96 }
Alexander Afanasyev4e9a98f2014-07-23 11:12:30 -070097 while (segmentBegin < end);
Vince Lehman5144f822014-07-23 15:12:56 -070098 }
99
100protected:
101
102 virtual size_t
103 generate(ndn::EncodingBuffer& outBuffer) =0;
104
105private:
106 void
107 publishSegment(shared_ptr<Data>& data)
108 {
109 m_keyChain.sign(*data);
110 m_face.put(*data);
111 }
112
113private:
114 FaceBase& m_face;
115 const Name m_prefix;
116 ndn::KeyChain& m_keyChain;
117};
118
119} // namespace nfd
120
121#endif // NFD_CORE_SEGMENT_PUBLISHER_HPP