blob: 51ba11c20327c429ee960dfded6d72004c938753 [file] [log] [blame]
Alexander Afanasyev31367922015-02-09 20:51:10 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Licf0db022016-01-29 00:54:25 -08003 * Copyright (c) 2014-2016, 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
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
38namespace nfd {
39namespace rib {
40
41static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
42
Junxiao Shib2600172016-07-11 08:53:53 +000043Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
Alexander Afanasyev31367922015-02-09 20:51:10 -080044 : m_configFile(configFile)
45 , m_keyChain(keyChain)
46{
47}
48
Junxiao Shib2600172016-07-11 08:53:53 +000049Service::Service(const ConfigSection& config, ndn::KeyChain& keyChain)
Alexander Afanasyev31367922015-02-09 20:51:10 -080050 : m_configSection(config)
51 , m_keyChain(keyChain)
52{
53}
54
Junxiao Shib2600172016-07-11 08:53:53 +000055Service::~Service()
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080056{
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 Afanasyev31367922015-02-09 20:51:10 -080062void
Junxiao Shib2600172016-07-11 08:53:53 +000063Service::initialize()
Alexander Afanasyev31367922015-02-09 20:51:10 -080064{
65 m_face.reset(new ndn::Face(getLocalNfdTransport(), getGlobalIoService(), m_keyChain));
Yanbiao Licf0db022016-01-29 00:54:25 -080066 m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_face, m_keyChain));
Alexander Afanasyev31367922015-02-09 20:51:10 -080067
68 initializeLogging();
69
Yanbiao Licf0db022016-01-29 00:54:25 -080070 m_ribManager.reset(new RibManager(*m_dispatcher, *m_face, m_keyChain));
Alexander Afanasyev31367922015-02-09 20:51:10 -080071
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 Shib2600172016-07-11 08:53:53 +000080 // missing "rib" section handler
Alexander Afanasyev31367922015-02-09 20:51:10 -080081 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 Newberryecc45cb2016-11-08 19:57:12 +000097 m_ribManager->enableLocalFields();
Alexander Afanasyev31367922015-02-09 20:51:10 -080098}
99
100void
Junxiao Shib2600172016-07-11 08:53:53 +0000101Service::initializeLogging()
Alexander Afanasyev31367922015-02-09 20:51:10 -0800102{
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
116shared_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 }
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 Mastorakis149e02c2015-07-27 13:22:22 -0700147 BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD"));
Alexander Afanasyev31367922015-02-09 20:51:10 -0800148 }
149}
150
151} // namespace rib
152} // namespace nfd