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 
106  (const OnReceivedSyncState& onReceivedSyncState,
107  const OnInitialized& onInitialized, const Name& applicationDataPrefix,
108  const Name& applicationBroadcastPrefix, int sessionNo,
109  Face& face, KeyChain& keyChain, const Name& certificateName,
110  Milliseconds syncLifetime, const OnRegisterFailed& onRegisterFailed)
111  : impl_(new Impl
112  (onReceivedSyncState, onInitialized, applicationDataPrefix,
113  applicationBroadcastPrefix, sessionNo, face, keyChain, certificateName,
114  syncLifetime))
115  {
116  impl_->initialize(onRegisterFailed);
117  }
118 
126  class SyncState {
127  public:
128  SyncState(const std::string& dataPrefixUri, int sessionNo, int sequenceNo)
129  : dataPrefixUri_(dataPrefixUri), sessionNo_(sessionNo), sequenceNo_(sequenceNo)
130  {
131  }
132 
137  const std::string&
138  getDataPrefix() const { return dataPrefixUri_; }
139 
145  int
146  getSessionNo() const { return sessionNo_; }
147 
152  int
153  getSequenceNo() const { return sequenceNo_; }
154 
155  private:
156  std::string dataPrefixUri_;
157  int sessionNo_;
158  int sequenceNo_;
159  };
160 
169  int
170  getProducerSequenceNo(const std::string& dataPrefix, int sessionNo) const
171  {
172  return impl_->getProducerSequenceNo(dataPrefix, sessionNo);
173  }
174 
189  void
191  {
192  return impl_->publishNextSequenceNo();
193  }
194 
200  int
202  {
203  return impl_->getSequenceNo();
204  }
205 
216  void
218  {
219  impl_->shutdown();
220  }
221 
222 private:
223  class DigestLogEntry {
224  public:
225  DigestLogEntry
226  (const std::string& digest,
228 
229  const std::string&
230  getDigest() const { return digest_; }
231 
233  getData() const { return *data_; }
234 
235  private:
236  std::string digest_;
237  ptr_lib::shared_ptr<google::protobuf::RepeatedPtrField<Sync::SyncState> > data_;
238  };
239 
245  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
246  public:
252  Impl
253  (const OnReceivedSyncState& onReceivedSyncState,
254  const OnInitialized& onInitialized, const Name& applicationDataPrefix,
255  const Name& applicationBroadcastPrefix, int sessionNo,
256  Face& face, KeyChain& keyChain, const Name& certificateName,
257  Milliseconds syncLifetime);
258 
268  void
269  initialize(const OnRegisterFailed& onRegisterFailed);
270 
274  int
275  getProducerSequenceNo(const std::string& dataPrefix, int sessionNo) const;
276 
280  void
282 
286  int
287  getSequenceNo() const { return sequenceNo_; }
288 
292  void
293  shutdown()
294  {
295  enabled_ = false;
296  contentCache_.unregisterAll();
297  }
298 
299  private:
307  void
308  broadcastSyncState
309  (const std::string& digest, const Sync::SyncStateMsg& syncMessage);
310 
318  bool
320 
321  // Search the digest log by digest.
322  int
323  logFind(const std::string& digest) const;
324 
330  void
331  onInterest
332  (const ptr_lib::shared_ptr<const Name>& prefix,
333  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
334  uint64_t registerPrefixId,
335  const ptr_lib::shared_ptr<const InterestFilter>& filter);
336 
337  // Process Sync Data.
338  void
339  onData
340  (const ptr_lib::shared_ptr<const Interest>& interest,
341  const ptr_lib::shared_ptr<Data>& data);
342 
343  // Initial sync interest timeout, which means there are no other publishers yet.
344  void
345  initialTimeOut(const ptr_lib::shared_ptr<const Interest>& interest);
346 
347  void
348  processRecoveryInterest
349  (const Interest& interest, const std::string& syncDigest, Face& face);
350 
356  bool
357  processSyncInterest(int index, const std::string& syncDigest, Face& face);
358 
359  // Send Recovery Interest.
360  void
361  sendRecovery(const std::string& syncDigest);
362 
369  void
370  judgeRecovery
371  (const ptr_lib::shared_ptr<const Interest> &interest,
372  const std::string& syncDigest, Face* face);
373 
374  // Sync interest time out, if the interest is the static one send again.
375  void
376  syncTimeout(const ptr_lib::shared_ptr<const Interest>& interest);
377 
378  // Process initial data which usually includes all other publisher's info, and send back the new comer's own info.
379  void
380  initialOndata(const google::protobuf::RepeatedPtrField<Sync::SyncState >& content);
381 
386  static void
387  dummyOnData
388  (const ptr_lib::shared_ptr<const Interest>& interest,
389  const ptr_lib::shared_ptr<Data>& data);
390 
391  Face& face_;
392  KeyChain& keyChain_;
393  Name certificateName_;
394  Milliseconds syncLifetime_;
395  OnReceivedSyncState onReceivedSyncState_;
396  OnInitialized onInitialized_;
397  std::vector<ptr_lib::shared_ptr<DigestLogEntry> > digestLog_;
398  ptr_lib::shared_ptr<DigestTree> digestTree_;
399  std::string applicationDataPrefixUri_;
400  const Name applicationBroadcastPrefix_;
401  int sessionNo_;
402  int sequenceNo_;
403  MemoryContentCache contentCache_;
404  bool enabled_;
405  };
406 
407  ptr_lib::shared_ptr<Impl> impl_;
408 };
409 
410 }
411 
412 #endif
Definition: sync-state.pb.h:290
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
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:170
void shutdown()
Unregister callbacks so that this does not respond to interests anymore.
Definition: chrono-sync2013.hpp:217
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)
Create a new ChronoSync2013 to communicate using the given face.
Definition: chrono-sync2013.hpp:106
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
Definition: chrono-sync2013.hpp:32
void publishNextSequenceNo()
Increment the sequence number, create a sync message with the new sequence number and publish a data ...
Definition: chrono-sync2013.hpp:190
Definition: chrono-sync2013.hpp:31
KeyChain is the main class of the security library.
Definition: key-chain.hpp:45
int getSequenceNo() const
Get the sequence number of the latest data published by this application instance.
Definition: chrono-sync2013.hpp:201
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
ChronoSync2013 implements the NDN ChronoSync protocol as described in the 2013 paper "Let's ChronoSyn...
Definition: chrono-sync2013.hpp:47
const std::string & getDataPrefix() const
Get the application data prefix for this sync state message.
Definition: chrono-sync2013.hpp:138
int getSequenceNo() const
Get the sequence number for this sync state message.
Definition: chrono-sync2013.hpp:153
Copyright (C) 2014-2016 Regents of the University of California.
Definition: channel-status.pb.h:315
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &)> OnRegisterFailed
An OnRegisterFailed function object is used to report when registerPrefix fails.
Definition: face.hpp:72
A SyncState holds the values of a sync state message which is passed to the onReceivedSyncState callb...
Definition: chrono-sync2013.hpp:126
int getSessionNo() const
Get the session number associated with the application data prefix for this sync state message...
Definition: chrono-sync2013.hpp:146