[ndnSIM] Hack rib::Service so it can be used in ndnSIM directly

Change-Id: Iefc1e294d376d85c61530fd828f93871cd2e4b17
diff --git a/daemon/rib/service.cpp b/daemon/rib/service.cpp
index 0c674b3..1a8c6c7 100644
--- a/daemon/rib/service.cpp
+++ b/daemon/rib/service.cpp
@@ -34,17 +34,17 @@
 #include "common/global.hpp"
 #include "common/logger.hpp"
 
-#include <boost/property_tree/info_parser.hpp>
-#include <ndn-cxx/transport/tcp-transport.hpp>
-#include <ndn-cxx/transport/unix-transport.hpp>
+#include "ns3/node-list.h"
+#include "ns3/node.h"
+#include "ns3/simulator.h"
+
+#include "ns3/ndnSIM/model/ndn-l3-protocol.hpp"
 
 namespace nfd {
 namespace rib {
 
 NFD_LOG_INIT(RibService);
 
-Service* Service::s_instance = nullptr;
-
 const std::string CFG_RIB = "rib";
 const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
 const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
@@ -55,52 +55,8 @@
 const uint64_t PROPAGATE_DEFAULT_COST = 15;
 const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
 
-static ConfigSection
-loadConfigSectionFromFile(const std::string& filename)
-{
-  ConfigSection config;
-  // Any format errors should have been caught already
-  boost::property_tree::read_info(filename, config);
-  return config;
-}
-
-/**
- * \brief Look into the config file and construct appropriate transport to communicate with NFD
- * If NFD-RIB instance was initialized with config file, INFO format is assumed
- */
-static shared_ptr<ndn::Transport>
-makeLocalNfdTransport(const ConfigSection& config)
-{
-  if (config.get_child_optional("face_system.unix")) {
-    // default socket path should be the same as in UnixStreamFactory::processConfig
-#ifdef __linux__
-    auto path = config.get<std::string>("face_system.unix.path", "/run/nfd.sock");
-#else
-    auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
-#endif // __linux__
-    return make_shared<ndn::UnixTransport>(path);
-  }
-  else if (config.get_child_optional("face_system.tcp") &&
-           config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
-    // default port should be the same as in TcpFactory::processConfig
-    auto port = config.get<std::string>("face_system.tcp.port", "6363");
-    return make_shared<ndn::TcpTransport>("localhost", port);
-  }
-  else {
-    NDN_THROW(ConfigFile::Error("No transport is available to communicate with NFD"));
-  }
-}
-
-Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
-  : Service(keyChain, makeLocalNfdTransport(loadConfigSectionFromFile(configFile)),
-            [&configFile] (ConfigFile& config, bool isDryRun) {
-              config.parse(configFile, isDryRun);
-            })
-{
-}
-
-Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
-  : Service(keyChain, makeLocalNfdTransport(configSection),
+Service::Service(const ConfigSection& configSection, ndn::Face& face, ndn::KeyChain& keyChain)
+  : Service(keyChain, face,
             [&configSection] (ConfigFile& config, bool isDryRun) {
               config.parse(configSection, isDryRun, "internal://nfd.conf");
             })
@@ -108,23 +64,15 @@
 }
 
 template<typename ConfigParseFunc>
-Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
-                 const ConfigParseFunc& configParse)
+Service::Service(ndn::KeyChain& keyChain, ndn::Face& face, const ConfigParseFunc& configParse)
   : m_keyChain(keyChain)
-  , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain)
+  , m_face(face)
+  , m_scheduler(m_face.getIoService())
   , m_nfdController(m_face, m_keyChain)
   , m_fibUpdater(m_rib, m_nfdController)
   , m_dispatcher(m_face, m_keyChain)
   , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
 {
-  if (s_instance != nullptr) {
-    NDN_THROW(std::logic_error("RIB service cannot be instantiated more than once"));
-  }
-  if (&getGlobalIoService() != &getRibIoService()) {
-    NDN_THROW(std::logic_error("RIB service must run on RIB thread"));
-  }
-  s_instance = this;
-
   ConfigFile config(ConfigFile::ignoreUnknownSection);
   config.addSectionHandler(CFG_RIB, [this] (auto&&... args) {
     processConfig(std::forward<decltype(args)>(args)...);
@@ -138,19 +86,14 @@
 
 Service::~Service()
 {
-  s_instance = nullptr;
 }
 
 Service&
 Service::get()
 {
-  if (s_instance == nullptr) {
-    NDN_THROW(std::logic_error("RIB service is not instantiated"));
-  }
-  if (&getGlobalIoService() != &getRibIoService()) {
-    NDN_THROW(std::logic_error("Must get RIB service on RIB thread"));
-  }
-  return *s_instance;
+  auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext());
+  auto l3 = node->GetObject<::ns3::ndn::L3Protocol>();
+  return l3->getRibService();
 }
 
 void