segment-publisher.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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 Lesser 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License along with
17  * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18  **/
19 
21 
22 #include <ndn-cxx/name-component.hpp>
23 
24 namespace psync {
25 
26 SegmentPublisher::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 
35 void
36 SegmentPublisher::publish(const ndn::Name& interestName, const ndn::Name& dataName,
37  const ndn::Block& block, ndn::time::milliseconds freshness,
38  const ndn::security::SigningInfo& signingInfo)
39 {
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 
56  ndn::Name segmentPrefix(dataName);
57  segmentPrefix.appendVersion();
58 
59  uint64_t segmentNo = 0;
60  do {
61  const uint8_t* segmentEnd = segmentBegin + maxPacketSize;
62  if (segmentEnd > end) {
63  segmentEnd = end;
64  }
65 
66  ndn::Name segmentName(segmentPrefix);
67  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 
77  m_keyChain.sign(*data, signingInfo);
78 
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 
95 bool
96 SegmentPublisher::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
void publish(const ndn::Name &interestName, const ndn::Name &dataName, const ndn::Block &block, ndn::time::milliseconds freshness, const ndn::security::SigningInfo &signingInfo=ndn::security::v2::KeyChain::getDefaultSigningInfo())
Put all the segments in memory.
SegmentPublisher(ndn::Face &face, ndn::KeyChain &keyChain, size_t imsLimit=MAX_SEGMENTS_STORED)
bool replyFromStore(const ndn::Name &interestName)
Try to reply from memory, return false if we cannot find the segment.