blob: de103b4abe6fba1e925c1bd12ba98f7e31c28416 [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
Teng Liang04d5ce62018-08-06 10:20:24 +080040Service* Service::s_instance = nullptr;
41
Junxiao Shib2600172016-07-11 08:53:53 +000042Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
Alexander Afanasyev31367922015-02-09 20:51:10 -080043 : m_configFile(configFile)
44 , m_keyChain(keyChain)
45{
Teng Liang04d5ce62018-08-06 10:20:24 +080046 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 Afanasyev31367922015-02-09 20:51:10 -080053}
54
Junxiao Shib2600172016-07-11 08:53:53 +000055Service::Service(const ConfigSection& config, ndn::KeyChain& keyChain)
Alexander Afanasyev31367922015-02-09 20:51:10 -080056 : m_configSection(config)
57 , m_keyChain(keyChain)
58{
Teng Liang04d5ce62018-08-06 10:20:24 +080059 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 Afanasyev31367922015-02-09 20:51:10 -080066}
67
Teng Liang04d5ce62018-08-06 10:20:24 +080068Service::~Service()
69{
70 s_instance = nullptr;
71}
72
73Service&
74Service::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 Afanasyevc3ea5a72015-02-12 20:14:16 -080084
Alexander Afanasyev31367922015-02-09 20:51:10 -080085void
Junxiao Shib2600172016-07-11 08:53:53 +000086Service::initialize()
Alexander Afanasyev31367922015-02-09 20:51:10 -080087{
Davide Pesaventoa3148082018-04-12 18:21:54 -040088 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 Afanasyev31367922015-02-09 20:51:10 -080091
92 ConfigFile config([] (const std::string& filename, const std::string& sectionName,
93 const ConfigSection& section, bool isDryRun) {
Davide Pesaventoa3148082018-04-12 18:21:54 -040094 // 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 Afanasyev31367922015-02-09 20:51:10 -080097 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 Newberryecc45cb2016-11-08 19:57:12 +0000113 m_ribManager->enableLocalFields();
Alexander Afanasyev31367922015-02-09 20:51:10 -0800114}
115
Alexander Afanasyev31367922015-02-09 20:51:10 -0800116shared_ptr<ndn::Transport>
Junxiao Shib2600172016-07-11 08:53:53 +0000117Service::getLocalNfdTransport()
Alexander Afanasyev31367922015-02-09 20:51:10 -0800118{
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 Pesaventoa3148082018-04-12 18:21:54 -0400126 else {
Alexander Afanasyev31367922015-02-09 20:51:10 -0800127 config = m_configSection;
Davide Pesaventoa3148082018-04-12 18:21:54 -0400128 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800129
130 if (config.get_child_optional("face_system.unix")) {
131 // unix socket enabled
132
Davide Pesaventoa3148082018-04-12 18:21:54 -0400133 auto socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
Alexander Afanasyev31367922015-02-09 20:51:10 -0800134 // 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 Pesaventoa3148082018-04-12 18:21:54 -0400142 auto port = config.get<std::string>("face_system.tcp.port", "6363");
Alexander Afanasyev31367922015-02-09 20:51:10 -0800143 // default port should be the same as in FaceManager::processSectionTcp
144
145 return make_shared<ndn::TcpTransport>("localhost", port);
146 }
147 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700148 BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD"));
Alexander Afanasyev31367922015-02-09 20:51:10 -0800149 }
150}
151
152} // namespace rib
153} // namespace nfd