Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 26 | #include "service.hpp" |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 27 | #include "rib-manager.hpp" |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
| 29 | |
| 30 | #include <boost/property_tree/info_parser.hpp> |
| 31 | |
| 32 | #include <ndn-cxx/transport/unix-transport.hpp> |
| 33 | #include <ndn-cxx/transport/tcp-transport.hpp> |
| 34 | |
| 35 | namespace nfd { |
| 36 | namespace rib { |
| 37 | |
| 38 | static const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
| 39 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame^] | 40 | Service* Service::s_instance = nullptr; |
| 41 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 42 | Service::Service(const std::string& configFile, ndn::KeyChain& keyChain) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 43 | : m_configFile(configFile) |
| 44 | , m_keyChain(keyChain) |
| 45 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame^] | 46 | if (s_instance != nullptr) { |
| 47 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once")); |
| 48 | } |
| 49 | if (&getGlobalIoService() != &getRibIoService()) { |
| 50 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread")); |
| 51 | } |
| 52 | s_instance = this; |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 55 | Service::Service(const ConfigSection& config, ndn::KeyChain& keyChain) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 56 | : m_configSection(config) |
| 57 | , m_keyChain(keyChain) |
| 58 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame^] | 59 | if (s_instance != nullptr) { |
| 60 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once")); |
| 61 | } |
| 62 | if (&getGlobalIoService() != &getRibIoService()) { |
| 63 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread")); |
| 64 | } |
| 65 | s_instance = this; |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame^] | 68 | Service::~Service() |
| 69 | { |
| 70 | s_instance = nullptr; |
| 71 | } |
| 72 | |
| 73 | Service& |
| 74 | Service::get() |
| 75 | { |
| 76 | if (s_instance == nullptr) { |
| 77 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service is not instantiated")); |
| 78 | } |
| 79 | if (&getGlobalIoService() != &getRibIoService()) { |
| 80 | BOOST_THROW_EXCEPTION(std::logic_error("Must get RIB service on RIB thread")); |
| 81 | } |
| 82 | return *s_instance; |
| 83 | } |
Alexander Afanasyev | c3ea5a7 | 2015-02-12 20:14:16 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 85 | void |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 86 | Service::initialize() |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 87 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 88 | m_face = make_unique<ndn::Face>(getLocalNfdTransport(), getGlobalIoService(), m_keyChain); |
| 89 | m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_face, m_keyChain); |
| 90 | m_ribManager = make_unique<RibManager>(*m_dispatcher, *m_face, m_keyChain); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 91 | |
| 92 | ConfigFile config([] (const std::string& filename, const std::string& sectionName, |
| 93 | const ConfigSection& section, bool isDryRun) { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 94 | // Ignore sections belonging to NFD, but raise an error |
| 95 | // if we're missing a handler for a "rib" section. |
| 96 | if (sectionName == "rib") { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 97 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 98 | } |
| 99 | }); |
| 100 | m_ribManager->setConfigFile(config); |
| 101 | |
| 102 | // parse config file |
| 103 | if (!m_configFile.empty()) { |
| 104 | config.parse(m_configFile, true); |
| 105 | config.parse(m_configFile, false); |
| 106 | } |
| 107 | else { |
| 108 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 109 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 110 | } |
| 111 | |
| 112 | m_ribManager->registerWithNfd(); |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 113 | m_ribManager->enableLocalFields(); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 116 | shared_ptr<ndn::Transport> |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 117 | Service::getLocalNfdTransport() |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 118 | { |
| 119 | ConfigSection config; |
| 120 | |
| 121 | if (!m_configFile.empty()) { |
| 122 | // Any format errors should have been caught already |
| 123 | // If error is thrown at this point, it is development error |
| 124 | boost::property_tree::read_info(m_configFile, config); |
| 125 | } |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 126 | else { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 127 | config = m_configSection; |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 128 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 129 | |
| 130 | if (config.get_child_optional("face_system.unix")) { |
| 131 | // unix socket enabled |
| 132 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 133 | auto socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock"); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 134 | // default socketPath should be the same as in FaceManager::processSectionUnix |
| 135 | |
| 136 | return make_shared<ndn::UnixTransport>(socketPath); |
| 137 | } |
| 138 | else if (config.get_child_optional("face_system.tcp") && |
| 139 | config.get<std::string>("face_system.tcp.listen", "yes") == "yes") { |
| 140 | // tcp is enabled |
| 141 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 142 | auto port = config.get<std::string>("face_system.tcp.port", "6363"); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 143 | // default port should be the same as in FaceManager::processSectionTcp |
| 144 | |
| 145 | return make_shared<ndn::TcpTransport>("localhost", port); |
| 146 | } |
| 147 | else { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 148 | BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD")); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | } // namespace rib |
| 153 | } // namespace nfd |