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 
97  (const OnReceivedSyncState& onReceivedSyncState,
98  const OnInitialized& onInitialized, const Name& applicationDataPrefix,
99  const Name& applicationBroadcastPrefix, int sessionNo,
100  Face& face, KeyChain& keyChain, const Name& certificateName,
101  Milliseconds syncLifetime, const OnRegisterFailed& onRegisterFailed)
102  : impl_(new Impl
103  (onReceivedSyncState, onInitialized, applicationDataPrefix,
104  applicationBroadcastPrefix, sessionNo, face, keyChain, certificateName,
105  syncLifetime))
106  {
107  impl_->initialize(onRegisterFailed);
108  }
109 
117  class SyncState {
118  public:
119  SyncState(const std::string& dataPrefixUri, int sessionNo, int sequenceNo)
120  : dataPrefixUri_(dataPrefixUri), sessionNo_(sessionNo), sequenceNo_(sequenceNo)
121  {
122  }
123 
128  const std::string&
129  getDataPrefix() const { return dataPrefixUri_; }
130 
136  int
137  getSessionNo() const { return sessionNo_; }
138 
143  int
144  getSequenceNo() const { return sequenceNo_; }
145 
146  private:
147  std::string dataPrefixUri_;
148  int sessionNo_;
149  int sequenceNo_;
150  };
151 
160  int
161  getProducerSequenceNo(const std::string& dataPrefix, int sessionNo) const
162  {
163  return impl_->getProducerSequenceNo(dataPrefix, sessionNo);
164  }
165 
180  void
182  {
183  return impl_->publishNextSequenceNo();
184  }
185 
191  int
193  {
194  return impl_->getSequenceNo();
195  }
196 
207  void
209  {
210  impl_->shutdown();
211  }
212 
213 private:
214  class DigestLogEntry {
215  public:
216  DigestLogEntry
217  (const std::string& digest,
219 
220  const std::string&
221  getDigest() const { return digest_; }
222 
224  getData() const { return *data_; }
225 
226  private:
227  std::string digest_;
228  ptr_lib::shared_ptr<google::protobuf::RepeatedPtrField<Sync::SyncState> > data_;
229  };
230 
236  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
237  public:
243  Impl
244  (const OnReceivedSyncState& onReceivedSyncState,
245  const OnInitialized& onInitialized, const Name& applicationDataPrefix,
246  const Name& applicationBroadcastPrefix, int sessionNo,
247  Face& face, KeyChain& keyChain, const Name& certificateName,
248  Milliseconds syncLifetime);
249 
259  void
260  initialize(const OnRegisterFailed& onRegisterFailed);
261 
265  int
266  getProducerSequenceNo(const std::string& dataPrefix, int sessionNo) const;
267 
271  void
273 
277  int
278  getSequenceNo() const { return sequenceNo_; }
279 
283  void
284  shutdown()
285  {
286  enabled_ = false;
287  contentCache_.unregisterAll();
288  }
289 
290  private:
298  void
299  broadcastSyncState
300  (const std::string& digest, const Sync::SyncStateMsg& syncMessage);
301 
309  bool
311 
312  // Search the digest log by digest.
313  int
314  logFind(const std::string& digest) const;
315 
321  void
322  onInterest
323  (const ptr_lib::shared_ptr<const Name>& prefix,
324  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
325  uint64_t registerPrefixId,
326  const ptr_lib::shared_ptr<const InterestFilter>& filter);
327 
328  // Process Sync Data.
329  void
330  onData
331  (const ptr_lib::shared_ptr<const Interest>& interest,
332  const ptr_lib::shared_ptr<Data>& data);
333 
334  // Initial sync interest timeout, which means there are no other publishers yet.
335  void
336  initialTimeOut(const ptr_lib::shared_ptr<const Interest>& interest);
337 
338  void
339  processRecoveryInterest
340  (const Interest& interest, const std::string& syncDigest, Face& face);
341 
347  bool
348  processSyncInterest(int index, const std::string& syncDigest, Face& face);
349 
350  // Send Recovery Interest.
351  void
352  sendRecovery(const std::string& syncDigest);
353 
360  void
361  judgeRecovery
362  (const ptr_lib::shared_ptr<const Interest> &interest,
363  const std::string& syncDigest, Face* face);
364 
365  // Sync interest time out, if the interest is the static one send again.
366  void
367  syncTimeout(const ptr_lib::shared_ptr<const Interest>& interest);
368 
369  // Process initial data which usually includes all other publisher's info, and send back the new comer's own info.
370  void
371  initialOndata(const google::protobuf::RepeatedPtrField<Sync::SyncState >& content);
372 
377  static void
378  dummyOnData
379  (const ptr_lib::shared_ptr<const Interest>& interest,
380  const ptr_lib::shared_ptr<Data>& data);
381 
382  Face& face_;
383  KeyChain& keyChain_;
384  Name certificateName_;
385  Milliseconds syncLifetime_;
386  OnReceivedSyncState onReceivedSyncState_;
387  OnInitialized onInitialized_;
388  std::vector<ptr_lib::shared_ptr<DigestLogEntry> > digestLog_;
389  ptr_lib::shared_ptr<DigestTree> digestTree_;
390  std::string applicationDataPrefixUri_;
391  const Name applicationBroadcastPrefix_;
392  int sessionNo_;
393  int sequenceNo_;
394  MemoryContentCache contentCache_;
395  bool enabled_;
396  };
397 
398  ptr_lib::shared_ptr<Impl> impl_;
399 };
400 
401 }
402 
403 #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-2015 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:161
void shutdown()
Unregister callbacks so that this does not respond to interests anymore.
Definition: chrono-sync2013.hpp:208
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:97
The Face class provides the main methods for NDN communication.
Definition: face.hpp:72
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:181
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:192
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:42
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:129
int getSequenceNo() const
Get the sequence number for this sync state message.
Definition: chrono-sync2013.hpp:144
Copyright (C) 2014-2015 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:64
A SyncState holds the values of a sync state message which is passed to the onReceivedSyncState callb...
Definition: chrono-sync2013.hpp:117
int getSessionNo() const
Get the session number associated with the application data prefix for this sync state message...
Definition: chrono-sync2013.hpp:137