37 #include <boost/property_tree/info_parser.hpp>
38 #include <ndn-cxx/transport/tcp-transport.hpp>
39 #include <ndn-cxx/transport/unix-transport.hpp>
46 Service* Service::s_instance =
nullptr;
63 boost::property_tree::read_info(filename, config);
71 static shared_ptr<ndn::Transport>
74 if (config.get_child_optional(
"face_system.unix")) {
77 auto path = config.get<std::string>(
"face_system.unix.path",
"/run/nfd.sock");
79 auto path = config.get<std::string>(
"face_system.unix.path",
"/var/run/nfd.sock");
81 return make_shared<ndn::UnixTransport>(path);
83 else if (config.get_child_optional(
"face_system.tcp") &&
84 config.get<std::string>(
"face_system.tcp.listen",
"yes") ==
"yes") {
86 auto port = config.get<std::string>(
"face_system.tcp.port",
"6363");
87 return make_shared<ndn::TcpTransport>(
"localhost", port);
90 NDN_THROW(
ConfigFile::Error(
"No transport is available to communicate with NFD"));
96 [&configFile] (
ConfigFile& config, bool isDryRun) {
97 config.parse(configFile, isDryRun);
104 [&configSection] (
ConfigFile& config, bool isDryRun) {
105 config.parse(configSection, isDryRun,
"internal://nfd.conf");
110 template<
typename ConfigParseFunc>
111 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
112 const ConfigParseFunc& configParse)
113 : m_keyChain(keyChain)
115 , m_nfdController(m_face, m_keyChain)
116 , m_fibUpdater(m_rib, m_nfdController)
117 , m_dispatcher(m_face, m_keyChain)
118 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
120 if (s_instance !=
nullptr) {
121 NDN_THROW(std::logic_error(
"RIB service cannot be instantiated more than once"));
124 NDN_THROW(std::logic_error(
"RIB service must run on RIB thread"));
129 config.addSectionHandler(
CFG_RIB, [
this] (
auto&&... args) {
130 processConfig(std::forward<decltype(args)>(args)...);
132 configParse(config,
true);
133 configParse(config,
false);
141 s_instance =
nullptr;
147 if (s_instance ==
nullptr) {
148 NDN_THROW(std::logic_error(
"RIB service is not instantiated"));
151 NDN_THROW(std::logic_error(
"Must get RIB service on RIB thread"));
157 Service::processConfig(
const ConfigSection& section,
bool isDryRun,
const std::string& filename)
160 checkConfig(section, filename);
163 applyConfig(section, filename);
168 Service::checkConfig(
const ConfigSection& section,
const std::string& filename)
170 bool hasLocalhop =
false;
171 bool hasPropagate =
false;
173 for (
const auto& item : section) {
174 const std::string& key = item.first;
177 ndn::ValidatorConfig testValidator(m_face);
178 testValidator.load(value, filename);
182 ndn::ValidatorConfig testValidator(m_face);
183 testValidator.load(value, filename);
193 NDN_THROW(ConfigFile::Error(
"Unrecognized option " +
CFG_RIB +
"." + key));
197 if (hasLocalhop && hasPropagate) {
199 " cannot be enabled at the same time"));
204 Service::applyConfig(
const ConfigSection& section,
const std::string& filename)
206 bool wantPrefixPropagate =
false;
207 bool wantReadvertiseNlsr =
false;
209 for (
const auto& item : section) {
210 const std::string& key = item.first;
222 wantPrefixPropagate =
true;
224 if (!m_readvertisePropagation) {
227 auto cost = item.second.get_optional<uint64_t>(
"cost");
228 auto parameters = ndn::nfd::ControlParameters()
230 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
232 auto timeout = item.second.get_optional<uint64_t>(
"timeout");
233 auto options = ndn::nfd::CommandOptions()
237 m_readvertisePropagation = make_unique<Readvertise>(
239 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
240 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
247 NDN_THROW(ConfigFile::Error(
"Unrecognized option " +
CFG_RIB +
"." + key));
251 if (!wantPrefixPropagate && m_readvertisePropagation !=
nullptr) {
253 m_readvertisePropagation.reset();
256 if (wantReadvertiseNlsr && m_readvertiseNlsr ==
nullptr) {
259 m_readvertiseNlsr = make_unique<Readvertise>(
261 make_unique<ClientToNlsrReadvertisePolicy>(),
262 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
264 else if (!wantReadvertiseNlsr && m_readvertiseNlsr !=
nullptr) {
266 m_readvertiseNlsr.reset();
configuration file parsing utility
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a config option that can be either "yes" or "no"
void registerWithNfd()
Start accepting commands and dataset requests.
static const Name LOCALHOP_TOP_PREFIX
void applyLocalhostConfig(const ConfigSection §ion, const std::string &filename)
Apply localhost_security configuration.
void enableLocalhop(const ConfigSection §ion, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix.
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands.
void applyPaConfig(const ConfigSection §ion, const std::string &filename)
Apply prefix_announcement_validation configuration.
initializes and executes NFD-RIB service thread
Service(const std::string &configFile, ndn::KeyChain &keyChain)
create NFD-RIB service
static Service & get()
Get a reference to the only instance of this class.
#define NFD_LOG_INIT(name)
const std::string CFG_PA_VALIDATION
const std::string CFG_LOCALHOST_SECURITY
const Name READVERTISE_NLSR_PREFIX
static shared_ptr< ndn::Transport > makeLocalNfdTransport(const ConfigSection &config)
Look into the config file and construct appropriate transport to communicate with NFD If NFD-RIB inst...
const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
const std::string CFG_RIB
const std::string CFG_PREFIX_PROPAGATE
const std::string CFG_LOCALHOP_SECURITY
const uint64_t PROPAGATE_DEFAULT_COST
const std::string CFG_READVERTISE_NLSR
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents,...
boost::asio::io_service & getRibIoService()
boost::property_tree::ptree ConfigSection
a config file section
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.