blob: bcee83cd89e6eabc77da57faa72915d42c1aa213 [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
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080055Nrd::~Nrd()
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 Afanasyev31367922015-02-09 20:51:10 -080062void
63Nrd::initialize()
64{
65 m_face.reset(new ndn::Face(getLocalNfdTransport(), getGlobalIoService(), m_keyChain));
66
67 initializeLogging();
68
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050069 m_ribManager.reset(new RibManager(*m_face, m_keyChain));
Alexander Afanasyev31367922015-02-09 20:51:10 -080070
71 ConfigFile config([] (const std::string& filename, const std::string& sectionName,
72 const ConfigSection& section, bool isDryRun) {
73 // Ignore "log" and sections belonging to NFD,
74 // but raise an error if we're missing a handler for a "rib" section.
75 if (sectionName != "rib" || sectionName == "log") {
76 // do nothing
77 }
78 else {
79 // missing NRD section
80 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
81 }
82 });
83 m_ribManager->setConfigFile(config);
84
85 // parse config file
86 if (!m_configFile.empty()) {
87 config.parse(m_configFile, true);
88 config.parse(m_configFile, false);
89 }
90 else {
91 config.parse(m_configSection, true, INTERNAL_CONFIG);
92 config.parse(m_configSection, false, INTERNAL_CONFIG);
93 }
94
95 m_ribManager->registerWithNfd();
96 m_ribManager->enableLocalControlHeader();
97}
98
99void
100Nrd::initializeLogging()
101{
102 ConfigFile config(&ConfigFile::ignoreUnknownSection);
103 LoggerFactory::getInstance().setConfigFile(config);
104
105 if (!m_configFile.empty()) {
106 config.parse(m_configFile, true);
107 config.parse(m_configFile, false);
108 }
109 else {
110 config.parse(m_configSection, true, INTERNAL_CONFIG);
111 config.parse(m_configSection, false, INTERNAL_CONFIG);
112 }
113}
114
115shared_ptr<ndn::Transport>
116Nrd::getLocalNfdTransport()
117{
118 ConfigSection config;
119
120 if (!m_configFile.empty()) {
121 // Any format errors should have been caught already
122 // If error is thrown at this point, it is development error
123 boost::property_tree::read_info(m_configFile, config);
124 }
125 else
126 config = m_configSection;
127
128 if (config.get_child_optional("face_system.unix")) {
129 // unix socket enabled
130
131 auto&& socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
132 // default socketPath should be the same as in FaceManager::processSectionUnix
133
134 return make_shared<ndn::UnixTransport>(socketPath);
135 }
136 else if (config.get_child_optional("face_system.tcp") &&
137 config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
138 // tcp is enabled
139
140 auto&& port = config.get<std::string>("face_system.tcp.port", "6363");
141 // default port should be the same as in FaceManager::processSectionTcp
142
143 return make_shared<ndn::TcpTransport>("localhost", port);
144 }
145 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700146 BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD"));
Alexander Afanasyev31367922015-02-09 20:51:10 -0800147 }
148}
149
150} // namespace rib
151} // namespace nfd