cmd: Switch code to use ndn-cxx

Implementation moved to ndn::chronoshare namespace

Change-Id: I1d7a46f03133a8e382f0a2bb272311fbd18c95c4
diff --git a/cmd/csd.cpp b/cmd/csd.cpp
index 00cd434..65b1abc 100644
--- a/cmd/csd.cpp
+++ b/cmd/csd.cpp
@@ -18,41 +18,87 @@
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
 
-#include <QtCore>
+#include "csd.hpp"
+#include <thread>
 
-#include "ccnx-wrapper.hpp"
-#include "dispatcher.hpp"
-#include "fs-watcher.hpp"
-#include "logging.hpp"
-
-#include <boost/make_shared.hpp>
-
-using namespace boost;
-using namespace std;
-using namespace Ndnx;
+namespace ndn {
+namespace chronoshare {
 
 int
 main(int argc, char* argv[])
 {
   QCoreApplication app(argc, argv);
+  Runner runner(&app);
+  QObject::connect(&runner, SIGNAL(terminateApp()), &app, SLOT(quit()), Qt::QueuedConnection);
 
   if (argc != 4) {
-    cerr << "Usage: ./csd <username> <shared-folder> <path>" << endl;
+    std::cerr << "Usage: ./csd <username> <shared-folder> <path>" << std::endl;
     return 1;
   }
 
-  string username = argv[1];
-  string sharedFolder = argv[2];
-  string path = argv[3];
+  std::string username = argv[1];
+  std::string sharedFolder = argv[2];
+  std::string path = argv[3];
 
-  cout << "Starting ChronoShare for [" << username << "] shared-folder [" << sharedFolder
-       << "] at [" << path << "]" << endl;
+  std::cout << "Starting ChronoShare for [" << username << "] shared-folder [" << sharedFolder
+            << "] at [" << path << "]" << std::endl;
 
-  Dispatcher dispatcher(username, sharedFolder, path, make_shared<CcnxWrapper>());
+  boost::asio::io_service ioService;
+  Face face(ioService);
 
-  FsWatcher watcher(path.c_str(),
+  Dispatcher dispatcher(username, sharedFolder, path, face);
+
+  std::thread ioThread([&ioService, &runner] {
+    try {
+      ioService.run();
+      runner.retval = 0;
+    }
+    catch (boost::exception& e) {
+      runner.retval = 2;
+      if (&dynamic_cast<std::exception&>(e) != nullptr) {
+        std::cerr << "ERROR: " << dynamic_cast<std::exception&>(e).what() << std::endl;
+      }
+      std::cerr << boost::diagnostic_information(e, true) << std::endl;
+    }
+    catch (std::exception& e) {
+      runner.retval = 2;
+      std::cerr << "ERROR: " << e.what() << std::endl;
+    }
+
+    QTimer::singleShot(0, &runner, SLOT(notifyAsioThread()));
+  });
+
+  FsWatcher watcher(ioService, path.c_str(),
                     bind(&Dispatcher::Did_LocalFile_AddOrModify, &dispatcher, _1),
                     bind(&Dispatcher::Did_LocalFile_Delete, &dispatcher, _1));
 
-  return app.exec();
+  int retval = 0;
+  try {
+    retval = app.exec();
+  }
+  catch (boost::exception& e) {
+    retval = 1;
+    if (&dynamic_cast<std::exception&>(e) != nullptr) {
+      std::cerr << "ERROR: " << dynamic_cast<std::exception&>(e).what() << std::endl;
+    }
+    std::cerr << boost::diagnostic_information(e, true) << std::endl;
+  }
+  catch (std::exception& e) {
+    retval = 1;
+    std::cerr << "ERROR: " << e.what() << std::endl;
+  }
+
+  ioService.stop();
+  ioThread.join();
+
+  return std::max(retval, runner.retval);
+}
+
+} // namespace chronoshare
+} // namespace ndn
+
+int
+main(int argc, char* argv[])
+{
+  return ndn::chronoshare::main(argc, argv);
 }