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();
diff --git a/tests/test-lsdb.cpp b/tests/test-lsdb.cpp
index 1585a4d..2991967 100644
--- a/tests/test-lsdb.cpp
+++ b/tests/test-lsdb.cpp
@@ -185,31 +185,6 @@
   BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), NameLsa::TYPE_STRING), false);
 }
 
-BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild)
-{
-  // Should register Sync prefix
-  lsdb.buildAndInstallOwnAdjLsa();
-  face->processEvents(ndn::time::milliseconds(1));
-
-  std::vector<ndn::Interest>& interests = face->m_sentInterests;
-
-  BOOST_REQUIRE(interests.size() > 0);
-
-  ndn::nfd::ControlParameters extractedParameters;
-  ndn::Name::Component verb;
-  extractParameters(interests[0], verb, extractedParameters);
-
-  BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
-  BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix());
-
-  // Should not register Sync prefix
-  face->m_sentInterests.clear();
-  lsdb.buildAndInstallOwnAdjLsa();
-  face->processEvents(ndn::time::milliseconds(1));
-
-  BOOST_CHECK_EQUAL(interests.size(), 0);
-}
-
 BOOST_AUTO_TEST_CASE(InstallNameLsa)
 {
   // Install lsa with name1 and name2
diff --git a/tests/test-sync-logic-handler.cpp b/tests/test-sync-logic-handler.cpp
index 2057ed3..2595e41 100644
--- a/tests/test-sync-logic-handler.cpp
+++ b/tests/test-sync-logic-handler.cpp
@@ -177,6 +177,19 @@
   BOOST_CHECK_EQUAL(sync.m_updatePrefix, expectedPrefix);
 }
 
+BOOST_AUTO_TEST_CASE(CreateSyncSocketOnInitialization) // Bug #2649
+{
+  nlsr.initialize();
+
+  // Make sure an adjacency LSA has not been built yet
+  ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
+  AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key);
+  BOOST_REQUIRE(lsa == nullptr);
+
+  // Publish a routing update before an Adjacency LSA is built
+  BOOST_CHECK_NO_THROW(sync.publishRoutingUpdate());
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace test