blob: bd7d971f535e50566a113b7921c5da0ad9048697 [file] [log] [blame]
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "segment-publisher.hpp"
8
9#include "common.hpp"
10#include "face/face.hpp"
11
12#include <ndn-cpp-dev/util/time.hpp>
13
14namespace nfd {
15
16NFD_LOG_INIT("SegmentPublisher");
17
18SegmentPublisher::SegmentPublisher(shared_ptr<AppFace> face,
19 const Name& prefix)
20 : m_face(face)
21 , m_prefix(prefix)
22{
23
24}
25
26
27SegmentPublisher::~SegmentPublisher()
28{
29
30}
31
32void
33SegmentPublisher::publish()
34{
35 Name segmentPrefix(m_prefix);
36 segmentPrefix.appendSegment(ndn::ndn_getNowMilliseconds());
37
38 static const size_t MAX_SEGMENT_SIZE = MAX_NDN_PACKET_SIZE >> 1;
39
40 ndn::EncodingBuffer buffer;
41
42 generate(buffer);
43
44 const uint8_t* rawBuffer = buffer.buf();
45 const uint8_t* segmentBegin = rawBuffer;
46 const uint8_t* end = rawBuffer + buffer.size();
47
48 uint64_t segmentNo = 0;
49 while (segmentBegin < end)
50 {
51 const uint8_t* segmentEnd = segmentBegin + MAX_SEGMENT_SIZE;
52 if (segmentEnd > end)
53 {
54 segmentEnd = end;
55 }
56
57 Name segmentName(segmentPrefix);
58 segmentName.appendSegment(segmentNo);
59
60 shared_ptr<Data> data(make_shared<Data>(segmentName));
61 data->setContent(segmentBegin, segmentEnd - segmentBegin);
62
63 segmentBegin = segmentEnd;
64 if (segmentBegin >= end)
65 {
66 NFD_LOG_DEBUG("final block is " << segmentNo);
67 data->setFinalBlockId(segmentName[-1]);
68 }
69
70 NFD_LOG_DEBUG("publishing segment #" << segmentNo);
71 publishSegment(data);
72 segmentNo++;
73 }
74}
75
76void
77SegmentPublisher::publishSegment(shared_ptr<Data>& data)
78{
79 m_face->sign(*data);
80 m_face->put(*data);
81}
82
83} // namespace nfd