comm: Create SyncSocket on initialization

refs: #2649

Change-Id: I9e5891b8ad5de9a6ff6ac4edab4bab1564a309c8
diff --git a/src/communication/sync-logic-handler.cpp b/src/communication/sync-logic-handler.cpp
index 705a721..1434aa4 100644
--- a/src/communication/sync-logic-handler.cpp
+++ b/src/communication/sync-logic-handler.cpp
@@ -138,8 +138,18 @@
 }
 
 void
-SyncLogicHandler::createSyncSocket()
+SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix)
 {
+  if (m_syncSocket != nullptr) {
+    _LOG_WARN("Trying to create Sync socket, but Sync socket already exists");
+    return;
+  }
+
+  m_syncPrefix = syncPrefix;
+
+  // Build LSA sync update prefix
+  buildUpdatePrefix();
+
   _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
 
   // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory
@@ -254,6 +264,12 @@
 void
 SyncLogicHandler::publishRoutingUpdate()
 {
+  if (m_syncSocket == nullptr) {
+    _LOG_FATAL("Cannot publish routing update; SyncSocket does not exist");
+
+    throw SyncLogicHandler::Error("Cannot publish routing update; SyncSocket does not exist");
+  }
+
   m_sequencingManager.writeSeqNoToFile();
 
   publishSyncUpdate(m_updatePrefix, m_sequencingManager.getCombinedSeqNo());
diff --git a/src/communication/sync-logic-handler.hpp b/src/communication/sync-logic-handler.hpp
index 36caae7..7df3671 100644
--- a/src/communication/sync-logic-handler.hpp
+++ b/src/communication/sync-logic-handler.hpp
@@ -44,10 +44,17 @@
 class SyncLogicHandler
 {
 public:
-  SyncLogicHandler(ndn::Face& face, Lsdb& lsdb, ConfParameter& conf, SequencingManager& seqManager);
+  class Error : public std::runtime_error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : std::runtime_error(what)
+    {
+    }
+  };
 
-  void
-  createSyncSocket();
+  SyncLogicHandler(ndn::Face& face, Lsdb& lsdb, ConfParameter& conf, SequencingManager& seqManager);
 
   void
   onNsyncUpdate(const std::vector<Sync::MissingDataInfo>& v, Sync::SyncSocket* socket);
@@ -59,15 +66,12 @@
   publishRoutingUpdate();
 
   void
-  setSyncPrefix(const std::string& sp)
-  {
-    m_syncPrefix = sp;
-  }
+  createSyncSocket(const ndn::Name& syncPrefix);
 
+private:
   void
   buildUpdatePrefix();
 
-private:
   void
   processUpdateFromSync(const SyncUpdate& updateName);
 
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 1374906..c0fb765 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -597,17 +597,9 @@
 
   m_nlsr.getSequencingManager().increaseAdjLsaSeq();
 
-  bool isInstalled = installAdjLsa(adjLsa);
-
-  // Delay Sync prefix registration until the first Adjacency LSA is built
-  if (isInstalled && !m_hasSyncPrefixBeenRegistered) {
-    m_nlsr.getSyncLogicHandler().createSyncSocket();
-    m_hasSyncPrefixBeenRegistered = true;
-  }
-
   m_sync.publishRoutingUpdate();
 
-  return isInstalled;
+  return installAdjLsa(adjLsa);
 }
 
 bool
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index 3cf74c6..417c978 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -45,7 +45,6 @@
     : m_nlsr(nlsr)
     , m_scheduler(scheduler)
     , m_sync(sync)
-    , m_hasSyncPrefixBeenRegistered(false)
     , m_lsaRefreshTime(0)
     , m_adjLsaBuildInterval(static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT))
   {
@@ -278,8 +277,6 @@
   std::list<AdjLsa> m_adjLsdb;
   std::list<CoordinateLsa> m_corLsdb;
 
-  bool m_hasSyncPrefixBeenRegistered;
-
   seconds m_lsaRefreshTime;
   std::string m_thisRouterPrefix;
 
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index 2d58b19..1176b69 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -134,8 +134,7 @@
   m_sequencingManager.setSeqFileName(m_confParam.getSeqFileDir());
   m_sequencingManager.initiateSeqNoFromFile();
 
-  m_syncLogicHandler.setSyncPrefix(m_confParam.getChronosyncPrefix().toUri());
-  m_syncLogicHandler.buildUpdatePrefix();
+  m_syncLogicHandler.createSyncSocket(m_confParam.getChronosyncPrefix());
 
   /* Logging start */
   m_confParam.writeLog();