Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "repo.hpp" |
| 8 | |
| 9 | namespace repo { |
| 10 | |
| 11 | RepoConfig |
| 12 | parseConfig(const std::string& configPath) |
| 13 | { |
| 14 | if (configPath.empty()) { |
| 15 | std::cerr << "configuration file path is empty" << std::endl; |
| 16 | } |
| 17 | |
| 18 | std::ifstream fin(configPath.c_str()); |
| 19 | if (!fin.is_open()) |
| 20 | throw Repo::Error("failed to open configuration file '"+ configPath +"'"); |
| 21 | |
| 22 | using namespace boost::property_tree; |
| 23 | ptree propertyTree; |
| 24 | try { |
| 25 | read_info(fin, propertyTree); |
| 26 | } |
| 27 | catch (ptree_error& e) { |
| 28 | throw Repo::Error("failed to read configuration file '"+ configPath +"'"); |
| 29 | } |
| 30 | |
| 31 | ptree repoConf = propertyTree.get_child("repo"); |
| 32 | |
| 33 | RepoConfig repoConfig; |
| 34 | |
| 35 | ptree dataConf = repoConf.get_child("data"); |
| 36 | |
| 37 | for (ptree::const_iterator it = dataConf.begin(); |
| 38 | it != dataConf.end(); |
| 39 | ++it) |
| 40 | { |
| 41 | if (it->first == "prefix") |
| 42 | repoConfig.dataPrefixes.push_back(Name(it->second.get_value<std::string>())); |
| 43 | else |
| 44 | throw Repo::Error("Unrecognized '" + it->first + "' option in 'data' section in " |
| 45 | "configuration file '"+ configPath +"'"); |
| 46 | } |
| 47 | |
| 48 | ptree commandConf = repoConf.get_child("command"); |
| 49 | for (ptree::const_iterator it = commandConf.begin(); |
| 50 | it != commandConf.end(); |
| 51 | ++it) |
| 52 | { |
| 53 | if (it->first == "prefix") |
| 54 | repoConfig.repoPrefixes.push_back(Name(it->second.get_value<std::string>())); |
| 55 | else |
| 56 | throw Repo::Error("Unrecognized '" + it->first + "' option in 'command' section in " |
| 57 | "configuration file '"+ configPath +"'"); |
| 58 | } |
| 59 | |
| 60 | ptree tcpBulkInsert = repoConf.get_child("tcp_bulk_insert"); |
| 61 | bool isTcpBulkEnabled = false; |
| 62 | std::string host = "localhost"; |
| 63 | std::string port = "7376"; |
| 64 | for (ptree::const_iterator it = tcpBulkInsert.begin(); |
| 65 | it != tcpBulkInsert.end(); |
| 66 | ++it) |
| 67 | { |
| 68 | isTcpBulkEnabled = true; |
| 69 | |
| 70 | // tcp_bulk_insert { |
| 71 | // host "localhost" ; IP address or hostname to listen on |
| 72 | // port 7635 ; Port number to listen on |
| 73 | // } |
| 74 | if (it->first == "host") { |
| 75 | host = it->second.get_value<std::string>(); |
| 76 | } |
| 77 | else if (it->first == "port") { |
| 78 | port = it->second.get_value<std::string>(); |
| 79 | } |
| 80 | else |
| 81 | throw Repo::Error("Unrecognized '" + it->first + "' option in 'tcp_bulk_insert' section in " |
| 82 | "configuration file '"+ configPath +"'"); |
| 83 | } |
| 84 | if (isTcpBulkEnabled) { |
| 85 | repoConfig.tcpBulkInsertEndpoints.push_back(std::make_pair(host, port)); |
| 86 | } |
| 87 | |
| 88 | if (repoConf.get<std::string>("storage.method") != "sqlite") |
| 89 | throw Repo::Error("Only 'sqlite' storage method is supported"); |
| 90 | |
| 91 | repoConfig.dbPath = repoConf.get<std::string>("storage.path"); |
| 92 | |
| 93 | repoConfig.validatorNode = repoConf.get_child("validator"); |
| 94 | return repoConfig; |
| 95 | } |
| 96 | |
| 97 | inline static void |
| 98 | NullDeleter(boost::asio::io_service* variable) |
| 99 | { |
| 100 | // do nothing |
| 101 | } |
| 102 | |
| 103 | Repo::Repo(boost::asio::io_service& ioService, const RepoConfig& config) |
| 104 | : m_config(config) |
| 105 | , m_scheduler(ioService) |
| 106 | , m_face(shared_ptr<boost::asio::io_service>(&ioService, &NullDeleter)) |
| 107 | , m_storageHandle(openStorage(config)) |
| 108 | , m_readHandle(m_face, *m_storageHandle, m_keyChain, m_scheduler) |
| 109 | , m_writeHandle(m_face, *m_storageHandle, m_keyChain, m_scheduler, m_validator) |
| 110 | , m_deleteHandle(m_face, *m_storageHandle, m_keyChain, m_scheduler, m_validator) |
| 111 | , m_tcpBulkInsertHandle(ioService, *m_storageHandle) |
| 112 | |
| 113 | { |
| 114 | //Trust model not implemented, this is just an empty validator |
| 115 | //@todo add a function to parse RepoConfig.validatorNode and define the trust model |
| 116 | m_validator.addInterestRule("^<>", |
| 117 | *m_keyChain. |
| 118 | getCertificate(m_keyChain.getDefaultCertificateName())); |
| 119 | } |
| 120 | |
| 121 | shared_ptr<StorageHandle> |
| 122 | Repo::openStorage(const RepoConfig& config) |
| 123 | { |
| 124 | shared_ptr<StorageHandle> storageHandle = ndn::make_shared<SqliteHandle>(config.dbPath); |
| 125 | return storageHandle; |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | Repo::enableListening() |
| 130 | { |
| 131 | // Enable "listening" on Data prefixes |
| 132 | for (vector<ndn::Name>::iterator it = m_config.dataPrefixes.begin(); |
| 133 | it != m_config.dataPrefixes.end(); |
| 134 | ++it) |
| 135 | { |
| 136 | m_readHandle.listen(*it); |
| 137 | } |
| 138 | |
| 139 | // Enable "listening" on control prefixes |
| 140 | for (vector<ndn::Name>::iterator it = m_config.repoPrefixes.begin(); |
| 141 | it != m_config.repoPrefixes.end(); |
| 142 | ++it) |
| 143 | { |
| 144 | m_writeHandle.listen(*it); |
| 145 | m_deleteHandle.listen(*it); |
| 146 | } |
| 147 | |
| 148 | // Enable listening on TCP bulk insert addresses |
| 149 | for (vector<pair<string, string> >::iterator it = m_config.tcpBulkInsertEndpoints.begin(); |
| 150 | it != m_config.tcpBulkInsertEndpoints.end(); |
| 151 | ++it) |
| 152 | { |
| 153 | m_tcpBulkInsertHandle.listen(it->first, it->second); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | } // namespace repo |