communication: adapt to API changes in PSync

Pass the KeyChain instance to psync::FullProducer

Change-Id: Ia97356dc364c7bdaf6dc80ad57fa351eeabe997d
diff --git a/src/communication/sync-logic-handler.cpp b/src/communication/sync-logic-handler.cpp
index 3e29067..cd9e17a 100644
--- a/src/communication/sync-logic-handler.cpp
+++ b/src/communication/sync-logic-handler.cpp
@@ -20,9 +20,7 @@
  */
 
 #include "sync-logic-handler.hpp"
-#include "common.hpp"
-#include "conf-parameter.hpp"
-#include "lsa/lsa.hpp"
+#include "hello-protocol.hpp"
 #include "logger.hpp"
 #include "utility/name-helper.hpp"
 
@@ -32,14 +30,14 @@
 
 INIT_LOGGER(SyncLogicHandler);
 
-SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew,
-                                   const ConfParameter& conf)
-  : onNewLsa(std::make_unique<OnNewLsa>())
-  , m_syncFace(face)
-  , m_isLsaNew(isLsaNew)
+const std::string LSA_COMPONENT{"LSA"};
+
+SyncLogicHandler::SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
+                                   IsLsaNew isLsaNew, const ConfParameter& conf)
+  : m_isLsaNew(std::move(isLsaNew))
   , m_confParam(conf)
   , m_nameLsaUserPrefix(ndn::Name(m_confParam.getSyncUserPrefix()).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)))
-  , m_syncLogic(m_syncFace, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
+  , m_syncLogic(face, keyChain, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
                 m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
                 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2, _3))
 {
@@ -62,7 +60,7 @@
 {
   NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
 
-  int32_t nlsrPosition = util::getNameComponentPosition(updateName, NLSR_COMPONENT);
+  int32_t nlsrPosition = util::getNameComponentPosition(updateName, HelloProtocol::NLSR_COMPONENT);
   int32_t lsaPosition = util::getNameComponentPosition(updateName, LSA_COMPONENT);
 
   if (nlsrPosition < 0 || lsaPosition < 0) {
@@ -70,9 +68,8 @@
     return;
   }
 
-  ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
+  ndn::Name networkName = updateName.getSubName(1, nlsrPosition - 1);
   ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
-
   ndn::Name originRouter = networkName;
   originRouter.append(routerName);
 
@@ -109,7 +106,8 @@
                        "is enabled. Not going to fetch.");
         return;
       }
-      (*onNewLsa)(updateName, seqNo, originRouter, incomingFaceId);
+
+      onNewLsa(updateName, seqNo, originRouter, incomingFaceId);
     }
   }
 }
diff --git a/src/communication/sync-logic-handler.hpp b/src/communication/sync-logic-handler.hpp
index 9f3e5de..6eee213 100644
--- a/src/communication/sync-logic-handler.hpp
+++ b/src/communication/sync-logic-handler.hpp
@@ -23,25 +23,19 @@
 #define NLSR_SYNC_LOGIC_HANDLER_HPP
 
 #include "conf-parameter.hpp"
-#include "test-access-control.hpp"
-#include "signals.hpp"
 #include "lsa/lsa.hpp"
+#include "signals.hpp"
 #include "sync-protocol-adapter.hpp"
