Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1 | /* -*- 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 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 9 | #include "core/logger.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 10 | #include "face/face.hpp" |
| 11 | |
| 12 | #include <ndn-cpp-dev/util/time.hpp> |
| 13 | |
| 14 | namespace nfd { |
| 15 | |
| 16 | NFD_LOG_INIT("SegmentPublisher"); |
| 17 | |
| 18 | SegmentPublisher::SegmentPublisher(shared_ptr<AppFace> face, |
| 19 | const Name& prefix) |
| 20 | : m_face(face) |
| 21 | , m_prefix(prefix) |
| 22 | { |
| 23 | |
| 24 | } |
| 25 | |
| 26 | |
| 27 | SegmentPublisher::~SegmentPublisher() |
| 28 | { |
| 29 | |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | SegmentPublisher::publish() |
| 34 | { |
| 35 | Name segmentPrefix(m_prefix); |
Alexander Afanasyev | 06ae907 | 2014-03-22 23:42:08 -0700 | [diff] [blame] | 36 | segmentPrefix.appendVersion(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 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 | |
| 76 | void |
| 77 | SegmentPublisher::publishSegment(shared_ptr<Data>& data) |
| 78 | { |
| 79 | m_face->sign(*data); |
| 80 | m_face->put(*data); |
| 81 | } |
| 82 | |
| 83 | } // namespace nfd |