24 #include "../encoding/buffer-stream.hpp" 29 SegmentFetcher::SegmentFetcher(
Face& face,
30 const VerifySegment& verifySegment,
31 const CompleteCallback& completeCallback,
34 , m_verifySegment(verifySegment)
35 , m_completeCallback(completeCallback)
36 , m_errorCallback(errorCallback)
37 , m_buffer(make_shared<OBufferStream>())
48 shared_ptr<SegmentFetcher> fetcher =
50 completeCallback, errorCallback));
52 fetcher->fetchFirstSegment(baseInterest, fetcher);
56 SegmentFetcher::fetchFirstSegment(
const Interest& baseInterest,
57 const shared_ptr<SegmentFetcher>&
self)
63 m_face.expressInterest(interest,
64 bind(&SegmentFetcher::onSegmentReceived,
this, _1, _2,
true,
self),
65 bind(m_errorCallback, INTEREST_TIMEOUT,
"Timeout"));
69 SegmentFetcher::fetchNextSegment(
const Interest& origInterest,
const Name& dataName,
71 const shared_ptr<SegmentFetcher>&
self)
78 m_face.expressInterest(interest,
79 bind(&SegmentFetcher::onSegmentReceived,
this, _1, _2,
false,
self),
80 bind(m_errorCallback, INTEREST_TIMEOUT,
"Timeout"));
84 SegmentFetcher::onSegmentReceived(
const Interest& origInterest,
85 const Data& data,
bool isSegmentZeroExpected,
86 const shared_ptr<SegmentFetcher>&
self)
88 if (!m_verifySegment(data)) {
89 return m_errorCallback(SEGMENT_VERIFICATION_FAIL,
"Segment validation fail");
95 if (isSegmentZeroExpected && currentSegment != 0) {
96 fetchNextSegment(origInterest, data.
getName(), 0,
self);
99 m_buffer->write(reinterpret_cast<const char*>(data.
getContent().
value()),
103 if (finalBlockId.
empty() ||
104 finalBlockId.
toSegment() > currentSegment)
106 fetchNextSegment(origInterest, data.
getName(), currentSegment + 1,
self);
109 return m_completeCallback(m_buffer->buf());
114 m_errorCallback(DATA_HAS_NO_SEGMENT, std::string(
"Error while decoding segment: ") + e.what());
Copyright (c) 2011-2015 Regents of the University of California.
Interest & setMustBeFresh(bool mustBeFresh)
function< void(uint32_t code, const std::string &msg)> ErrorCallback
void refreshNonce()
Refresh nonce.
Utility class to fetch latest version of the segmented data.
function< bool(const Data &data)> VerifySegment
represents an Interest packet
const MetaInfo & getMetaInfo() const
Get MetaInfo block from Data packet.
function< void(const std::string &reason)> ErrorCallback
const Name & getName() const
Get name of the Data packet.
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
Interest & setChildSelector(int childSelector)
Interest & setName(const Name &name)
Name & appendSegment(uint64_t segmentNo)
Append segment number (sequential) using NDN naming conventions.
Abstraction to communicate with local or remote NDN forwarder.
Name abstraction to represent an absolute name.
ndn cxx Face
Copyright (c) 2013-2015 Regents of the University of California.
size_t value_size() const
Component holds a read-only name component value.
const Block & getContent() const
Get content Block.
const uint8_t * value() const
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
static void fetch(Face &face, const Interest &baseInterest, const VerifySegment &verifySegment, const CompleteCallback &completeCallback, const ErrorCallback &errorCallback)
Initiate segment fetching.
const Component & get(ssize_t i) const
Get the component at the given index.
represents an error in TLV encoding or decoding
function< void(const ConstBufferPtr &data)> CompleteCallback