esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
segment-producer.hpp
Go to the documentation of this file.
1#ifndef NDNPH_APP_SEGMENT_PRODUCER_HPP
2#define NDNPH_APP_SEGMENT_PRODUCER_HPP
3
4#include "../face/packet-handler.hpp"
5#include "../keychain/digest.hpp"
6
7namespace ndnph {
8
10public:
11 struct Options {
13
15 size_t contentLen = 1000;
16
17 uint32_t freshnessPeriod = 1000;
18
30 int discovery = 2;
31 };
32
38 explicit SegmentProducerBase(Face& face, Options opts)
39 : PacketHandler(face)
40 , m_opts(std::move(opts)) {}
41
42 explicit SegmentProducerBase(Face& face)
43 : SegmentProducerBase(face, Options()) {}
44
52 void setContent(Name prefix, const uint8_t* content, size_t size) {
53 m_prefix = prefix;
54 m_content = content;
55 m_size = size;
56 m_lastSegment = divCeil(std::max(size, static_cast<size_t>(1)), m_opts.contentLen) - 1;
57 }
58
59protected:
62 uint64_t m_lastSegment = 0;
63 const uint8_t* m_content = nullptr;
64 size_t m_size = 0;
65};
66
72template<typename SegmentConvention = convention::Segment, size_t regionCap = 2048>
74public:
76
77private:
78 bool processInterest(Interest interest) final {
79 if (!m_prefix || m_content == nullptr) {
80 return false;
81 }
82
83 const Name& interestName = interest.getName();
84 size_t dataNameSize = m_prefix.size() + 1;
85 if (interestName.size() == dataNameSize) {
86 auto lastComp = interestName[-1];
87 if (!m_prefix.isPrefixOf(interestName) || !lastComp.is<SegmentConvention>()) {
88 return false;
89 }
90 return replySegment(lastComp.as<SegmentConvention>());
91 }
92
93 if (interestName.size() >= dataNameSize - m_opts.discovery &&
94 interestName.isPrefixOf(m_prefix) && interest.getCanBePrefix()) {
95 return replySegment(0);
96 }
97 return false;
98 }
99
100 bool replySegment(uint64_t segment) {
101 if (segment > m_lastSegment) {
102 return true;
103 }
104
105 StaticRegion<regionCap> region;
106 Data data = region.template create<Data>();
107 NDNPH_ASSERT(!!data);
108 data.setName(m_prefix.append(region, SegmentConvention(), segment));
109 data.setFreshnessPeriod(m_opts.freshnessPeriod);
110 data.setIsFinalBlock(segment == m_lastSegment);
111 data.setContent(
112 tlv::Value(m_content + m_opts.contentLen * segment,
113 m_content + std::min<size_t>(m_opts.contentLen * (segment + 1), m_size)));
114 reply(data.sign(m_opts.signer));
115 return true;
116 }
117};
118
120
121} // namespace ndnph
122
123#endif // NDNPH_APP_SEGMENT_PRODUCER_HPP
Producer of segmented object.
Definition segment-producer.hpp:73
bool processInterest(Interest interest) final
Override to receive Interest packets.
Definition segment-producer.hpp:78
static const DigestKey & get()
Definition digest.hpp:16
Network layer face.
Definition face.hpp:12
Interest packet.
Definition interest.hpp:284
Name.
Definition name.hpp:14
size_t size() const
Get number of components.
Definition name.hpp:86
Name append(Region &region, const C &... comps) const
Append a sequence of components.
Definition name.hpp:183
bool isPrefixOf(const Name &other) const
Determine if this name is a prefix of other.
Definition name.hpp:239
Base class to receive packets from Face.
Definition packet-handler.hpp:10
bool reply(Arg &&... arg)
Synchronously transmit a packet in reply to current processing packet.
Definition packet-handler.hpp:119
Private key.
Definition private-key.hpp:9
Definition segment-producer.hpp:9
const uint8_t * m_content
Definition segment-producer.hpp:63
void setContent(Name prefix, const uint8_t *content, size_t size)
Set or change served content.
Definition segment-producer.hpp:52
SegmentProducerBase(Face &face)
Definition segment-producer.hpp:42
SegmentProducerBase(Face &face, Options opts)
Constructor.
Definition segment-producer.hpp:38
size_t m_size
Definition segment-producer.hpp:64
Options m_opts
Definition segment-producer.hpp:60
Name m_prefix
Definition segment-producer.hpp:61
uint64_t m_lastSegment
Definition segment-producer.hpp:62
#define NDNPH_ASSERT(x)
Definition common.hpp:30
Definition fs.hpp:33
std::enable_if< std::is_integral< I >::value, I >::type divCeil(const I &a, const I &b)
Compute ceil( a / b ).
Definition operators.hpp:25
Definition segment-producer.hpp:11
int discovery
Name discovery setting.
Definition segment-producer.hpp:30
size_t contentLen
Maximum Content TLV-VALUE in each segment.
Definition segment-producer.hpp:15
uint32_t freshnessPeriod
Definition segment-producer.hpp:17
const PrivateKey & signer
Definition segment-producer.hpp:12