blob: d78d47f5a593f9487544c12e04a0f23093cabfb0 [file] [log] [blame]
Ashlesh Gawandeec43b362018-08-01 15:15:01 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, The University of Memphis
4 *
5 * This file is part of PSync.
6 * See AUTHORS.md for complete list of PSync authors and contributors.
7 *
8 * PSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#include "segment-publisher.hpp"
21
22#include <ndn-cxx/name-component.hpp>
23
24namespace psync {
25
26SegmentPublisher::SegmentPublisher(ndn::Face& face, ndn::KeyChain& keyChain,
27 size_t imsLimit)
28 : m_face(face)
29 , m_scheduler(m_face.getIoService())
30 , m_keyChain(keyChain)
31 , m_ims(imsLimit)
32{
33}
34
35void
36SegmentPublisher::publish(const ndn::Name& interestName, const ndn::Name& dataName,
37 const ndn::Block& block, ndn::time::milliseconds freshness)
38{
39 uint64_t interestSegment = 0;
40 if (interestName[-1].isSegment()) {
41 interestSegment = interestName[-1].toSegment();
42 }
43
44 ndn::EncodingBuffer buffer;
45 buffer.prependBlock(std::move(block));
46
47 const uint8_t* rawBuffer = buffer.buf();
48 const uint8_t* segmentBegin = rawBuffer;
49 const uint8_t* end = rawBuffer + buffer.size();
50
51 size_t maxPacketSize = (ndn::MAX_NDN_PACKET_SIZE >> 1);
52
53 uint64_t totalSegments = buffer.size() / maxPacketSize;
54
55 uint64_t segmentNo = 0;
56 do {
57 const uint8_t* segmentEnd = segmentBegin + maxPacketSize;
58 if (segmentEnd > end) {
59 segmentEnd = end;
60 }
61
62 ndn::Name segmentName(dataName);
63 segmentName.appendSegment(segmentNo);
64
65 // We get a std::exception: bad_weak_ptr from m_ims if we don't use shared_ptr for data
66 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName);
67 data->setContent(segmentBegin, segmentEnd - segmentBegin);
68 data->setFreshnessPeriod(freshness);
69 data->setFinalBlock(ndn::name::Component::fromSegment(totalSegments));
70
71 segmentBegin = segmentEnd;
72
73 m_keyChain.sign(*data);
74
75 // Put on face only the segment which has a pending interest
76 // otherwise the segment is unsolicited
77 if (interestSegment == segmentNo) {
78 m_face.put(*data);
79 }
80
81 m_ims.insert(*data, freshness);
82 m_scheduler.scheduleEvent(freshness,
83 [this, segmentName] {
84 m_ims.erase(segmentName);
85 });
86
87 ++segmentNo;
88 } while (segmentBegin < end);
89}
90
91bool
92SegmentPublisher::replyFromStore(const ndn::Name& interestName)
93{
94 auto it = m_ims.find(interestName);
95
96 if (it != nullptr) {
97 m_face.put(*it);
98 return true;
99 }
100 return false;
101}
102
103} // namespace psync