Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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 | * |
| 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 DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 24 | |
| 25 | #include "segment-publisher.hpp" |
| 26 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 28 | #include "face/face.hpp" |
| 29 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/util/time.hpp> |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | NFD_LOG_INIT("SegmentPublisher"); |
| 35 | |
| 36 | SegmentPublisher::SegmentPublisher(shared_ptr<AppFace> face, |
| 37 | const Name& prefix) |
| 38 | : m_face(face) |
| 39 | , m_prefix(prefix) |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | |
| 45 | SegmentPublisher::~SegmentPublisher() |
| 46 | { |
| 47 | |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | SegmentPublisher::publish() |
| 52 | { |
| 53 | Name segmentPrefix(m_prefix); |
Alexander Afanasyev | 06ae907 | 2014-03-22 23:42:08 -0700 | [diff] [blame] | 54 | segmentPrefix.appendVersion(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 55 | |
| 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 | |
| 94 | void |
| 95 | SegmentPublisher::publishSegment(shared_ptr<Data>& data) |
| 96 | { |
| 97 | m_face->sign(*data); |
| 98 | m_face->put(*data); |
| 99 | } |
| 100 | |
| 101 | } // namespace nfd |