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 NDN_THROW(
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_nfdController(m_face, m_keyChain)
111 , m_fibUpdater(m_rib, m_nfdController)
112 , m_dispatcher(m_face, m_keyChain)
113 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
115 if (s_instance !=
nullptr) {
116 NDN_THROW(std::logic_error(
"RIB service cannot be instantiated more than once"));
119 NDN_THROW(std::logic_error(
"RIB service must run on RIB thread"));
124 config.
addSectionHandler(CFG_SECTION, bind(&Service::processConfig,
this, _1, _2, _3));
125 configParse(config,
true);
126 configParse(config,
false);
134 s_instance =
nullptr;
140 if (s_instance ==
nullptr) {
141 NDN_THROW(std::logic_error(
"RIB service is not instantiated"));
144 NDN_THROW(std::logic_error(
"Must get RIB service on RIB thread"));
150 Service::processConfig(
const ConfigSection& section,
bool isDryRun,
const std::string& filename)
153 checkConfig(section, filename);
156 applyConfig(section, filename);
161 Service::checkConfig(
const ConfigSection& section,
const std::string& filename)
163 for (
const auto& item : section) {
164 const std::string& key = item.first;
166 if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
167 ndn::ValidatorConfig testValidator(m_face);
168 testValidator.load(value, filename);
170 else if (key == CFG_PREFIX_PROPAGATE) {
173 else if (key == CFG_READVERTISE_NLSR) {
183 Service::applyConfig(
const ConfigSection& section,
const std::string& filename)
185 bool wantPrefixPropagate =
false;
186 bool wantReadvertiseNlsr =
false;
188 for (
const auto& item : section) {
189 const std::string& key = item.first;
191 if (key == CFG_LOCALHOST_SECURITY) {
194 else if (key == CFG_LOCALHOP_SECURITY) {
197 else if (key == CFG_PREFIX_PROPAGATE) {
198 wantPrefixPropagate =
true;
200 if (!m_readvertisePropagation) {
203 auto parameters = ndn::nfd::ControlParameters()
204 .setCost(PROPAGATE_DEFAULT_COST)
205 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
206 auto cost = item.second.get_optional<uint64_t>(
"cost");
208 parameters.setCost(*cost);
211 auto options = ndn::nfd::CommandOptions()
213 .setTimeout(PROPAGATE_DEFAULT_TIMEOUT);
214 auto timeout = item.second.get_optional<uint64_t>(
"timeout");
216 options.setTimeout(time::milliseconds(*timeout));
219 m_readvertisePropagation = make_unique<Readvertise>(
221 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
222 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
225 else if (key == CFG_READVERTISE_NLSR) {
233 if (!wantPrefixPropagate && m_readvertisePropagation !=
nullptr) {
235 m_readvertisePropagation.reset();
238 if (wantReadvertiseNlsr && m_readvertiseNlsr ==
nullptr) {
240 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
241 m_readvertiseNlsr = make_unique<Readvertise>(
243 make_unique<ClientToNlsrReadvertisePolicy>(),
244 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
246 else if (!wantReadvertiseNlsr && m_readvertiseNlsr !=
nullptr) {
248 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 ConfigSection loadConfigSectionFromFile(const std::string &filename)
static const std::string CFG_LOCALHOST_SECURITY
void enableLocalhop(const ConfigSection §ion, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix...
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 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...
boost::asio::io_service & getRibIoService()
static const uint64_t PROPAGATE_DEFAULT_COST
void registerWithNfd()
Start accepting commands and dataset requests.
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 Name LOCALHOP_TOP_PREFIX
static const std::string CFG_LOCALHOP_SECURITY
#define NFD_LOG_INIT(name)
static const std::string CFG_PREFIX_PROPAGATE
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
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()
Returns the global io_service instance for the calling thread.