-
-#include <ndn-cxx/face.hpp>
-#include <ndn-cxx/util/signal.hpp>
+#include "test-access-control.hpp"
 
 namespace nlsr {
 
-class ConfParameter;
-
 /*! \brief NLSR-to-ChronoSync interaction point
  *
  * This class serves as the abstraction for the syncing portion of
  * NLSR and its components. NLSR has no particular reliance on
  * ChronoSync, except that the NLSR source would need to be modified
  * for use with other sync protocols.
- *
  */
 class SyncLogicHandler
 {
@@ -55,7 +49,8 @@
   using IsLsaNew =
     std::function<bool(const ndn::Name&, const Lsa::Type& lsaType, const uint64_t&, uint64_t/*inFace*/)>;
 
-  SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew, const ConfParameter& conf);
+  SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
+                   IsLsaNew isLsaNew, const ConfParameter& conf);
 
   /*! \brief Instruct ChronoSync to publish an update.
    *
@@ -92,10 +87,9 @@
                         const ndn::Name& updateName, uint64_t seqNo, uint64_t incomingFaceId);
 
 public:
-  std::unique_ptr<OnNewLsa> onNewLsa;
+  OnNewLsa onNewLsa;
 
 private:
-  ndn::Face& m_syncFace;
   IsLsaNew m_isLsaNew;
   const ConfParameter& m_confParam;
 
@@ -105,10 +99,6 @@
   ndn::Name m_coorLsaUserPrefix;
 
   SyncProtocolAdapter m_syncLogic;
-
-private:
-  const std::string NLSR_COMPONENT = "nlsr";
-  const std::string LSA_COMPONENT = "LSA";
 };
 
 } // namespace nlsr
diff --git a/src/communication/sync-protocol-adapter.cpp b/src/communication/sync-protocol-adapter.cpp
index 81ec6f3..2e09d2b 100644
--- a/src/communication/sync-protocol-adapter.cpp
+++ b/src/communication/sync-protocol-adapter.cpp
@@ -22,30 +22,32 @@
 #include "sync-protocol-adapter.hpp"
 #include "logger.hpp"
 
-INIT_LOGGER(SyncProtocolAdapter);
-
 namespace nlsr {
 
+INIT_LOGGER(SyncProtocolAdapter);
+
+#ifdef HAVE_CHRONOSYNC
 const auto FIXED_SESSION = ndn::name::Component::fromNumber(0);
+#endif
 
 SyncProtocolAdapter::SyncProtocolAdapter(ndn::Face& face,
+                                         ndn::KeyChain& keyChain,
                                          SyncProtocol syncProtocol,
                                          const ndn::Name& syncPrefix,
                                          const ndn::Name& userPrefix,
                                          ndn::time::milliseconds syncInterestLifetime,
-                                         const SyncUpdateCallback& syncUpdateCallback)
- : m_syncProtocol(syncProtocol)
- , m_syncUpdateCallback(syncUpdateCallback)
+                                         SyncUpdateCallback syncUpdateCallback)
+  : m_syncProtocol(syncProtocol)
+  , m_syncUpdateCallback(std::move(syncUpdateCallback))
 {
-  NLSR_LOG_TRACE("SyncProtocol value: " << m_syncProtocol);
-
+  switch (m_syncProtocol) {
 #ifdef HAVE_CHRONOSYNC
-  if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
+  case SyncProtocol::CHRONOSYNC:
     NDN_LOG_DEBUG("Using ChronoSync");
     m_chronoSyncLogic = std::make_shared<chronosync::Logic>(face,
                           syncPrefix,
                           userPrefix,
-                          std::bind(&SyncProtocolAdapter::onChronoSyncUpdate, this, _1),
+                          [this] (auto&&... args) { onChronoSyncUpdate(std::forward<decltype(args)>(args)...); },
                           chronosync::Logic::DEFAULT_NAME,
                           chronosync::Logic::DEFAULT_VALIDATOR,
                           chronosync::Logic::DEFAULT_RESET_TIMER,
@@ -55,42 +57,55 @@
                           chronosync::Logic::DEFAULT_SYNC_REPLY_FRESHNESS,
                           chronosync::Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME,
                           FIXED_SESSION);
-    return;
-  }
+    break;
 #endif
-
-  NDN_LOG_DEBUG("Using PSync");
-  m_psyncLogic = std::make_shared<psync::FullProducer>(80,
-                   face,
-                   syncPrefix,
-                   userPrefix,
-                   std::bind(&SyncProtocolAdapter::onPSyncUpdate, this, _1),
-                   syncInterestLifetime);
+  case SyncProtocol::PSYNC:
+    NDN_LOG_DEBUG("Using PSync");
+    m_psyncLogic = std::make_shared<psync::FullProducer>(face,
+                     keyChain,
+                     80,
+                     syncPrefix,
+                     userPrefix,
+                     [this] (auto&&... args) { onPSyncUpdate(std::forward<decltype(args)>(args)...); },
+                     syncInterestLifetime);
+    break;
+  default:
+    NDN_CXX_UNREACHABLE;
+  }
 }
 
 void
 SyncProtocolAdapter::addUserNode(const ndn::Name& userPrefix)
 {
+  switch (m_syncProtocol) {
 #ifdef HAVE_CHRONOSYNC
-  if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
+  case SyncProtocol::CHRONOSYNC:
     m_chronoSyncLogic->addUserNode(userPrefix, chronosync::Logic::DEFAULT_NAME, FIXED_SESSION);
-    return;
-  }
+    break;
 #endif
-
-  m_psyncLogic->addUserNode(userPrefix);
+  case SyncProtocol::PSYNC:
+    m_psyncLogic->addUserNode(userPrefix);
+    break;
+  default:
+    NDN_CXX_UNREACHABLE;
+  }
 }
 
 void
 SyncProtocolAdapter::publishUpdate(const ndn::Name& userPrefix, uint64_t seq)
 {
+  switch (m_syncProtocol) {
 #ifdef HAVE_CHRONOSYNC
-  if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
+  case SyncProtocol::CHRONOSYNC:
     m_chronoSyncLogic->updateSeqNo(seq, userPrefix);
-    return;
-  }
+    break;
 #endif
-  m_psyncLogic->publishName(userPrefix, seq);
+  case SyncProtocol::PSYNC:
+    m_psyncLogic->publishName(userPrefix, seq);
+    break;
+  default:
+    NDN_CXX_UNREACHABLE;
+  }
 }
 
 #ifdef HAVE_CHRONOSYNC
diff --git a/src/communication/sync-protocol-adapter.hpp b/src/communication/sync-protocol-adapter.hpp
index db67fb1..fb5cdbe 100644
--- a/src/communication/sync-protocol-adapter.hpp
+++ b/src/communication/sync-protocol-adapter.hpp
@@ -25,6 +25,8 @@
 #include "conf-parameter.hpp"
 
 #include <ndn-cxx/face.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+
 #ifdef HAVE_CHRONOSYNC
 #include <ChronoSync/logic.hpp>
 #endif
@@ -32,18 +34,19 @@
 
 namespace nlsr {
 
-typedef std::function<void(const ndn::Name& updateName,
-                           uint64_t seqNo, uint64_t incomingFaceId)> SyncUpdateCallback;
+using SyncUpdateCallback = std::function<void(const ndn::Name& updateName,
+                                              uint64_t seqNo, uint64_t incomingFaceId)>;
 
 class SyncProtocolAdapter
 {
 public:
-  SyncProtocolAdapter(ndn::Face& facePtr,
+  SyncProtocolAdapter(ndn::Face& face,
+                      ndn::KeyChain& keyChain,
                       SyncProtocol syncProtocol,
                       const ndn::Name& syncPrefix,
                       const ndn::Name& userPrefix,
                       ndn::time::milliseconds syncInterestLifetime,
-                      const SyncUpdateCallback& syncUpdateCallback);
+                      SyncUpdateCallback syncUpdateCallback);
 
   /*! \brief Add user node to ChronoSync or PSync
    *
@@ -91,6 +94,7 @@
 private:
   SyncProtocol m_syncProtocol;
   SyncUpdateCallback m_syncUpdateCallback;
+
 #ifdef HAVE_CHRONOSYNC
   std::shared_ptr<chronosync::Logic> m_chronoSyncLogic;
 #endif
diff --git a/src/conf-file-processor.cpp b/src/conf-file-processor.cpp
index 4140e0d..bd7dceb 100644
--- a/src/conf-file-processor.cpp
+++ b/src/conf-file-processor.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2021,  The University of Memphis,
+ * Copyright (c) 2014-2022,  The University of Memphis,
  *                           Regents of the University of California,
  *                           Arizona Board of Regents.
  *
@@ -287,19 +287,19 @@
   std::string syncProtocol = section.get<std::string>("sync-protocol", "psync");
   if (syncProtocol == "chronosync") {
 #ifdef HAVE_CHRONOSYNC
-    m_confParam.setSyncProtocol(SYNC_PROTOCOL_CHRONOSYNC);
+    m_confParam.setSyncProtocol(SyncProtocol::CHRONOSYNC);
 #else
-    std::cerr << "NLSR was compiled without Chronosync support!" << std::endl;
-    std::cerr << "Only PSync support is currently available ('sync-protocol psync')" << std::endl;
+    std::cerr << "NLSR was compiled without ChronoSync support!\n"
+              << "Only PSync support is currently available ('sync-protocol psync')\n";
     return false;
 #endif
   }
   else if (syncProtocol == "psync") {
-    m_confParam.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
+    m_confParam.setSyncProtocol(SyncProtocol::PSYNC);
   }
   else {
     std::cerr << "Sync protocol '" << syncProtocol << "' is not supported!\n"
-              << "Use chronosync or psync" << std::endl;
+              << "Use either 'chronosync' or 'psync'\n";
     return false;
   }
 
diff --git a/src/conf-parameter.cpp b/src/conf-parameter.cpp
index 8a7a56f..d1c6412 100644
--- a/src/conf-parameter.cpp
+++ b/src/conf-parameter.cpp
@@ -51,7 +51,6 @@
   , m_corR(0)
   , m_maxFacesPerPrefix(MAX_FACES_PER_PREFIX_MIN)
   , m_syncInterestLifetime(ndn::time::milliseconds(SYNC_INTEREST_LIFETIME_DEFAULT))
-  , m_syncProtocol(SYNC_PROTOCOL_PSYNC)
   , m_adjl()
   , m_npl()
   , m_validator(makeCertificateFetcher(face))
diff --git a/src/conf-parameter.hpp b/src/conf-parameter.hpp
index aedffac..32b343c 100644
--- a/src/conf-parameter.hpp
+++ b/src/conf-parameter.hpp
@@ -35,19 +35,17 @@
 
 namespace nlsr {
 
+enum class SyncProtocol {
+  CHRONOSYNC,
+  PSYNC,
+};
+
 enum {
   LSA_REFRESH_TIME_MIN = 240,
   LSA_REFRESH_TIME_DEFAULT = 1800,
   LSA_REFRESH_TIME_MAX = 7200
 };
 
-enum SyncProtocol {
-#ifdef HAVE_CHRONOSYNC
-  SYNC_PROTOCOL_CHRONOSYNC,
-#endif
-  SYNC_PROTOCOL_PSYNC
-};
-
 enum {
   LSA_INTEREST_LIFETIME_MIN = 1,
   LSA_INTEREST_LIFETIME_DEFAULT = 4,
@@ -528,7 +526,7 @@
 
   ndn::time::milliseconds m_syncInterestLifetime;
 
-  SyncProtocol m_syncProtocol;
+  SyncProtocol m_syncProtocol = SyncProtocol::PSYNC;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   // must be incremented when breaking changes are made to sync
diff --git a/src/hello-protocol.hpp b/src/hello-protocol.hpp
index 3ae3629..c1664bd 100644
--- a/src/hello-protocol.hpp
+++ b/src/hello-protocol.hpp
@@ -123,6 +123,9 @@
                             const ndn::security::ValidationError& ve);
 
 public:
+  static inline const std::string INFO_COMPONENT{"INFO"};
+  static inline const std::string NLSR_COMPONENT{"nlsr"};
+
   ndn::util::Signal<HelloProtocol, const ndn::Name&> onInitialHelloDataValidated;
 
 private:
@@ -134,10 +137,6 @@
   RoutingTable& m_routingTable;
   Lsdb& m_lsdb;
   AdjacencyList& m_adjacencyList;
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  static inline const std::string INFO_COMPONENT{"INFO"};
-  static inline const std::string NLSR_COMPONENT{"nlsr"};
 };
 
 } // namespace nlsr
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index bca46a6..9922fa0 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -35,7 +35,7 @@
   : m_face(face)
   , m_scheduler(face.getIoService())
   , m_confParam(confParam)
-  , m_sync(m_face,
+  , m_sync(m_face, keyChain,
            [this] (const auto& routerName, const Lsa::Type& lsaType,
                    uint64_t sequenceNumber, uint64_t incomingFaceId) {
              return isLsaNew(routerName, lsaType, sequenceNumber);
@@ -44,7 +44,7 @@
   , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
   , m_thisRouterPrefix(m_confParam.getRouterPrefix())
   , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
-  , m_onNewLsaConnection(m_sync.onNewLsa->connect(
+  , m_onNewLsaConnection(m_sync.onNewLsa.connect(
       [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
               const ndn::Name& originRouter, uint64_t incomingFaceId) {
         ndn::Name lsaInterest{updateName};
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index 915c7b6..0578edd 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -178,7 +178,8 @@
   }
 
   void
-  incrementDataSentStats(Lsa::Type lsaType) {
+  incrementDataSentStats(Lsa::Type lsaType)
+  {
     if (lsaType == Lsa::Type::NAME) {
       lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
     }
@@ -191,7 +192,8 @@
   }
 
   void
-  incrementInterestRcvdStats(Lsa::Type lsaType) {
+  incrementInterestRcvdStats(Lsa::Type lsaType)
+  {
     if (lsaType == Lsa::Type::NAME) {
       lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
     }
@@ -204,7 +206,8 @@
   }
 
   void
-  incrementInterestSentStats(Lsa::Type lsaType) {
+  incrementInterestSentStats(Lsa::Type lsaType)
+  {
     if (lsaType == Lsa::Type::NAME) {
       lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
     }
@@ -222,7 +225,7 @@
     \param seqNo The sequence number to check.
   */
   bool
-  isLsaNew(const ndn::Name& originRouter, const Lsa::Type& lsaType, uint64_t lsSeqNo)
+  isLsaNew(const ndn::Name& originRouter, const Lsa::Type& lsaType, uint64_t lsSeqNo) const
   {
     // Is the name in the LSDB and the supplied seq no is the highest so far
     auto lsaPtr = findLsa(originRouter, lsaType);
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index c43ad47..533d920 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -45,7 +45,7 @@
   , m_namePrefixTable(confParam.getRouterPrefix(), m_fib, m_routingTable,
                       m_routingTable.afterRoutingChange, m_lsdb.onLsdbModified)
   , m_helloProtocol(m_face, keyChain, confParam, m_routingTable, m_lsdb)
-  , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa->connect(
+  , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa.connect(
       [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
               const ndn::Name& originRouter, uint64_t incomingFaceId) {
         registerStrategyForCerts(originRouter);