NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
segment-publisher.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #ifndef NFD_CORE_SEGMENT_PUBLISHER_HPP
27 #define NFD_CORE_SEGMENT_PUBLISHER_HPP
28 
29 #include "common.hpp"
30 
31 #include <ndn-cxx/encoding/encoding-buffer.hpp>
32 #include <ndn-cxx/security/key-chain.hpp>
33 
34 namespace nfd {
35 
39 template <class FaceBase>
40 class SegmentPublisher : noncopyable
41 {
42 public:
44  const Name& prefix,
45  ndn::KeyChain& keyChain,
46  const time::milliseconds& freshnessPeriod = getDefaultFreshness())
47  : m_face(face)
48  , m_prefix(prefix)
49  , m_keyChain(keyChain)
50  , m_freshnessPeriod(freshnessPeriod)
51  {
52  }
53 
54  virtual
56  {
57  }
58 
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 time::milliseconds
68  {
69  return time::milliseconds(1000);
70  }
71 
72  void
74  {
75  ndn::EncodingBuffer buffer;
76  generate(buffer);
77 
78  const uint8_t* rawBuffer = buffer.buf();
79  const uint8_t* segmentBegin = rawBuffer;
80  const uint8_t* end = rawBuffer + buffer.size();
81 
82  Name segmentPrefix(m_prefix);
83  segmentPrefix.appendVersion();
84 
85  uint64_t segmentNo = 0;
86  do {
87  const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
88  if (segmentEnd > end) {
89  segmentEnd = end;
90  }
91 
92  Name segmentName(segmentPrefix);
93  segmentName.appendSegment(segmentNo);
94 
95  shared_ptr<Data> data = make_shared<Data>(segmentName);
96  data->setContent(segmentBegin, segmentEnd - segmentBegin);
97  data->setFreshnessPeriod(m_freshnessPeriod);
98 
99  segmentBegin = segmentEnd;
100  if (segmentBegin >= end) {
101  data->setFinalBlockId(segmentName[-1]);
102  }
103 
104  publishSegment(data);
105  ++segmentNo;
106  } while (segmentBegin < end);
107  }
108 
109 protected:
112  virtual size_t
113  generate(ndn::EncodingBuffer& outBuffer) = 0;
114 
115 private:
116  void
117  publishSegment(shared_ptr<Data>& data)
118  {
119  m_keyChain.sign(*data);
120  m_face.put(*data);
121  }
122 
123 private:
124  FaceBase& m_face;
125  const Name m_prefix;
126  ndn::KeyChain& m_keyChain;
127  const time::milliseconds m_freshnessPeriod;
128 };
129 
130 } // namespace nfd
131 
132 #endif // NFD_CORE_SEGMENT_PUBLISHER_HPP
virtual size_t generate(ndn::EncodingBuffer &outBuffer)=0
In a derived class, write the octets into outBuffer.
provides a publisher of Status Dataset or other segmented octet stream
The packet signing interface.
Definition: key-chain.hpp:48
void sign(Data &data, const SigningInfo &params=DEFAULT_SIGNING_INFO)
Sign data according to the supplied signing information.
Definition: key-chain.cpp:501
static constexpr time::milliseconds getDefaultFreshness()
EncodingImpl< EncoderTag > EncodingBuffer
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
Name & appendSegment(uint64_t segmentNo)
Append segment number (sequential) using NDN naming conventions.
Definition: name.cpp:232
SegmentPublisher(FaceBase &face, const Name &prefix, ndn::KeyChain &keyChain, const time::milliseconds &freshnessPeriod=getDefaultFreshness())
Name abstraction to represent an absolute name.
Definition: name.hpp:46
static size_t getMaxSegmentSize()
Name & appendVersion(uint64_t version)
Append version using NDN naming conventions.
Definition: name.cpp:218
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:39