NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
segment-fetcher.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "segment-fetcher.hpp"
23 
24 #include "../encoding/buffer-stream.hpp"
25 
26 namespace ndn {
27 namespace util {
28 
29 SegmentFetcher::SegmentFetcher(Face& face,
30  const VerifySegment& verifySegment,
31  const CompleteCallback& completeCallback,
32  const ErrorCallback& errorCallback)
33  : m_face(face)
34  , m_verifySegment(verifySegment)
35  , m_completeCallback(completeCallback)
36  , m_errorCallback(errorCallback)
37  , m_buffer(make_shared<OBufferStream>())
38 {
39 }
40 
41 void
43  const Interest& baseInterest,
44  const VerifySegment& verifySegment,
45  const CompleteCallback& completeCallback,
46  const ErrorCallback& errorCallback)
47 {
48  shared_ptr<SegmentFetcher> fetcher =
49  shared_ptr<SegmentFetcher>(new SegmentFetcher(face, verifySegment,
50  completeCallback, errorCallback));
51 
52  fetcher->fetchFirstSegment(baseInterest, fetcher);
53 }
54 
55 void
56 SegmentFetcher::fetchFirstSegment(const Interest& baseInterest,
57  const shared_ptr<SegmentFetcher>& self)
58 {
59  Interest interest(baseInterest);
60  interest.setChildSelector(1);
61  interest.setMustBeFresh(true);
62 
63  m_face.expressInterest(interest,
64  bind(&SegmentFetcher::onSegmentReceived, this, _1, _2, true, self),
65  bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout"));
66 }
67 
68 void
69 SegmentFetcher::fetchNextSegment(const Interest& origInterest, const Name& dataName,
70  uint64_t segmentNo,
71  const shared_ptr<SegmentFetcher>& self)
72 {
73  Interest interest(origInterest); // to preserve any special selectors
74  interest.refreshNonce();
75  interest.setChildSelector(0);
76  interest.setMustBeFresh(false);
77  interest.setName(dataName.getPrefix(-1).appendSegment(segmentNo));
78  m_face.expressInterest(interest,
79  bind(&SegmentFetcher::onSegmentReceived, this, _1, _2, false, self),
80  bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout"));
81 }
82 
83 void
84 SegmentFetcher::onSegmentReceived(const Interest& origInterest,
85  const Data& data, bool isSegmentZeroExpected,
86  const shared_ptr<SegmentFetcher>& self)
87 {
88  if (!m_verifySegment(data)) {
89  return m_errorCallback(SEGMENT_VERIFICATION_FAIL, "Segment validation fail");
90  }
91 
92  try {
93  uint64_t currentSegment = data.getName().get(-1).toSegment();
94 
95  if (isSegmentZeroExpected && currentSegment != 0) {
96  fetchNextSegment(origInterest, data.getName(), 0, self);
97  }
98  else {
99  m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
100  data.getContent().value_size());
101 
102  const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
103  if (finalBlockId.empty() ||
104  finalBlockId.toSegment() > currentSegment)
105  {
106  fetchNextSegment(origInterest, data.getName(), currentSegment + 1, self);
107  }
108  else {
109  return m_completeCallback(m_buffer->buf());
110  }
111  }
112  }
113  catch (const tlv::Error& e) {
114  m_errorCallback(DATA_HAS_NO_SEGMENT, std::string("Error while decoding segment: ") + e.what());
115  }
116 }
117 
118 } // util
119 } // ndn
Copyright (c) 2011-2015 Regents of the University of California.
Interest & setMustBeFresh(bool mustBeFresh)
Definition: interest.hpp:418
function< void(uint32_t code, const std::string &msg)> ErrorCallback
void refreshNonce()
Refresh nonce.
Definition: interest.cpp:91
Utility class to fetch latest version of the segmented data.
function< bool(const Data &data)> VerifySegment
represents an Interest packet
Definition: interest.hpp:45
const MetaInfo & getMetaInfo() const
Get MetaInfo block from Data packet.
Definition: data.hpp:349
function< void(const std::string &reason)> ErrorCallback
Definition: dns.hpp:72
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
Interest & setChildSelector(int childSelector)
Definition: interest.hpp:404
Interest & setName(const Name &name)
Definition: interest.hpp:222
Name & appendSegment(uint64_t segmentNo)
Append segment number (sequential) using NDN naming conventions.
Definition: name.cpp:232
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:100
Name abstraction to represent an absolute name.
Definition: name.hpp:46
ndn cxx Face
Copyright (c) 2013-2015 Regents of the University of California.
Definition: face.cpp:25
size_t value_size() const
Definition: block.cpp:529
Component holds a read-only name component value.
const Block & getContent() const
Get content Block.
Definition: data.cpp:230
const uint8_t * value() const
Definition: block.cpp:520
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:249
static void fetch(Face &face, const Interest &baseInterest, const VerifySegment &verifySegment, const CompleteCallback &completeCallback, const ErrorCallback &errorCallback)
Initiate segment fetching.
represents a Data packet
Definition: data.hpp:39
const name::Component & getFinalBlockId() const
Definition: meta-info.hpp:214
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
function< void(const ConstBufferPtr &data)> CompleteCallback