Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
| 28 | #include "rib-manager.hpp" |
| 29 | #include "core/config-file.hpp" |
| 30 | #include "core/logger-factory.hpp" |
| 31 | #include "core/global-io.hpp" |
| 32 | |
| 33 | #include <boost/property_tree/info_parser.hpp> |
| 34 | |
| 35 | #include <ndn-cxx/transport/unix-transport.hpp> |
| 36 | #include <ndn-cxx/transport/tcp-transport.hpp> |
| 37 | |
| 38 | namespace nfd { |
| 39 | namespace rib { |
| 40 | |
| 41 | static const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
| 42 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 43 | Service::Service(const std::string& configFile, ndn::KeyChain& keyChain) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 44 | : m_configFile(configFile) |
| 45 | , m_keyChain(keyChain) |
| 46 | { |
| 47 | } |
| 48 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 49 | Service::Service(const ConfigSection& config, ndn::KeyChain& keyChain) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 50 | : m_configSection(config) |
| 51 | , m_keyChain(keyChain) |
| 52 | { |
| 53 | } |
| 54 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 55 | Service::~Service() |
Alexander Afanasyev | c3ea5a7 | 2015-02-12 20:14:16 -0800 | [diff] [blame] | 56 | { |
| 57 | // It is necessary to explicitly define the destructor, because some member variables |
| 58 | // (e.g., unique_ptr<RibManager>) are forward-declared, but implicitly declared destructor |
| 59 | // requires complete types for all members when instantiated. |
| 60 | } |
| 61 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 62 | void |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 63 | Service::initialize() |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 64 | { |
| 65 | m_face.reset(new ndn::Face(getLocalNfdTransport(), getGlobalIoService(), m_keyChain)); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 66 | m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_face, m_keyChain)); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 67 | |
| 68 | initializeLogging(); |
| 69 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 70 | m_ribManager.reset(new RibManager(*m_dispatcher, *m_face, m_keyChain)); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 71 | |
| 72 | ConfigFile config([] (const std::string& filename, const std::string& sectionName, |
| 73 | const ConfigSection& section, bool isDryRun) { |
| 74 | // Ignore "log" and sections belonging to NFD, |
| 75 | // but raise an error if we're missing a handler for a "rib" section. |
| 76 | if (sectionName != "rib" || sectionName == "log") { |
| 77 | // do nothing |
| 78 | } |
| 79 | else { |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 80 | // missing "rib" section handler |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 81 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 82 | } |
| 83 | }); |
| 84 | m_ribManager->setConfigFile(config); |
| 85 | |
| 86 | // parse config file |
| 87 | if (!m_configFile.empty()) { |
| 88 | config.parse(m_configFile, true); |
| 89 | config.parse(m_configFile, false); |
| 90 | } |
| 91 | else { |
| 92 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 93 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 94 | } |
| 95 | |
| 96 | m_ribManager->registerWithNfd(); |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame^] | 97 | m_ribManager->enableLocalFields(); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 101 | Service::initializeLogging() |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 102 | { |
| 103 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
| 104 | LoggerFactory::getInstance().setConfigFile(config); |
| 105 | |
| 106 | if (!m_configFile.empty()) { |
| 107 | config.parse(m_configFile, true); |
| 108 | config.parse(m_configFile, false); |
| 109 | } |
| 110 | else { |
| 111 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 112 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 113 | } |
| 114 | } |
| 115 | |
| 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 | } |
| 126 | else |
| 127 | config = m_configSection; |
| 128 | |
| 129 | if (config.get_child_optional("face_system.unix")) { |
| 130 | // unix socket enabled |
| 131 | |
| 132 | auto&& socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock"); |
| 133 | // default socketPath should be the same as in FaceManager::processSectionUnix |
| 134 | |
| 135 | return make_shared<ndn::UnixTransport>(socketPath); |
| 136 | } |
| 137 | else if (config.get_child_optional("face_system.tcp") && |
| 138 | config.get<std::string>("face_system.tcp.listen", "yes") == "yes") { |
| 139 | // tcp is enabled |
| 140 | |
| 141 | auto&& port = config.get<std::string>("face_system.tcp.port", "6363"); |
| 142 | // default port should be the same as in FaceManager::processSectionTcp |
| 143 | |
| 144 | return make_shared<ndn::TcpTransport>("localhost", port); |
| 145 | } |
| 146 | else { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 147 | BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD")); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | } // namespace rib |
| 152 | } // namespace nfd |