blob: be1f527b6bb5bfc976dad2982ffbae51dc3f8167 [file] [log] [blame]
Alexander Afanasyev31367922015-02-09 20:51:10 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
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
26#include "nrd.hpp"
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
38namespace nfd {
39namespace rib {
40
41static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
42
43Nrd::Nrd(const std::string& configFile, ndn::KeyChain& keyChain)
44 : m_configFile(configFile)
45 , m_keyChain(keyChain)
46{
47}
48
49Nrd::Nrd(const ConfigSection& config, ndn::KeyChain& keyChain)
50 : m_configSection(config)
51 , m_keyChain(keyChain)
52{
53}
54
55void
56Nrd::initialize()
57{
58 m_face.reset(new ndn::Face(getLocalNfdTransport(), getGlobalIoService(), m_keyChain));
59
60 initializeLogging();
61
62 m_ribManager.reset(new RibManager(*m_face));
63
64 ConfigFile config([] (const std::string& filename, const std::string& sectionName,
65 const ConfigSection& section, bool isDryRun) {
66 // Ignore "log" and sections belonging to NFD,
67 // but raise an error if we're missing a handler for a "rib" section.
68 if (sectionName != "rib" || sectionName == "log") {
69 // do nothing
70 }
71 else {
72 // missing NRD section
73 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
74 }
75 });
76 m_ribManager->setConfigFile(config);
77
78 // parse config file
79 if (!m_configFile.empty()) {
80 config.parse(m_configFile, true);
81 config.parse(m_configFile, false);
82 }
83 else {
84 config.parse(m_configSection, true, INTERNAL_CONFIG);
85 config.parse(m_configSection, false, INTERNAL_CONFIG);
86 }
87
88 m_ribManager->registerWithNfd();
89 m_ribManager->enableLocalControlHeader();
90}
91
92void
93Nrd::initializeLogging()
94{
95 ConfigFile config(&ConfigFile::ignoreUnknownSection);
96 LoggerFactory::getInstance().setConfigFile(config);
97
98 if (!m_configFile.empty()) {
99 config.parse(m_configFile, true);
100 config.parse(m_configFile, false);
101 }
102 else {
103 config.parse(m_configSection, true, INTERNAL_CONFIG);
104 config.parse(m_configSection, false, INTERNAL_CONFIG);
105 }
106}
107
108shared_ptr<ndn::Transport>
109Nrd::getLocalNfdTransport()
110{
111 ConfigSection config;
112
113 if (!m_configFile.empty()) {
114 // Any format errors should have been caught already
115 // If error is thrown at this point, it is development error
116 boost::property_tree::read_info(m_configFile, config);
117 }
118 else
119 config = m_configSection;
120
121 if (config.get_child_optional("face_system.unix")) {
122 // unix socket enabled
123
124 auto&& socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
125 // default socketPath should be the same as in FaceManager::processSectionUnix
126
127 return make_shared<ndn::UnixTransport>(socketPath);
128 }
129 else if (config.get_child_optional("face_system.tcp") &&
130 config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
131 // tcp is enabled
132
133 auto&& port = config.get<std::string>("face_system.tcp.port", "6363");
134 // default port should be the same as in FaceManager::processSectionTcp
135
136 return make_shared<ndn::TcpTransport>("localhost", port);
137 }
138 else {
139 throw Error("No transport is available to communicate with NFD");
140 }
141}
142
143} // namespace rib
144} // namespace nfd