segment-publisher.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #ifndef NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP
27 #define NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP
28 
29 #include <ndn-cxx/encoding/encoding-buffer.hpp>
30 #include <ndn-cxx/security/key-chain.hpp>
31 
32 namespace nlsr {
33 
37 template <class FaceBase>
38 class SegmentPublisher : boost::noncopyable
39 {
40 public:
41  SegmentPublisher(FaceBase& face,
42  ndn::KeyChain& keyChain,
43  const ndn::security::SigningInfo& signingInfo,
44  const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness())
45  : m_face(face)
46  , m_keyChain(keyChain)
47  , m_signingInfo(signingInfo)
48  , m_freshnessPeriod(freshnessPeriod)
49  {
50  }
51 
52  virtual
54  {
55  }
56 
59  static size_t
61  {
62  static const size_t MAX_SEGMENT_SIZE = ndn::MAX_NDN_PACKET_SIZE >> 1;
63  return MAX_SEGMENT_SIZE;
64  }
65 
66  static constexpr ndn::time::milliseconds
68  {
69  return ndn::time::milliseconds(1000);
70  }
71 
79  void
80  publish(const ndn::Name& prefix)
81  {
82  ndn::EncodingBuffer buffer;
83  generate(buffer);
84 
85  const uint8_t* rawBuffer = buffer.buf();
86  const uint8_t* segmentBegin = rawBuffer;
87  const uint8_t* end = rawBuffer + buffer.size();
88 
89  ndn::Name segmentPrefix(prefix);
90  segmentPrefix.appendVersion();
91 
92  uint64_t segmentNo = 0;
93  do {
94  const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
95  if (segmentEnd > end) {
96  segmentEnd = end;
97  }
98 
99  ndn::Name segmentName(segmentPrefix);
100  segmentName.appendSegment(segmentNo);
101 
102  std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName);
103  data->setContent(segmentBegin, segmentEnd - segmentBegin);
104  data->setFreshnessPeriod(m_freshnessPeriod);
105 
106  segmentBegin = segmentEnd;
107  if (segmentBegin >= end) {
108  data->setFinalBlockId(segmentName[-1]);
109  }
110 
111  publishSegment(data);
112  ++segmentNo;
113  } while (segmentBegin < end);
114  }
115 
116 protected:
119  virtual size_t
120  generate(ndn::EncodingBuffer& outBuffer) = 0;
121 
122 private:
125  void
126  publishSegment(std::shared_ptr<ndn::Data>& data)
127  {
128  m_keyChain.sign(*data, m_signingInfo);
129  m_face.put(*data);
130  }
131 
132 private:
133  FaceBase& m_face;
134  ndn::KeyChain& m_keyChain;
135  const ndn::security::SigningInfo& m_signingInfo;
136  const ndn::time::milliseconds m_freshnessPeriod;
137 };
138 
139 } // namespace nlsr
140 
141 #endif // NLSR_PUBLISHER_SEGMENT_PUBLISHER_HPP
SegmentPublisher(FaceBase &face, ndn::KeyChain &keyChain, const ndn::security::SigningInfo &signingInfo, const ndn::time::milliseconds &freshnessPeriod=getDefaultFreshness())
static size_t getMaxSegmentSize()
Define the max segment size as half the max NDN packet size.
virtual size_t generate(ndn::EncodingBuffer &outBuffer)=0
In a derived class, write the octets into outBuffer.
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void publish(const ndn::Name &prefix)
Publish data under the provided prefix.
static constexpr ndn::time::milliseconds getDefaultFreshness()
provides a publisher of Status Dataset or other segmented octet stream