24 #include <boost/iostreams/read.hpp>
30 : m_keyChain(keyChain)
31 , m_signingInfo(signingInfo)
35 std::vector<std::shared_ptr<Data>>
39 if (maxSegmentSize == 0) {
40 NDN_THROW(std::invalid_argument(
"maxSegmentSize must be greater than 0"));
44 const uint64_t numSegments = 1 + (buffer.size() - !buffer.empty()) / maxSegmentSize;
47 std::vector<std::shared_ptr<Data>> segments;
48 segments.reserve(numSegments);
51 auto segLen = std::min(buffer.size(), maxSegmentSize);
53 auto data = std::make_shared<Data>();
54 data->setName(
Name(dataName).appendSegment(segments.size()));
55 data->setContentType(contentType);
56 data->setFreshnessPeriod(freshnessPeriod);
57 data->setFinalBlock(finalBlockId);
58 data->setContent(buffer.first(segLen));
60 m_keyChain.sign(*data, m_signingInfo);
61 segments.push_back(std::move(data));
63 buffer = buffer.subspan(segLen);
64 }
while (!buffer.empty());
66 BOOST_ASSERT(segments.size() == numSegments);
70 std::vector<std::shared_ptr<Data>>
74 if (maxSegmentSize == 0) {
75 NDN_THROW(std::invalid_argument(
"maxSegmentSize must be greater than 0"));
78 std::vector<std::shared_ptr<Data>> segments;
81 auto buffer = std::make_shared<Buffer>(maxSegmentSize);
82 auto n = boost::iostreams::read(input, buffer->get<
char>(), buffer->size());
88 auto data = std::make_shared<Data>();
89 data->setName(
Name(dataName).appendSegment(segments.size()));
90 data->setContentType(contentType);
91 data->setFreshnessPeriod(freshnessPeriod);
92 data->setContent(std::move(buffer));
94 segments.push_back(std::move(data));
98 if (segments.empty()) {
99 auto data = std::make_shared<Data>();
100 data->setName(
Name(dataName).appendSegment(0));
101 data->setContentType(contentType);
102 data->setFreshnessPeriod(freshnessPeriod);
103 segments.push_back(std::move(data));
108 for (
const auto& data : segments) {
109 data->setFinalBlock(finalBlockId);
110 m_keyChain.sign(*data, m_signingInfo);
Represents an absolute name.
static Component fromSegment(uint64_t segmentNo)
Create a segment number component using NDN naming conventions.
Signing parameters passed to KeyChain.
Segmenter(KeyChain &keyChain, const security::SigningInfo &signingInfo)
Constructor.
std::vector< std::shared_ptr< Data > > segment(span< const uint8_t > buffer, const Name &dataName, size_t maxSegmentSize, time::milliseconds freshnessPeriod, uint32_t contentType=tlv::ContentType_Blob)
Splits a blob of bytes into one or more Data packets (segments).
boost::chrono::milliseconds milliseconds