blob: 394dfc5e6eec8b488a2d3dcc1d2aa0b87d097076 [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,
Ashlesh Gawande2e82df12018-12-08 21:42:29 -060037 const ndn::Block& block, ndn::time::milliseconds freshness,
38 const ndn::security::SigningInfo& signingInfo)
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050039{
40 uint64_t interestSegment = 0;
41 if (interestName[-1].isSegment()) {
42 interestSegment = interestName[-1].toSegment();
43 }
44
45 ndn::EncodingBuffer buffer;
46 buffer.prependBlock(std::move(block));
47
48 const uint8_t* rawBuffer = buffer.buf();
49 const uint8_t* segmentBegin = rawBuffer;
50 const uint8_t* end = rawBuffer + buffer.size();
51
52 size_t maxPacketSize = (ndn::MAX_NDN_PACKET_SIZE >> 1);
53
54 uint64_t totalSegments = buffer.size() / maxPacketSize;
55
Ashlesh Gawande2e82df12018-12-08 21:42:29 -060056 ndn::Name segmentPrefix(dataName);
57 segmentPrefix.appendVersion();
58
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050059 uint64_t segmentNo = 0;
60 do {
61 const uint8_t* segmentEnd = segmentBegin + maxPacketSize;
62 if (segmentEnd > end) {
63 segmentEnd = end;
64 }
65
Ashlesh Gawande2e82df12018-12-08 21:42:29 -060066 ndn::Name segmentName(segmentPrefix);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050067 segmentName.appendSegment(segmentNo);
68
69 // We get a std::exception: bad_weak_ptr from m_ims if we don't use shared_ptr for data
70 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName);
71 data->setContent(segmentBegin, segmentEnd - segmentBegin);
72 data->setFreshnessPeriod(freshness);
73 data->setFinalBlock(ndn::name::Component::fromSegment(totalSegments));
74
75 segmentBegin = segmentEnd;
76
Ashlesh Gawande2e82df12018-12-08 21:42:29 -060077 m_keyChain.sign(*data, signingInfo);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050078
79 // Put on face only the segment which has a pending interest
80 // otherwise the segment is unsolicited
81 if (interestSegment == segmentNo) {
82 m_face.put(*data);
83 }
84
85 m_ims.insert(*data, freshness);
86 m_scheduler.scheduleEvent(freshness,
87 [this, segmentName] {
88 m_ims.erase(segmentName);
89 });
90
91 ++segmentNo;
92 } while (segmentBegin < end);
93}
94
95bool
96SegmentPublisher::replyFromStore(const ndn::Name& interestName)
97{
98 auto it = m_ims.find(interestName);
99
100 if (it != nullptr) {
101 m_face.put(*it);
102 return true;
103 }
104 return false;
105}
106
107} // namespace psync