NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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:
43  SegmentPublisher(FaceBase& face, const Name& prefix, ndn::KeyChain& keyChain)
44  : m_face(face)
45  , m_prefix(prefix)
46  , m_keyChain(keyChain)
47  {
48  }
49 
50  virtual
52  {
53  }
54 
55  static size_t
57  {
58  static const size_t MAX_SEGMENT_SIZE = ndn::MAX_NDN_PACKET_SIZE >> 1;
59  return MAX_SEGMENT_SIZE;
60  }
61 
62  void
64  {
65  ndn::EncodingBuffer buffer;
66  generate(buffer);
67 
68  const uint8_t* rawBuffer = buffer.buf();
69  const uint8_t* segmentBegin = rawBuffer;
70  const uint8_t* end = rawBuffer + buffer.size();
71 
72  Name segmentPrefix(m_prefix);
73  segmentPrefix.appendVersion();
74 
75  uint64_t segmentNo = 0;
76  do {
77  const uint8_t* segmentEnd = segmentBegin + getMaxSegmentSize();
78  if (segmentEnd > end) {
79  segmentEnd = end;
80  }
81 
82  Name segmentName(segmentPrefix);
83  segmentName.appendSegment(segmentNo);
84 
85  shared_ptr<Data> data = make_shared<Data>(segmentName);
86  data->setContent(segmentBegin, segmentEnd - segmentBegin);
87 
88  segmentBegin = segmentEnd;
89  if (segmentBegin >= end) {
90  data->setFinalBlockId(segmentName[-1]);
91  }
92 
93  publishSegment(data);
94  ++segmentNo;
95  } while (segmentBegin < end);
96  }
97 
98 protected:
101  virtual size_t
102  generate(ndn::EncodingBuffer& outBuffer) = 0;
103 
104 private:
105  void
106  publishSegment(shared_ptr<Data>& data)
107  {
108  m_keyChain.sign(*data);
109  m_face.put(*data);
110  }
111 
112 private:
113  FaceBase& m_face;
114  const Name m_prefix;
115  ndn::KeyChain& m_keyChain;
116 };
117 
118 } // namespace nfd
119 
120 #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
SegmentPublisher(FaceBase &face, const Name &prefix, ndn::KeyChain &keyChain)
static size_t getMaxSegmentSize()