All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
chrono-sync2013.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_CHRONO_SYNC_HPP
24 #define NDN_CHRONO_SYNC_HPP
25 
26 #include <vector>
27 #include "../face.hpp"
28 #include "../security/key-chain.hpp"
29 #include "../util/memory-content-cache.hpp"
30 
31 namespace google { namespace protobuf { template <typename Element> class RepeatedPtrField; } }
32 namespace Sync { class SyncStateMsg; }
33 namespace Sync { class SyncState; }
34 
35 namespace ndn {
36 
37 class DigestTree;
38 
48 public:
49  class SyncState;
50  typedef func_lib::function<void
51  (const std::vector<ChronoSync2013::SyncState>& syncStates, bool isRecovery)>
52  OnReceivedSyncState;
53 
54  typedef func_lib::function<void()> OnInitialized;
55 
110  (const OnReceivedSyncState& onReceivedSyncState,
111  const OnInitialized& onInitialized, const Name& applicationDataPrefix,
112  const Name& applicationBroadcastPrefix, int sessionNo,
113  Face& face, KeyChain& keyChain, const Name& certificateName,
114  Milliseconds syncLifetime, const OnRegisterFailed& onRegisterFailed,
115  int previousSequenceNumber = -1)
116  : impl_(new Impl
117  (onReceivedSyncState, onInitialized, applicationDataPrefix,
118  applicationBroadcastPrefix, sessionNo, face, keyChain, certificateName,
119  syncLifetime, previousSequenceNumber))
120  {
121  impl_->initialize(onRegisterFailed);
122  }
123 
131  class SyncState {
132  public:
133  SyncState
134  (const std::string& dataPrefixUri, int sessionNo, int sequenceNo,
135  const Blob& applicationInfo)
136  : dataPrefixUri_(dataPrefixUri), sessionNo_(sessionNo),
137  sequenceNo_(sequenceNo), applicationInfo_(applicationInfo)
138  {
139  }
140 
145  const std::string&
146  getDataPrefix() const { return dataPrefixUri_; }
147 
152  int
153  getSessionNo() const { return sessionNo_; }
154 
159  int
160  getSequenceNo() const { return sequenceNo_; }
161 
168  const Blob&
169  getApplicationInfo() const { return applicationInfo_; }
170 
171  private:
172  std::string dataPrefixUri_;
173  int sessionNo_;
174  int sequenceNo_;
175  Blob applicationInfo_;
176  };
177 
183  public:
184  PrefixAndSessionNo(const std::string& dataPrefixUri, int sessionNo)
185  : dataPrefixUri_(dataPrefixUri), sessionNo_(sessionNo)
186  {
187  }
188 
193  const std::string&
194  getDataPrefix() const { return dataPrefixUri_; }
195 
201  int
202  getSessionNo() const { return sessionNo_; }
203 
204  private:
205  std::string dataPrefixUri_;
206  int sessionNo_;
207  };
208 
216  void
217  getProducerPrefixes(std::vector<PrefixAndSessionNo>& prefixes) const
218  {
219  impl_->getProducerPrefixes(prefixes);
220  }
221 
230  int
231  getProducerSequenceNo(const std::string& dataPrefix, int sessionNo) const
232  {
233  return impl_->getProducerSequenceNo(dataPrefix, sessionNo);
234  }
235 
254  void
255  publishNextSequenceNo(const Blob& applicationInfo = Blob())
256  {
257  return impl_->publishNextSequenceNo(applicationInfo);
258  }
259 
265  int
267  {
268  return impl_->getSequenceNo();
269  }
270 
281  void
283  {
284  impl_->shutdown();
285  }
286 
287 private:
288  class DigestLogEntry {
289  public:
290  DigestLogEntry
291  (const std::string& digest,
293 
294  const std::string&
295  getDigest() const { return digest_; }
296 
298  getData() const { return *data_; }
299 
300  private:
301  std::string digest_;
302  ptr_lib::shared_ptr<google::protobuf::RepeatedPtrField<Sync::SyncState> > data_;
303  };
304 
310  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
311  public:
317  Impl
318  (const OnReceivedSyncState& onReceivedSyncState,
319  const OnInitialized& onInitialized, const Name& applicationDataPrefix,
320  const Name& applicationBroadcastPrefix, int sessionNo,
321  Face& face, KeyChain& keyChain, const Name& certificateName,
322  Milliseconds syncLifetime, int previousSequenceNumber);
323 
333  void
334  initialize(const OnRegisterFailed& onRegisterFailed);
335 
339  void
340  getProducerPrefixes(std::vector<PrefixAndSessionNo>& prefixes) const;
341 
345  int
346  getProducerSequenceNo(const std::string& dataPrefix, int sessionNo) const;
347 
351  void
352  publishNextSequenceNo(const Blob& applicationInfo);
353 
357  int
358  getSequenceNo() const { return sequenceNo_; }
359 
363  void
364  shutdown()
365  {
366  enabled_ = false;
367  contentCache_.unregisterAll();
368  }
369 
370  private:
378  void
379  broadcastSyncState
380  (const std::string& digest, const Sync::SyncStateMsg& syncMessage);
381 
389  bool
391 
392  // Search the digest log by digest.
393  int
394  logFind(const std::string& digest) const;
395 
401  void
402  onInterest
403  (const ptr_lib::shared_ptr<const Name>& prefix,
404  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
405  uint64_t registerPrefixId,
406  const ptr_lib::shared_ptr<const InterestFilter>& filter);
407 
408  // Process Sync Data.
409  void
410  onData
411  (const ptr_lib::shared_ptr<const Interest>& interest,
412  const ptr_lib::shared_ptr<Data>& data);
413 
414  // Initial sync interest timeout, which means there are no other publishers yet.
415  void
416  initialTimeOut(const ptr_lib::shared_ptr<const Interest>& interest);
417 
418  void
419  processRecoveryInterest
420  (const Interest& interest, const std::string& syncDigest, Face& face);
421 
427  bool
428  processSyncInterest(int index, const std::string& syncDigest, Face& face);
429 
430  // Send Recovery Interest.
431  void
432  sendRecovery(const std::string& syncDigest);
433 
440  void
441  judgeRecovery
442  (const ptr_lib::shared_ptr<const Interest> &interest,
443  const std::string& syncDigest, Face* face);
444 
445  // Sync interest time out, if the interest is the static one send again.
446  void
447  syncTimeout(const ptr_lib::shared_ptr<const Interest>& interest);
448 
449  // Process initial data which usually includes all other publisher's info, and send back the new comer's own info.
450  void
451  initialOndata(const google::protobuf::RepeatedPtrField<Sync::SyncState >& content);
452 
457  static void
458  dummyOnData
459  (const ptr_lib::shared_ptr<const Interest>& interest,
460  const ptr_lib::shared_ptr<Data>& data);
461 
462  Face& face_;
463  KeyChain& keyChain_;
464  Name certificateName_;
465  Milliseconds syncLifetime_;
466  OnReceivedSyncState onReceivedSyncState_;
467  OnInitialized onInitialized_;
468  std::vector<ptr_lib::shared_ptr<DigestLogEntry> > digestLog_;
469  ptr_lib::shared_ptr<DigestTree> digestTree_;
470  std::string applicationDataPrefixUri_;
471  const Name applicationBroadcastPrefix_;
472  int sessionNo_;
473  int initialPreviousSequenceNo_;
474  int sequenceNo_;
475  MemoryContentCache contentCache_;
476  bool enabled_;
477  };
478 
479  ptr_lib::shared_ptr<Impl> impl_;
480 };
481 
482 }
483 
484 #endif
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:114
int getProducerSequenceNo(const std::string &dataPrefix, int sessionNo) const
Get the current sequence number in the digest tree for the given producer dataPrefix and sessionNo...
Definition: chrono-sync2013.hpp:231
const Blob & getApplicationInfo() const
Get the application info which was included when the sender published the next sequence number...
Definition: chrono-sync2013.hpp:169
void shutdown()
Unregister callbacks so that this does not respond to interests anymore.
Definition: chrono-sync2013.hpp:282
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
Definition: chrono-sync2013.hpp:31
KeyChain is the main class of the security library.
Definition: key-chain.hpp:53
ChronoSync2013(const OnReceivedSyncState &onReceivedSyncState, const OnInitialized &onInitialized, const Name &applicationDataPrefix, const Name &applicationBroadcastPrefix, int sessionNo, Face &face, KeyChain &keyChain, const Name &certificateName, Milliseconds syncLifetime, const OnRegisterFailed &onRegisterFailed, int previousSequenceNumber=-1)
Create a new ChronoSync2013 to communicate using the given face.
Definition: chrono-sync2013.hpp:110
int getSequenceNo() const
Get the sequence number of the latest data published by this application instance.
Definition: chrono-sync2013.hpp:266
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
ChronoSync2013 implements the NDN ChronoSync protocol as described in the 2013 paper "Let's ChronoSy...
Definition: chrono-sync2013.hpp:47
const std::string & getDataPrefix() const
Get the application data prefix.
Definition: chrono-sync2013.hpp:146
int getSequenceNo() const
Get the sequence number for this sync state message.
Definition: chrono-sync2013.hpp:160
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &prefix)> OnRegisterFailed
An OnRegisterFailed function object is used to report when registerPrefix fails.
Definition: face.hpp:71
A PrefixAndSessionNo holds a user's data prefix and session number (used to return a list from getPro...
Definition: chrono-sync2013.hpp:182
const std::string & getDataPrefix() const
Get the application data prefix for this sync state message.
Definition: chrono-sync2013.hpp:194
void getProducerPrefixes(std::vector< PrefixAndSessionNo > &prefixes) const
Get a copy of the current list of producer data prefixes, and the associated session number...
Definition: chrono-sync2013.hpp:217
A SyncState holds the values of a sync state message which is passed to the onReceivedSyncState callb...
Definition: chrono-sync2013.hpp:131
int getSessionNo() const
Get the session number associated with the application data prefix for this sync state message...
Definition: chrono-sync2013.hpp:202
int getSessionNo() const
Get the session number associated with the application data prefix.
Definition: chrono-sync2013.hpp:153
void publishNextSequenceNo(const Blob &applicationInfo=Blob())
Increment the sequence number, create a sync message with the new sequence number and publish a data ...
Definition: chrono-sync2013.hpp:255