main: use boost::program_options and other cleanups
Change-Id: I5d20e78aaf782b6d0013175f9db9d2eb33156776
diff --git a/src/handles/tcp-bulk-insert-handle.cpp b/src/handles/tcp-bulk-insert-handle.cpp
index f27d173..598e966 100644
--- a/src/handles/tcp-bulk-insert-handle.cpp
+++ b/src/handles/tcp-bulk-insert-handle.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018, Regents of the University of California.
+/*
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -21,12 +21,9 @@
#include <ndn-cxx/util/logger.hpp>
+NDN_LOG_INIT(repo.TcpHandle);
+
namespace repo {
-
-NDN_LOG_INIT(repo.tcpHandle);
-
-const size_t MAX_NDN_PACKET_SIZE = 8800;
-
namespace detail {
class TcpBulkInsertClient : noncopyable
@@ -47,7 +44,7 @@
BOOST_ASSERT(!client->m_hasStarted);
client->m_socket->async_receive(
- boost::asio::buffer(client->m_inputBuffer, MAX_NDN_PACKET_SIZE), 0,
+ boost::asio::buffer(client->m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), 0,
std::bind(&TcpBulkInsertClient::handleReceive, client, _1, _2, client));
client->m_hasStarted = true;
@@ -63,7 +60,7 @@
TcpBulkInsertHandle& m_writer;
std::shared_ptr<boost::asio::ip::tcp::socket> m_socket;
bool m_hasStarted;
- uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE];
+ uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE];
std::size_t m_inputBufferSize;
};
@@ -144,9 +141,9 @@
if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
return;
- boost::system::error_code error;
- m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
- m_socket->close(error);
+ boost::system::error_code ec;
+ m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
+ m_socket->close(ec);
return;
}
@@ -175,17 +172,17 @@
else
NDN_LOG_DEBUG("FAILED to inject " << data.getName());
}
- catch (const std::runtime_error& error) {
+ catch (const std::runtime_error&) {
/// \todo Catch specific error after determining what wireDecode() can throw
NDN_LOG_ERROR("Error decoding received Data packet");
}
}
}
- if (!isOk && m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) {
- boost::system::error_code error;
- m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
- m_socket->close(error);
+ if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) {
+ boost::system::error_code ec;
+ m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
+ m_socket->close(ec);
return;
}
@@ -200,9 +197,8 @@
}
m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize,
- MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0,
+ ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0,
std::bind(&TcpBulkInsertClient::handleReceive, this, _1, _2, client));
}
-
} // namespace repo
diff --git a/src/main.cpp b/src/main.cpp
index 800b8c7..6229759 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2018, Regents of the University of California.
+/*
+ * Copyright (c) 2018-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -18,82 +18,75 @@
*/
#include "config.hpp"
+#include "extended-error-message.hpp"
#include "repo.hpp"
-#include "extended-error-message.hpp"
+#include <string.h> // for strsignal()
+
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/signal_set.hpp>
+#include <boost/program_options.hpp>
#include <ndn-cxx/util/logger.hpp>
NDN_LOG_INIT(repo.main);
-static const std::string ndnRepoUsageMessage =
- /* argv[0] */ " - Next generation of NDN repository\n"
- "-h: show help message\n"
- "-c: set config file path\n"
- ;
-
-void
-terminate(boost::asio::io_service& ioService,
- const boost::system::error_code& error,
- int signalNo,
- boost::asio::signal_set& signalSet)
-{
- if (error)
- return;
-
- if (signalNo == SIGINT || signalNo == SIGTERM) {
- ioService.stop();
- std::cout << "Caught signal '" << strsignal(signalNo) << "', exiting..." << std::endl;
- }
- else {
- /// \todo try to reload config file
- signalSet.async_wait(std::bind(&terminate, std::ref(ioService), _1, _2, std::ref(signalSet)));
- }
-}
-
int
main(int argc, char** argv)
{
- std::string configPath = DEFAULT_CONFIG_FILE;
- int opt;
- while ((opt = getopt(argc, argv, "hc:")) != -1) {
- switch (opt) {
- case 'h':
- std::cout << argv[0] << ndnRepoUsageMessage << std::endl;
- return 1;
- case 'c':
- configPath = std::string(optarg);
- break;
- default:
- break;
- }
+ std::string configFile(DEFAULT_CONFIG_FILE);
+
+ namespace po = boost::program_options;
+ po::options_description optsDesc("Options");
+ optsDesc.add_options()
+ ("help,h", "print this help message and exit")
+ ("config-file,c", po::value<std::string>(&configFile)->default_value(configFile),
+ "path to configuration file")
+ ;
+
+ po::variables_map vm;
+ try {
+ po::store(po::parse_command_line(argc, argv, optsDesc), vm);
+ po::notify(vm);
+ }
+ catch (const po::error& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ return 2;
+ }
+ catch (const boost::bad_any_cast& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ return 2;
}
+ if (vm.count("help") != 0) {
+ std::cout << "Usage: " << argv[0] << " [options]\n"
+ << "\n"
+ << optsDesc;
+ return 0;
+ }
+
+ boost::asio::io_service ioService;
+
+ /// \todo reload config file on SIGHUP
+ boost::asio::signal_set signalSet(ioService, SIGINT, SIGTERM);
+ signalSet.async_wait([&ioService] (const boost::system::error_code& error, int signalNo) {
+ if (!error) {
+ NDN_LOG_FATAL("Exiting on signal " << signalNo << "/" << strsignal(signalNo));
+ ioService.stop();
+ }
+ });
+
try {
- boost::asio::io_service ioService;
- repo::Repo repoInstance(ioService, repo::parseConfig(configPath));
-
- boost::asio::signal_set signalSet(ioService);
- signalSet.add(SIGINT);
- signalSet.add(SIGTERM);
- signalSet.add(SIGHUP);
- signalSet.add(SIGUSR1);
- signalSet.add(SIGUSR2);
- signalSet.async_wait(std::bind(&terminate, std::ref(ioService),
- std::placeholders::_1, std::placeholders::_2,
- std::ref(signalSet)));
-
- repoInstance.initializeStorage();
-
- repoInstance.enableValidation();
-
- repoInstance.enableListening();
+ repo::Repo repo(ioService, repo::parseConfig(configFile));
+ repo.initializeStorage();
+ repo.enableValidation();
+ repo.enableListening();
ioService.run();
}
catch (const std::exception& e) {
NDN_LOG_FATAL(repo::getExtendedErrorMessage(e));
- return 2;
+ return 1;
}
return 0;
diff --git a/src/repo.cpp b/src/repo.cpp
index 9deef35..2bdc833 100644
--- a/src/repo.cpp
+++ b/src/repo.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -24,7 +24,7 @@
namespace repo {
-NDN_LOG_INIT(repo);
+NDN_LOG_INIT(repo.Repo);
RepoConfig
parseConfig(const std::string& configPath)
diff --git a/src/storage/repo-storage.hpp b/src/storage/repo-storage.hpp
index 39e576d..5934b12 100644
--- a/src/storage/repo-storage.hpp
+++ b/src/storage/repo-storage.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -17,10 +17,9 @@
* repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef REPO_REPO_STORAGE_HPP
-#define REPO_REPO_STORAGE_HPP
+#ifndef REPO_STORAGE_REPO_STORAGE_HPP
+#define REPO_STORAGE_REPO_STORAGE_HPP
-#include "../common.hpp"
#include "storage.hpp"
#include "../repo-command-parameter.hpp"
@@ -93,4 +92,4 @@
} // namespace repo
-#endif // REPO_REPO_STORAGE_HPP
+#endif // REPO_STORAGE_REPO_STORAGE_HPP