server: add read configuration file function
Change-Id: I6a3fe8208219a18e0a67b791dedb6b93d5e9caab
diff --git a/server/repo.cpp b/server/repo.cpp
new file mode 100644
index 0000000..0c3165a
--- /dev/null
+++ b/server/repo.cpp
@@ -0,0 +1,157 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "repo.hpp"
+
+namespace repo {
+
+RepoConfig
+parseConfig(const std::string& configPath)
+{
+ if (configPath.empty()) {
+ std::cerr << "configuration file path is empty" << std::endl;
+ }
+
+ std::ifstream fin(configPath.c_str());
+ if (!fin.is_open())
+ throw Repo::Error("failed to open configuration file '"+ configPath +"'");
+
+ using namespace boost::property_tree;
+ ptree propertyTree;
+ try {
+ read_info(fin, propertyTree);
+ }
+ catch (ptree_error& e) {
+ throw Repo::Error("failed to read configuration file '"+ configPath +"'");
+ }
+
+ ptree repoConf = propertyTree.get_child("repo");
+
+ RepoConfig repoConfig;
+
+ ptree dataConf = repoConf.get_child("data");
+
+ for (ptree::const_iterator it = dataConf.begin();
+ it != dataConf.end();
+ ++it)
+ {
+ if (it->first == "prefix")
+ repoConfig.dataPrefixes.push_back(Name(it->second.get_value<std::string>()));
+ else
+ throw Repo::Error("Unrecognized '" + it->first + "' option in 'data' section in "
+ "configuration file '"+ configPath +"'");
+ }
+
+ ptree commandConf = repoConf.get_child("command");
+ for (ptree::const_iterator it = commandConf.begin();
+ it != commandConf.end();
+ ++it)
+ {
+ if (it->first == "prefix")
+ repoConfig.repoPrefixes.push_back(Name(it->second.get_value<std::string>()));
+ else
+ throw Repo::Error("Unrecognized '" + it->first + "' option in 'command' section in "
+ "configuration file '"+ configPath +"'");
+ }
+
+ ptree tcpBulkInsert = repoConf.get_child("tcp_bulk_insert");
+ bool isTcpBulkEnabled = false;
+ std::string host = "localhost";
+ std::string port = "7376";
+ for (ptree::const_iterator it = tcpBulkInsert.begin();
+ it != tcpBulkInsert.end();
+ ++it)
+ {
+ isTcpBulkEnabled = true;
+
+ // tcp_bulk_insert {
+ // host "localhost" ; IP address or hostname to listen on
+ // port 7635 ; Port number to listen on
+ // }
+ if (it->first == "host") {
+ host = it->second.get_value<std::string>();
+ }
+ else if (it->first == "port") {
+ port = it->second.get_value<std::string>();
+ }
+ else
+ throw Repo::Error("Unrecognized '" + it->first + "' option in 'tcp_bulk_insert' section in "
+ "configuration file '"+ configPath +"'");
+ }
+ if (isTcpBulkEnabled) {
+ repoConfig.tcpBulkInsertEndpoints.push_back(std::make_pair(host, port));
+ }
+
+ if (repoConf.get<std::string>("storage.method") != "sqlite")
+ throw Repo::Error("Only 'sqlite' storage method is supported");
+
+ repoConfig.dbPath = repoConf.get<std::string>("storage.path");
+
+ repoConfig.validatorNode = repoConf.get_child("validator");
+ return repoConfig;
+}
+
+inline static void
+NullDeleter(boost::asio::io_service* variable)
+{
+ // do nothing
+}
+
+Repo::Repo(boost::asio::io_service& ioService, const RepoConfig& config)
+ : m_config(config)
+ , m_scheduler(ioService)
+ , m_face(shared_ptr<boost::asio::io_service>(&ioService, &NullDeleter))
+ , m_storageHandle(openStorage(config))
+ , m_readHandle(m_face, *m_storageHandle, m_keyChain, m_scheduler)
+ , m_writeHandle(m_face, *m_storageHandle, m_keyChain, m_scheduler, m_validator)
+ , m_deleteHandle(m_face, *m_storageHandle, m_keyChain, m_scheduler, m_validator)
+ , m_tcpBulkInsertHandle(ioService, *m_storageHandle)
+
+{
+ //Trust model not implemented, this is just an empty validator
+ //@todo add a function to parse RepoConfig.validatorNode and define the trust model
+ m_validator.addInterestRule("^<>",
+ *m_keyChain.
+ getCertificate(m_keyChain.getDefaultCertificateName()));
+}
+
+shared_ptr<StorageHandle>
+Repo::openStorage(const RepoConfig& config)
+{
+ shared_ptr<StorageHandle> storageHandle = ndn::make_shared<SqliteHandle>(config.dbPath);
+ return storageHandle;
+}
+
+void
+Repo::enableListening()
+{
+ // Enable "listening" on Data prefixes
+ for (vector<ndn::Name>::iterator it = m_config.dataPrefixes.begin();
+ it != m_config.dataPrefixes.end();
+ ++it)
+ {
+ m_readHandle.listen(*it);
+ }
+
+ // Enable "listening" on control prefixes
+ for (vector<ndn::Name>::iterator it = m_config.repoPrefixes.begin();
+ it != m_config.repoPrefixes.end();
+ ++it)
+ {
+ m_writeHandle.listen(*it);
+ m_deleteHandle.listen(*it);
+ }
+
+ // Enable listening on TCP bulk insert addresses
+ for (vector<pair<string, string> >::iterator it = m_config.tcpBulkInsertEndpoints.begin();
+ it != m_config.tcpBulkInsertEndpoints.end();
+ ++it)
+ {
+ m_tcpBulkInsertHandle.listen(it->first, it->second);
+ }
+}
+
+} // namespace repo
diff --git a/server/repo.hpp b/server/repo.hpp
new file mode 100644
index 0000000..d5cb653
--- /dev/null
+++ b/server/repo.hpp
@@ -0,0 +1,86 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef REPO_SERVER_REPO_HPP
+#define REPO_SERVER_REPO_HPP
+
+#include "../storage/storage-handle.hpp"
+#include "../storage/sqlite/sqlite-handle.hpp"
+#include "../ndn-handle/read-handle.hpp"
+#include "../ndn-handle/write-handle.hpp"
+#include "../ndn-handle/delete-handle.hpp"
+#include "../ndn-handle/tcp-bulk-insert-handle.hpp"
+
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <vector>
+#include <ndn-cpp-dev/face.hpp>
+#include <ndn-cpp-dev/util/command-interest-validator.hpp>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/info_parser.hpp>
+
+namespace repo {
+
+using std::string;
+using std::vector;
+using std::pair;
+
+struct RepoConfig
+{
+ //StorageMethod storageMethod; This will be implemtented if there is other method.
+ std::string dbPath;
+ vector<ndn::Name> dataPrefixes;
+ vector<ndn::Name> repoPrefixes;
+ vector<pair<string, string> > tcpBulkInsertEndpoints;
+
+ //@todo validator should be configured in config file
+ boost::property_tree::ptree validatorNode;
+};
+
+RepoConfig
+parseConfig(const std::string& confPath);
+
+class Repo : noncopyable
+{
+
+public:
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
+
+public:
+ Repo(boost::asio::io_service& ioService, const RepoConfig& config);
+
+ void
+ enableListening();
+
+private:
+ static shared_ptr<StorageHandle>
+ openStorage(const RepoConfig& config);
+
+private:
+ RepoConfig m_config;
+ ndn::Scheduler m_scheduler;
+ ndn::Face m_face;
+ shared_ptr<StorageHandle> m_storageHandle;
+ KeyChain m_keyChain;
+ CommandInterestValidator m_validator;
+ ReadHandle m_readHandle;
+ WriteHandle m_writeHandle;
+ DeleteHandle m_deleteHandle;
+ TcpBulkInsertHandle m_tcpBulkInsertHandle;
+};
+
+} // namespace repo
+
+#endif // REPO_SERVER_REPO_HPP
diff --git a/server/server.cpp b/server/server.cpp
index b577be0..1adc68f 100644
--- a/server/server.cpp
+++ b/server/server.cpp
@@ -4,80 +4,79 @@
* See COPYING for copyright and distribution information.
*/
-#include <string>
-#include <iostream>
-#include <ndn-cpp-dev/face.hpp>
-#include <ndn-cpp-dev/util/command-interest-validator.hpp>
-
-#include "../storage/storage-handle.hpp"
-#include "../storage/sqlite/sqlite-handle.hpp"
-#include "../ndn-handle/read-handle.hpp"
-#include "../ndn-handle/write-handle.hpp"
-#include "../ndn-handle/tcp-bulk-insert-handle.hpp"
-#include "../ndn-handle/delete-handle.hpp"
+#include "config.hpp"
+#include "repo.hpp"
using namespace repo;
static const string ndnRepoUsageMessage =
- "ndn-repo - NDNx Repository Daemon\n"
- "-d: set database path\n"
+ /* 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 May be try to reload config file
+ signalSet.async_wait(bind(&terminate, boost::ref(ioService), _1, _2,
+ boost::ref(signalSet)));
+ }
+}
+
int
-main(int argc, char** argv) {
+main(int argc, char** argv)
+{
+ string configPath = DEFAULT_CONFIG_FILE;
int opt;
- string dbPath;
- string confPath;
- while ((opt = getopt(argc, argv, "d:hc:")) != -1) {
+ while ((opt = getopt(argc, argv, "hc:")) != -1) {
switch (opt) {
- case 'd':
- dbPath = string(optarg);
- break;
case 'h':
- std::cout << ndnRepoUsageMessage << std::endl;
+ std::cout << argv[0] << ndnRepoUsageMessage << std::endl;
return 1;
case 'c':
- confPath = string(optarg);
+ configPath = string(optarg);
break;
default:
break;
}
}
- if (confPath.empty()) {
- confPath = "./repo.conf";
+ try {
+ boost::asio::io_service ioService;
+ Repo repoInstance(ioService, 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(bind(&terminate, boost::ref(ioService), _1, _2,
+ boost::ref(signalSet)));
+
+ repoInstance.enableListening();
+
+ ioService.run();
+ }
+ catch (const std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ return 2;
}
- Name dataPrefix("ndn:/");
- Name repoPrefix("ndn:/example/repo");
- /// @todo read from configuration
-
- SqliteHandle sqliteHandle(dbPath);
-
- shared_ptr<boost::asio::io_service> io =
- ndn::make_shared<boost::asio::io_service>();
-
- Face face(io);
- Scheduler scheduler(*io);
-
- /// @todo specify trust model
- CommandInterestValidator validator;
- KeyChain keyChain;
-
- ReadHandle readHandle(face, sqliteHandle, keyChain, scheduler);
- readHandle.listen(dataPrefix);
-
- WriteHandle writeHandle(face, sqliteHandle, keyChain, scheduler, validator);
- writeHandle.listen(repoPrefix);
-
- DeleteHandle deleteHandle(face, sqliteHandle, keyChain, scheduler, validator);
- deleteHandle.listen(repoPrefix);
-
- TcpBulkInsertHandle tcpBulkInsertHandle(*io, sqliteHandle);
- tcpBulkInsertHandle.listen("localhost", "7376");
-
- face.processEvents();
return 0;
}