blob: dce806f00e766aa92891bd89ed4cdbacae67581d [file] [log] [blame]
Alexander Afanasyev31367922015-02-09 20:51:10 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Alexander Afanasyev31367922015-02-09 20:51:10 -08004 * 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 Shib2600172016-07-11 08:53:53 +000026#include "service.hpp"
Alexander Afanasyev31367922015-02-09 20:51:10 -080027#include "rib-manager.hpp"
Alexander Afanasyev31367922015-02-09 20:51:10 -080028#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
35namespace nfd {
36namespace rib {
37
38static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
39
Junxiao Shib2600172016-07-11 08:53:53 +000040Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
Alexander Afanasyev31367922015-02-09 20:51:10 -080041 : m_configFile(configFile)
42 , m_keyChain(keyChain)
43{
44}
45
Junxiao Shib2600172016-07-11 08:53:53 +000046Service::Service(const ConfigSection& config, ndn::KeyChain& keyChain)
Alexander Afanasyev31367922015-02-09 20:51:10 -080047 : m_configSection(config)
48 , m_keyChain(keyChain)
49{
50}
51
Davide Pesaventoa3148082018-04-12 18:21:54 -040052// It is necessary to explicitly define the destructor, because some member variables
53// (e.g., unique_ptr<RibManager>) are forward-declared, but implicitly declared destructor
54// requires complete types for all members when instantiated.
55Service::~Service() = default;
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080056
Alexander Afanasyev31367922015-02-09 20:51:10 -080057void
Junxiao Shib2600172016-07-11 08:53:53 +000058Service::initialize()
Alexander Afanasyev31367922015-02-09 20:51:10 -080059{
Davide Pesaventoa3148082018-04-12 18:21:54 -040060 m_face = make_unique<ndn::Face>(getLocalNfdTransport(), getGlobalIoService(), m_keyChain);
61 m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_face, m_keyChain);
62 m_ribManager = make_unique<RibManager>(*m_dispatcher, *m_face, m_keyChain);
Alexander Afanasyev31367922015-02-09 20:51:10 -080063
64 ConfigFile config([] (const std::string& filename, const std::string& sectionName,
65 const ConfigSection& section, bool isDryRun) {
Davide Pesaventoa3148082018-04-12 18:21:54 -040066 // Ignore sections belonging to NFD, but raise an error
67 // if we're missing a handler for a "rib" section.
68 if (sectionName == "rib") {
Alexander Afanasyev31367922015-02-09 20:51:10 -080069 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
70 }
71 });
72 m_ribManager->setConfigFile(config);
73
74 // parse config file
75 if (!m_configFile.empty()) {
76 config.parse(m_configFile, true);
77 config.parse(m_configFile, false);
78 }
79 else {
80 config.parse(m_configSection, true, INTERNAL_CONFIG);
81 config.parse(m_configSection, false, INTERNAL_CONFIG);
82 }
83
84 m_ribManager->registerWithNfd();
Eric Newberryecc45cb2016-11-08 19:57:12 +000085 m_ribManager->enableLocalFields();
Alexander Afanasyev31367922015-02-09 20:51:10 -080086}
87
Alexander Afanasyev31367922015-02-09 20:51:10 -080088shared_ptr<ndn::Transport>
Junxiao Shib2600172016-07-11 08:53:53 +000089Service::getLocalNfdTransport()
Alexander Afanasyev31367922015-02-09 20:51:10 -080090{
91 ConfigSection config;
92
93 if (!m_configFile.empty()) {
94 // Any format errors should have been caught already
95 // If error is thrown at this point, it is development error
96 boost::property_tree::read_info(m_configFile, config);
97 }
Davide Pesaventoa3148082018-04-12 18:21:54 -040098 else {
Alexander Afanasyev31367922015-02-09 20:51:10 -080099 config = m_configSection;
Davide Pesaventoa3148082018-04-12 18:21:54 -0400100 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800101
102 if (config.get_child_optional("face_system.unix")) {
103 // unix socket enabled
104
Davide Pesaventoa3148082018-04-12 18:21:54 -0400105 auto socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
Alexander Afanasyev31367922015-02-09 20:51:10 -0800106 // default socketPath should be the same as in FaceManager::processSectionUnix
107
108 return make_shared<ndn::UnixTransport>(socketPath);
109 }
110 else if (config.get_child_optional("face_system.tcp") &&
111 config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
112 // tcp is enabled
113
Davide Pesaventoa3148082018-04-12 18:21:54 -0400114 auto port = config.get<std::string>("face_system.tcp.port", "6363");
Alexander Afanasyev31367922015-02-09 20:51:10 -0800115 // default port should be the same as in FaceManager::processSectionTcp
116
117 return make_shared<ndn::TcpTransport>("localhost", port);
118 }
119 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700120 BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD"));
Alexander Afanasyev31367922015-02-09 20:51:10 -0800121 }
122}
123
124} // namespace rib
125} // namespace nfd