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 #include "../security/key-chain.hpp"
28 
29 namespace ndn {
30 
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>& data)> VerifySegment;
96 
97  typedef func_lib::function<void(const Blob& content)> 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 
137  static void
138  fetch
139  (Face& face, const Interest &baseInterest, const VerifySegment& verifySegment,
140  const OnComplete& onComplete, const OnError& onError);
141 
169  static void
170  fetch
171  (Face& face, const Interest &baseInterest, KeyChain* validatorKeyChain,
172  const OnComplete& onComplete, const OnError& onError);
173 
174 private:
181  (Face& face, KeyChain* validatorKeyChain, const VerifySegment& verifySegment,
182  const OnComplete& onComplete, const OnError& onError)
183  : face_(face), validatorKeyChain_(validatorKeyChain), verifySegment_(verifySegment),
184  onComplete_(onComplete), onError_(onError)
185  {
186  }
187 
188  void
189  fetchFirstSegment(const Interest& baseInterest);
190 
191  void
192  fetchNextSegment
193  (const Interest& originalInterest, const Name& dataName, uint64_t segment);
194 
195  void
196  onSegmentReceived
197  (const ptr_lib::shared_ptr<const Interest>& originalInterest,
198  const ptr_lib::shared_ptr<Data>& data);
199 
200  void
201  onVerified
202  (const ptr_lib::shared_ptr<Data>& data,
203  const ptr_lib::shared_ptr<const Interest>& originalInterest);
204 
205  void
206  onValidationFailed
207  (const ptr_lib::shared_ptr<Data>& data, const std::string& reason);
208 
209  void
210  onTimeout(const ptr_lib::shared_ptr<const Interest>& interest);
211 
217  static bool
218  endsWithSegmentNumber(const Name& name)
219  {
220  return name.size() >= 1 && name.get(-1).isSegment();
221  }
222 
223  std::vector<Blob> contentParts_;
224  Face& face_;
225  KeyChain* validatorKeyChain_;
226  VerifySegment verifySegment_;
227  OnComplete onComplete_;
228  OnError onError_;
229 };
230 
231 }
232 
233 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
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:36
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
static void fetch(Face &face, const Interest &baseInterest, const VerifySegment &verifySegment, const OnComplete &onComplete, const OnError &onError)
Initiate segment fetching.
Definition: segment-fetcher.cpp:43
SegmentFetcher is a utility class to the fetch latest version of segmented data.
Definition: segment-fetcher.hpp:87
KeyChain is the main class of the security library.
Definition: key-chain.hpp:45
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
size_t size() const
Get the number of components.
Definition: name.hpp:1129
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:42
void get(NameLite &nameLite) const
Set nameLite to point to the components in this name, without copying any memory. ...
Definition: name.cpp:363