segment-fetcher.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_SEGMENT_FETCHER_HPP
24 #define NDN_SEGMENT_FETCHER_HPP
25 
26 #include "../face.hpp"
27 
28 namespace ndn {
29 
87 class SegmentFetcher : public ptr_lib::enable_shared_from_this<SegmentFetcher> {
88 public:
89  enum ErrorCode {
90  INTEREST_TIMEOUT = 1,
91  DATA_HAS_NO_SEGMENT = 2,
92  SEGMENT_VERIFICATION_FAILED = 3
93  };
94 
95  typedef func_lib::function<bool(const ptr_lib::shared_ptr<Data>&)> VerifySegment;
96 
97  typedef func_lib::function<void(const Blob&)> OnComplete;
98 
99  typedef func_lib::function<void
100  (ErrorCode errorCode, const std::string& message)> OnError;
101 
105  static bool
106  DontVerifySegment(const ptr_lib::shared_ptr<Data>& data);
107 
128  static void
129  fetch
130  (Face& face, const Interest &baseInterest, const VerifySegment& verifySegment,
131  const OnComplete& onComplete, const OnError& onError);
132 
133 private:
147  (Face& face, const VerifySegment& verifySegment, const OnComplete& onComplete,
148  const OnError& onError)
149  : face_(face), verifySegment_(verifySegment), onComplete_(onComplete),
150  onError_(onError)
151  {
152  }
153 
154  void
155  fetchFirstSegment(const Interest& baseInterest);
156 
157  void
158  fetchNextSegment
159  (const Interest& originalInterest, const Name& dataName, uint64_t segment);
160 
161  void
162  onSegmentReceived
163  (const ptr_lib::shared_ptr<const Interest>& originalInterest,
164  const ptr_lib::shared_ptr<Data>& data);
165 
166  void
167  onTimeout(const ptr_lib::shared_ptr<const Interest>& interest);
168 
174  static bool
175  endsWithSegmentNumber(Name name)
176  {
177  return name.size() >= 1 &&
178  name.get(-1).getValue().size() >= 1 &&
179  name.get(-1).getValue().buf()[0] == 0;
180  }
181 
182  std::vector<Blob> contentParts_;
183  Face& face_;
184  VerifySegment verifySegment_;
185  OnComplete onComplete_;
186  OnError onError_;
187 };
188 
189 }
190 
191 #endif
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
static bool DontVerifySegment(const ptr_lib::shared_ptr< Data > &data)
DontVerifySegment may be used in fetch to skip validation of Data packets.
Definition: segment-fetcher.cpp:33
The Face class provides the main methods for NDN communication.
Definition: face.hpp:72
void get(struct ndn_Name &nameStruct) const
Set the nameStruct to point to the components in this name, without copying any memory.
Definition: name.cpp:292
static void fetch(Face &face, const Interest &baseInterest, const VerifySegment &verifySegment, const OnComplete &onComplete, const OnError &onError)
Initiate segment fetching.
Definition: segment-fetcher.cpp:40
SegmentFetcher is a utility class to the fetch latest version of segmented data.
Definition: segment-fetcher.hpp:87
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:42
size_t size() const
Get the number of components.
Definition: name.hpp:813
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:41