tables-config-section.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 #include "fw/strategy.hpp"
28 
29 namespace nfd {
30 
31 const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
32 
34  : m_forwarder(forwarder)
35  , m_isConfigured(false)
36 {
37 }
38 
39 void
41 {
42  configFile.addSectionHandler("tables",
43  bind(&TablesConfigSection::processConfig, this, _1, _2));
44 }
45 
46 void
48 {
49  if (m_isConfigured) {
50  return;
51  }
52 
53  m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
54  // Don't set default cs_policy because it's already created by CS itself.
55  m_forwarder.setUnsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>());
56 
57  m_isConfigured = true;
58 }
59 
60 void
61 TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun)
62 {
63  size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
64  OptionalConfigSection csMaxPacketsNode = section.get_child_optional("cs_max_packets");
65  if (csMaxPacketsNode) {
66  nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
67  }
68 
69  unique_ptr<cs::Policy> csPolicy;
70  OptionalConfigSection csPolicyNode = section.get_child_optional("cs_policy");
71  if (csPolicyNode) {
72  std::string policyName = csPolicyNode->get_value<std::string>();
73  csPolicy = cs::Policy::create(policyName);
74  if (csPolicy == nullptr) {
75  BOOST_THROW_EXCEPTION(ConfigFile::Error(
76  "Unknown cs_policy \"" + policyName + "\" in \"tables\" section"));
77  }
78  }
79 
80  unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy;
81  OptionalConfigSection unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy");
82  if (unsolicitedDataPolicyNode) {
83  std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>();
84  unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName);
85  if (unsolicitedDataPolicy == nullptr) {
86  BOOST_THROW_EXCEPTION(ConfigFile::Error(
87  "Unknown cs_unsolicited_policy \"" + policyName + "\" in \"tables\" section"));
88  }
89  }
90  else {
91  unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>();
92  }
93 
94  OptionalConfigSection strategyChoiceSection = section.get_child_optional("strategy_choice");
95  if (strategyChoiceSection) {
96  processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
97  }
98 
99  OptionalConfigSection networkRegionSection = section.get_child_optional("network_region");
100  if (networkRegionSection) {
101  processNetworkRegionSection(*networkRegionSection, isDryRun);
102  }
103 
104  if (isDryRun) {
105  return;
106  }
107 
108  Cs& cs = m_forwarder.getCs();
109  cs.setLimit(nCsMaxPackets);
110  if (cs.size() == 0 && csPolicy != nullptr) {
111  cs.setPolicy(std::move(csPolicy));
112  }
113 
114  m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy));
115 
116  m_isConfigured = true;
117 }
118 
119 void
120 TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
121 {
122  using fw::Strategy;
123 
124  std::map<Name, Name> choices;
125  for (const auto& prefixAndStrategy : section) {
126  Name prefix(prefixAndStrategy.first);
127  Name strategy(prefixAndStrategy.second.get_value<std::string>());
128 
129  if (!Strategy::canCreate(strategy)) {
130  BOOST_THROW_EXCEPTION(ConfigFile::Error(
131  "Unknown strategy \"" + prefixAndStrategy.second.get_value<std::string>() +
132  "\" for prefix \"" + prefix.toUri() + "\" in \"strategy_choice\" section"));
133  }
134 
135  if (!choices.emplace(prefix, strategy).second) {
136  BOOST_THROW_EXCEPTION(ConfigFile::Error(
137  "Duplicate strategy choice for prefix \"" + prefix.toUri() +
138  "\" in \"strategy_choice\" section"));
139  }
140  }
141 
142  if (isDryRun) {
143  return;
144  }
145 
146  StrategyChoice& sc = m_forwarder.getStrategyChoice();
147  for (const auto& prefixAndStrategy : choices) {
148  if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
149  BOOST_THROW_EXCEPTION(ConfigFile::Error(
150  "Failed to set strategy \"" + prefixAndStrategy.second.toUri() + "\" for "
151  "prefix \"" + prefixAndStrategy.first.toUri() + "\" in \"strategy_choicev\""));
152  }
153  }
155 }
156 
157 void
158 TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
159 {
160  if (isDryRun) {
161  return;
162  }
163 
164  NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
165  nrt.clear();
166  for (const auto& pair : section) {
167  Name region(pair.first);
168  nrt.insert(region);
169  }
170 }
171 
172 } // namespace nfd
main class of NFD
Definition: forwarder.hpp:52
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
configuration file parsing utility
Definition: config-file.hpp:58
StrategyChoice & getStrategyChoice()
Definition: forwarder.hpp:168
static unique_ptr< UnsolicitedDataPolicy > create(const std::string &policyName)
static unique_ptr< Policy > create(const std::string &policyName)
Definition: cs-policy.cpp:45
TablesConfigSection(Forwarder &forwarder)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void ensureConfigured()
apply default configuration, if tables section was omitted in configuration file
stores a collection of producer region names
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
void setConfigFile(ConfigFile &configFile)
void setUnsolicitedDataPolicy(unique_ptr< fw::UnsolicitedDataPolicy > policy)
Definition: forwarder.hpp:100
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
represents a forwarding strategy
Definition: strategy.hpp:37
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:180