Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "repo.hpp" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 21 | #include "storage/sqlite-storage.hpp" |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 22 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 23 | namespace repo { |
| 24 | |
| 25 | RepoConfig |
| 26 | parseConfig(const std::string& configPath) |
| 27 | { |
| 28 | if (configPath.empty()) { |
| 29 | std::cerr << "configuration file path is empty" << std::endl; |
| 30 | } |
| 31 | |
| 32 | std::ifstream fin(configPath.c_str()); |
| 33 | if (!fin.is_open()) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 34 | BOOST_THROW_EXCEPTION(Repo::Error("failed to open configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 35 | |
| 36 | using namespace boost::property_tree; |
| 37 | ptree propertyTree; |
| 38 | try { |
| 39 | read_info(fin, propertyTree); |
| 40 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 41 | catch (const ptree_error& e) { |
| 42 | BOOST_THROW_EXCEPTION(Repo::Error("failed to read configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ptree repoConf = propertyTree.get_child("repo"); |
| 46 | |
| 47 | RepoConfig repoConfig; |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 48 | repoConfig.repoConfigPath = configPath; |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 49 | |
| 50 | ptree dataConf = repoConf.get_child("data"); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 51 | for (ptree::const_iterator it = dataConf.begin(); |
| 52 | it != dataConf.end(); |
| 53 | ++it) |
| 54 | { |
| 55 | if (it->first == "prefix") |
| 56 | repoConfig.dataPrefixes.push_back(Name(it->second.get_value<std::string>())); |
| 57 | else |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 58 | BOOST_THROW_EXCEPTION(Repo::Error("Unrecognized '" + it->first + "' option in 'data' section in " |
| 59 | "configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ptree commandConf = repoConf.get_child("command"); |
| 63 | for (ptree::const_iterator it = commandConf.begin(); |
| 64 | it != commandConf.end(); |
| 65 | ++it) |
| 66 | { |
| 67 | if (it->first == "prefix") |
| 68 | repoConfig.repoPrefixes.push_back(Name(it->second.get_value<std::string>())); |
| 69 | else |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 70 | BOOST_THROW_EXCEPTION(Repo::Error("Unrecognized '" + it->first + "' option in 'command' section in " |
| 71 | "configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | ptree tcpBulkInsert = repoConf.get_child("tcp_bulk_insert"); |
| 75 | bool isTcpBulkEnabled = false; |
| 76 | std::string host = "localhost"; |
| 77 | std::string port = "7376"; |
| 78 | for (ptree::const_iterator it = tcpBulkInsert.begin(); |
| 79 | it != tcpBulkInsert.end(); |
| 80 | ++it) |
| 81 | { |
| 82 | isTcpBulkEnabled = true; |
| 83 | |
| 84 | // tcp_bulk_insert { |
| 85 | // host "localhost" ; IP address or hostname to listen on |
| 86 | // port 7635 ; Port number to listen on |
| 87 | // } |
| 88 | if (it->first == "host") { |
| 89 | host = it->second.get_value<std::string>(); |
| 90 | } |
| 91 | else if (it->first == "port") { |
| 92 | port = it->second.get_value<std::string>(); |
| 93 | } |
| 94 | else |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 95 | BOOST_THROW_EXCEPTION(Repo::Error("Unrecognized '" + it->first + "' option in 'tcp_bulk_insert' section in " |
| 96 | "configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 97 | } |
| 98 | if (isTcpBulkEnabled) { |
| 99 | repoConfig.tcpBulkInsertEndpoints.push_back(std::make_pair(host, port)); |
| 100 | } |
| 101 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 102 | if (repoConf.get<std::string>("storage.method") != "sqlite") { |
| 103 | BOOST_THROW_EXCEPTION(Repo::Error("Only 'sqlite' storage method is supported")); |
| 104 | } |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 105 | |
| 106 | repoConfig.dbPath = repoConf.get<std::string>("storage.path"); |
| 107 | |
| 108 | repoConfig.validatorNode = repoConf.get_child("validator"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 109 | |
Shuo Chen | 6e81812 | 2015-05-04 10:40:03 +0800 | [diff] [blame] | 110 | repoConfig.nMaxPackets = repoConf.get<uint64_t>("storage.max-packets"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 111 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 112 | return repoConfig; |
| 113 | } |
| 114 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 115 | Repo::Repo(boost::asio::io_service& ioService, const RepoConfig& config) |
| 116 | : m_config(config) |
| 117 | , m_scheduler(ioService) |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 118 | , m_face(ioService) |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 119 | , m_store(std::make_shared<SqliteStorage>(config.dbPath)) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 120 | , m_storageHandle(config.nMaxPackets, *m_store) |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 121 | , m_validator(m_face) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 122 | , m_readHandle(m_face, m_storageHandle, m_keyChain, m_scheduler) |
| 123 | , m_writeHandle(m_face, m_storageHandle, m_keyChain, m_scheduler, m_validator) |
Weiqi Shi | 098f91c | 2014-07-23 17:41:35 -0700 | [diff] [blame] | 124 | , m_watchHandle(m_face, m_storageHandle, m_keyChain, m_scheduler, m_validator) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 125 | , m_deleteHandle(m_face, m_storageHandle, m_keyChain, m_scheduler, m_validator) |
| 126 | , m_tcpBulkInsertHandle(ioService, m_storageHandle) |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 127 | |
| 128 | { |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 129 | this->enableValidation(); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
Shuo Chen | a12f528 | 2014-08-01 15:18:30 +0800 | [diff] [blame] | 133 | Repo::initializeStorage() |
| 134 | { |
| 135 | // Rebuild storage if storage checkpoin exists |
| 136 | ndn::time::steady_clock::TimePoint start = ndn::time::steady_clock::now(); |
| 137 | m_storageHandle.initialize(); |
| 138 | ndn::time::steady_clock::TimePoint end = ndn::time::steady_clock::now(); |
| 139 | ndn::time::milliseconds cost = ndn::time::duration_cast<ndn::time::milliseconds>(end - start); |
| 140 | std::cerr << "initialize storage cost: " << cost << "ms" << std::endl; |
| 141 | } |
| 142 | |
| 143 | void |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 144 | Repo::enableListening() |
| 145 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 146 | for (const ndn::Name& dataPrefix : m_config.dataPrefixes) { |
| 147 | // ReadHandle performs prefix registration internally. |
| 148 | m_readHandle.listen(dataPrefix); |
| 149 | } |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 150 | |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 151 | for (const ndn::Name& cmdPrefix : m_config.repoPrefixes) { |
| 152 | m_face.registerPrefix(cmdPrefix, nullptr, |
| 153 | [] (const Name& cmdPrefix, const std::string& reason) { |
| 154 | std::cerr << "Command prefix " << cmdPrefix << " registration error: " << reason << std::endl; |
| 155 | BOOST_THROW_EXCEPTION(Error("Command prefix registration failed")); |
| 156 | }); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 158 | m_writeHandle.listen(cmdPrefix); |
| 159 | m_watchHandle.listen(cmdPrefix); |
| 160 | m_deleteHandle.listen(cmdPrefix); |
| 161 | } |
| 162 | |
| 163 | for (const auto& ep : m_config.tcpBulkInsertEndpoints) { |
| 164 | m_tcpBulkInsertHandle.listen(ep.first, ep.second); |
| 165 | } |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 168 | void |
| 169 | Repo::enableValidation() |
| 170 | { |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 171 | m_validator.load(m_config.validatorNode, m_config.repoConfigPath); |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 174 | } // namespace repo |