sync-protocol-adapter.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 #include "logger.hpp"
24 
25 INIT_LOGGER(SyncProtocolAdapter);
26 
27 namespace nlsr {
28 
29 const auto FIXED_SESSION = ndn::name::Component::fromNumber(0);
30 
32  int32_t syncProtocol,
33  const ndn::Name& syncPrefix,
34  const ndn::Name& userPrefix,
35  ndn::time::milliseconds syncInterestLifetime,
36  const SyncUpdateCallback& syncUpdateCallback)
37  : m_syncProtocol(syncProtocol)
38  , m_syncUpdateCallback(syncUpdateCallback)
39 {
40  if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
41  NDN_LOG_DEBUG("Using ChronoSync");
42  m_chronoSyncLogic = std::make_shared<chronosync::Logic>(face,
43  syncPrefix,
44  userPrefix,
45  std::bind(&SyncProtocolAdapter::onChronoSyncUpdate, this, _1),
46  chronosync::Logic::DEFAULT_NAME,
47  chronosync::Logic::DEFAULT_VALIDATOR,
48  chronosync::Logic::DEFAULT_RESET_TIMER,
49  chronosync::Logic::DEFAULT_CANCEL_RESET_TIMER,
50  chronosync::Logic::DEFAULT_RESET_INTEREST_LIFETIME,
51  syncInterestLifetime,
52  chronosync::Logic::DEFAULT_SYNC_REPLY_FRESHNESS,
53  chronosync::Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME,
55  }
56  else {
57  NDN_LOG_DEBUG("Using PSync");
58  m_psyncLogic = std::make_shared<psync::FullProducer>(80,
59  face,
60  syncPrefix,
61  userPrefix,
62  std::bind(&SyncProtocolAdapter::onPSyncUpdate, this, _1),
63  syncInterestLifetime);
64  }
65 }
66 
67 void
68 SyncProtocolAdapter::addUserNode(const ndn::Name& userPrefix)
69 {
70  if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
71  m_chronoSyncLogic->addUserNode(userPrefix, chronosync::Logic::DEFAULT_NAME, FIXED_SESSION);
72  }
73  else {
74  m_psyncLogic->addUserNode(userPrefix);
75  }
76 }
77 
78 void
79 SyncProtocolAdapter::publishUpdate(const ndn::Name& userPrefix, uint64_t seq)
80 {
81  if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
82  m_chronoSyncLogic->updateSeqNo(seq, userPrefix);
83  }
84  else {
85  m_psyncLogic->publishName(userPrefix, seq);
86  }
87 }
88 
89 void
90 SyncProtocolAdapter::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates)
91 {
92  NLSR_LOG_TRACE("Received ChronoSync update event");
93 
94  for (const auto& update : updates) {
95  // Remove FIXED_SESSION
96  m_syncUpdateCallback(update.session.getPrefix(-1), update.high);
97  }
98 }
99 
100 void
101 SyncProtocolAdapter::onPSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
102 {
103  NLSR_LOG_TRACE("Received PSync update event");
104 
105  for (const auto& update : updates) {
106  m_syncUpdateCallback(update.prefix, update.highSeq);
107  }
108 }
109 
110 } // namespace nlsr
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
const auto FIXED_SESSION
std::function< void(const ndn::Name &updateName, uint64_t seqNo)> SyncUpdateCallback
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void addUserNode(const ndn::Name &userPrefix)
Add user node to ChronoSync or PSync.
SyncProtocolAdapter(ndn::Face &facePtr, int32_t syncProtocol, const ndn::Name &syncPrefix, const ndn::Name &userPrefix, ndn::time::milliseconds syncInterestLifetime, const SyncUpdateCallback &syncUpdateCallback)
void publishUpdate(const ndn::Name &userPrefix, uint64_t seq)
Publish update to ChronoSync or PSync.
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:37