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-2020, 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, size_t imsLimit)
27  : m_face(face)
28  , m_scheduler(m_face.getIoService())
29  , m_keyChain(keyChain)
30  , m_ims(imsLimit)
31 {
32 }
33 
34 void
35 SegmentPublisher::publish(const ndn::Name& interestName, const ndn::Name& dataName,
36  const ndn::Block& block, ndn::time::milliseconds freshness,
37  const ndn::security::SigningInfo& signingInfo)
38 {
39  auto buf = std::make_shared<const ndn::Buffer>(block.wire(), block.size());
40  publish(interestName, dataName, buf, freshness, signingInfo);
41 }
42 
43 void
44 SegmentPublisher::publish(const ndn::Name& interestName, const ndn::Name& dataName,
45  const ndn::ConstBufferPtr& buffer,
46  ndn::time::milliseconds freshness,
47  const ndn::security::SigningInfo& signingInfo)
48 {
49  uint64_t interestSegment = 0;
50  if (interestName[-1].isSegment()) {
51  interestSegment = interestName[-1].toSegment();
52  }
53 
54  const uint8_t* rawBuffer = buffer->data();
55  const uint8_t* segmentBegin = rawBuffer;
56  const uint8_t* end = rawBuffer + buffer->size();
57 
58  size_t maxPacketSize = (ndn::MAX_NDN_PACKET_SIZE >> 1);
59 
60  uint64_t totalSegments = buffer->size() / maxPacketSize;
61 
62  ndn::Name segmentPrefix(dataName);
63  segmentPrefix.appendVersion();
64 
65  uint64_t segmentNo = 0;
66  do {
67  const uint8_t* segmentEnd = segmentBegin + maxPacketSize;
68  if (segmentEnd > end) {
69  segmentEnd = end;
70  }
71 
72  ndn::Name segmentName(segmentPrefix);
73  segmentName.appendSegment(segmentNo);
74 
75  // We get a std::exception: bad_weak_ptr from m_ims if we don't use shared_ptr for data
76  auto data = std::make_shared<ndn::Data>(segmentName);
77  data->setContent(segmentBegin, segmentEnd - segmentBegin);
78  data->setFreshnessPeriod(freshness);
79  data->setFinalBlock(ndn::name::Component::fromSegment(totalSegments));
80 
81  segmentBegin = segmentEnd;
82 
83  m_keyChain.sign(*data, signingInfo);
84 
85  // Put on face only the segment which has a pending interest
86  // otherwise the segment is unsolicited
87  if (interestSegment == segmentNo) {
88  m_face.put(*data);
89  }
90 
91  m_ims.insert(*data, freshness);
92  m_scheduler.schedule(freshness, [this, segmentName] { m_ims.erase(segmentName); });
93 
94  ++segmentNo;
95  } while (segmentBegin < end);
96 }
97 
98 bool
99 SegmentPublisher::replyFromStore(const ndn::Name& interestName)
100 {
101  auto it = m_ims.find(interestName);
102 
103  if (it != nullptr) {
104  m_face.put(*it);
105  return true;
106  }
107  return false;
108 }
109 
110 } // namespace psync
SegmentPublisher(ndn::Face &face, ndn::KeyChain &keyChain, size_t imsLimit=100)
Definition: common.hpp:33
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::SigningInfo())
Put all the segments in memory.
bool replyFromStore(const ndn::Name &interestName)
Try to reply from memory, return false if we cannot find the segment.