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;
62 boost::property_tree::read_info(filename, config);
70 static shared_ptr<ndn::Transport>
73 if (config.get_child_optional(
"face_system.unix")) {
75 auto path = config.get<std::string>(
"face_system.unix.path",
"/var/run/nfd.sock");
76 return make_shared<ndn::UnixTransport>(path);
78 else if (config.get_child_optional(
"face_system.tcp") &&
79 config.get<std::string>(
"face_system.tcp.listen",
"yes") ==
"yes") {
81 auto port = config.get<std::string>(
"face_system.tcp.port",
"6363");
82 return make_shared<ndn::TcpTransport>(
"localhost", port);
85 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"No transport is available to communicate with NFD"));
91 [&configFile] (
ConfigFile& config, bool isDryRun) {
92 config.parse(configFile, isDryRun);
99 [&configSection] (
ConfigFile& config, bool isDryRun) {
100 config.parse(configSection, isDryRun,
"internal://nfd.conf");
105 template<
typename ConfigParseFunc>
106 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
107 const ConfigParseFunc& configParse)
108 : m_keyChain(keyChain)
110 , m_scheduler(m_face.getIoService())
111 , m_nfdController(m_face, m_keyChain)
112 , m_fibUpdater(m_rib, m_nfdController)
113 , m_dispatcher(m_face, m_keyChain)
114 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
116 if (s_instance !=
nullptr) {
117 BOOST_THROW_EXCEPTION(std::logic_error(
"RIB service cannot be instantiated more than once"));
120 BOOST_THROW_EXCEPTION(std::logic_error(
"RIB service must run on RIB thread"));
125 config.
addSectionHandler(CFG_SECTION, bind(&Service::processConfig,
this, _1, _2, _3));
126 configParse(config,
true);
127 configParse(config,
false);
135 s_instance =
nullptr;
141 if (s_instance ==
nullptr) {
142 BOOST_THROW_EXCEPTION(std::logic_error(
"RIB service is not instantiated"));
145 BOOST_THROW_EXCEPTION(std::logic_error(
"Must get RIB service on RIB thread"));
151 Service::processConfig(
const ConfigSection& section,
bool isDryRun,
const std::string& filename)
154 checkConfig(section, filename);
157 applyConfig(section, filename);
162 Service::checkConfig(
const ConfigSection& section,
const std::string& filename)
164 for (
const auto& item : section) {
165 const std::string& key = item.first;
167 if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
168 ndn::ValidatorConfig testValidator(m_face);
169 testValidator.load(value, filename);
171 else if (key == CFG_PREFIX_PROPAGATE) {
174 else if (key == CFG_READVERTISE_NLSR) {
178 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option " + CFG_SECTION +
"." + key));
184 Service::applyConfig(
const ConfigSection& section,
const std::string& filename)
186 bool wantPrefixPropagate =
false;
187 bool wantReadvertiseNlsr =
false;
189 for (
const auto& item : section) {
190 const std::string& key = item.first;
192 if (key == CFG_LOCALHOST_SECURITY) {
195 else if (key == CFG_LOCALHOP_SECURITY) {
198 else if (key == CFG_PREFIX_PROPAGATE) {
199 wantPrefixPropagate =
true;
201 if (!m_readvertisePropagation) {
204 auto parameters = ndn::nfd::ControlParameters()
205 .setCost(PROPAGATE_DEFAULT_COST)
206 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
207 auto cost = item.second.get_optional<uint64_t>(
"cost");
209 parameters.setCost(*cost);
212 auto options = ndn::nfd::CommandOptions()
214 .setTimeout(PROPAGATE_DEFAULT_TIMEOUT);
215 auto timeout = item.second.get_optional<uint64_t>(
"timeout");
217 options.setTimeout(time::milliseconds(*timeout));
220 m_readvertisePropagation = make_unique<Readvertise>(
223 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
224 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
227 else if (key == CFG_READVERTISE_NLSR) {
231 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unrecognized option " + CFG_SECTION +
"." + key));
235 if (!wantPrefixPropagate && m_readvertisePropagation !=
nullptr) {
237 m_readvertisePropagation.reset();
240 if (wantReadvertiseNlsr && m_readvertiseNlsr ==
nullptr) {
242 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
243 m_readvertiseNlsr = make_unique<Readvertise>(
246 make_unique<ClientToNlsrReadvertisePolicy>(),
247 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
249 else if (!wantReadvertiseNlsr && m_readvertiseNlsr !=
nullptr) {
251 m_readvertiseNlsr.reset();
static const Name READVERTISE_NLSR_PREFIX
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
static Service & get()
Get a reference to the only instance of this class.
static const std::string CFG_SECTION
static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
configuration file parsing utility
static const Name LOCALHOP_TOP_PREFIX
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
static const std::string CFG_LOCALHOST_SECURITY
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"
static const std::string CFG_READVERTISE_NLSR
void enableLocalhop(const ConfigSection §ion, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix...
void registerWithNfd()
Start accepting commands and dataset requests.
void applyLocalhostConfig(const ConfigSection §ion, const std::string &filename)
Apply localhost_security configuration.
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...
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
boost::asio::io_service & getRibIoService()
static const uint64_t PROPAGATE_DEFAULT_COST
Service(const std::string &configFile, ndn::KeyChain &keyChain)
create NFD-RIB service
boost::property_tree::ptree ConfigSection
a config file section
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
static const std::string CFG_LOCALHOP_SECURITY
#define NFD_LOG_INIT(name)
static const std::string CFG_PREFIX_PROPAGATE
initializes and executes NFD-RIB service thread
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
boost::asio::io_service & getGlobalIoService()