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-2022, 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 namespace psync {
23 
24 SegmentPublisher::SegmentPublisher(ndn::Face& face, ndn::KeyChain& keyChain,
25  const ndn::security::SigningInfo& signingInfo, size_t imsLimit)
26  : m_face(face)
27  , m_scheduler(m_face.getIoService())
28  , m_segmenter(keyChain, signingInfo)
29  , m_ims(imsLimit)
30 {
31 }
32 
33 void
34 SegmentPublisher::publish(const ndn::Name& interestName, const ndn::Name& dataName,
35  ndn::span<const uint8_t> buffer, ndn::time::milliseconds freshness)
36 {
37  auto segments = m_segmenter.segment(buffer, ndn::Name(dataName).appendVersion(),
38  ndn::MAX_NDN_PACKET_SIZE >> 1, freshness);
39  for (const auto& data : segments) {
40  m_ims.insert(*data, freshness);
41  m_scheduler.schedule(freshness, [this, name = data->getName()] { m_ims.erase(name); });
42  }
43 
44  // Put on face only the segment which has a pending interest,
45  // otherwise the segment is unsolicited
46  uint64_t interestSegment = 0;
47  if (interestName[-1].isSegment()) {
48  interestSegment = interestName[-1].toSegment();
49  }
50  if (interestSegment < segments.size()) {
51  m_face.put(*segments[interestSegment]);
52  }
53 }
54 
55 bool
56 SegmentPublisher::replyFromStore(const ndn::Name& interestName)
57 {
58  auto it = m_ims.find(interestName);
59  if (it != nullptr) {
60  m_face.put(*it);
61  return true;
62  }
63  return false;
64 }
65 
66 } // namespace psync
bool replyFromStore(const ndn::Name &interestName)
Try to reply from memory, return false if we cannot find the segment.
SegmentPublisher(ndn::Face &face, ndn::KeyChain &keyChain, const ndn::security::SigningInfo &signingInfo=ndn::security::SigningInfo(), size_t imsLimit=100)
void publish(const ndn::Name &interestName, const ndn::Name &dataName, ndn::span< const uint8_t > buffer, ndn::time::milliseconds freshness)
Put all the segments in memory.
Definition: common.hpp:34