blob: 01f3d1293868a50f6c8bce23b36476a0125fb699 [file] [log] [blame]
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060024
25#include "segment-publisher.hpp"
26
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060028#include "face/face.hpp"
29
Alexander Afanasyev4a771362014-04-24 21:29:33 -070030#include <ndn-cxx/util/time.hpp>
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060031
32namespace nfd {
33
34NFD_LOG_INIT("SegmentPublisher");
35
36SegmentPublisher::SegmentPublisher(shared_ptr<AppFace> face,
37 const Name& prefix)
38 : m_face(face)
39 , m_prefix(prefix)
40{
41
42}
43
44
45SegmentPublisher::~SegmentPublisher()
46{
47
48}
49
50void
51SegmentPublisher::publish()
52{
53 Name segmentPrefix(m_prefix);
Alexander Afanasyev06ae9072014-03-22 23:42:08 -070054 segmentPrefix.appendVersion();
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060055
56 static const size_t MAX_SEGMENT_SIZE = MAX_NDN_PACKET_SIZE >> 1;
57
58 ndn::EncodingBuffer buffer;
59
60 generate(buffer);
61
62 const uint8_t* rawBuffer = buffer.buf();
63 const uint8_t* segmentBegin = rawBuffer;
64 const uint8_t* end = rawBuffer + buffer.size();
65
66 uint64_t segmentNo = 0;
67 while (segmentBegin < end)
68 {
69 const uint8_t* segmentEnd = segmentBegin + MAX_SEGMENT_SIZE;
70 if (segmentEnd > end)
71 {
72 segmentEnd = end;
73 }
74
75 Name segmentName(segmentPrefix);
76 segmentName.appendSegment(segmentNo);
77
78 shared_ptr<Data> data(make_shared<Data>(segmentName));
79 data->setContent(segmentBegin, segmentEnd - segmentBegin);
80
81 segmentBegin = segmentEnd;
82 if (segmentBegin >= end)
83 {
84 NFD_LOG_DEBUG("final block is " << segmentNo);
85 data->setFinalBlockId(segmentName[-1]);
86 }
87
88 NFD_LOG_DEBUG("publishing segment #" << segmentNo);
89 publishSegment(data);
90 segmentNo++;
91 }
92}
93
94void
95SegmentPublisher::publishSegment(shared_ptr<Data>& data)
96{
97 m_face->sign(*data);
98 m_face->put(*data);
99}
100
101} // namespace nfd