tables-config-section.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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 
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  NDN_THROW(ConfigFile::Error("Unknown cs_policy '" + policyName + "' in section 'tables'"));
76  }
77  }
78 
79  unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy;
80  OptionalConfigSection unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy");
81  if (unsolicitedDataPolicyNode) {
82  std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>();
83  unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName);
84  if (unsolicitedDataPolicy == nullptr) {
85  NDN_THROW(ConfigFile::Error("Unknown cs_unsolicited_policy '" + policyName + "' in section 'tables'"));
86  }
87  }
88  else {
89  unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>();
90  }
91 
92  OptionalConfigSection strategyChoiceSection = section.get_child_optional("strategy_choice");
93  if (strategyChoiceSection) {
94  processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
95  }
96 
97  OptionalConfigSection networkRegionSection = section.get_child_optional("network_region");
98  if (networkRegionSection) {
99  processNetworkRegionSection(*networkRegionSection, isDryRun);
100  }
101 
102  if (isDryRun) {
103  return;
104  }
105 
106  Cs& cs = m_forwarder.getCs();
107  cs.setLimit(nCsMaxPackets);
108  if (cs.size() == 0 && csPolicy != nullptr) {
109  cs.setPolicy(std::move(csPolicy));
110  }
111 
112  m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy));
113 
114  m_isConfigured = true;
115 }
116 
117 void
118 TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
119 {
120  using fw::Strategy;
121 
122  std::map<Name, Name> choices;
123  for (const auto& prefixAndStrategy : section) {
124  Name prefix(prefixAndStrategy.first);
125  Name strategy(prefixAndStrategy.second.get_value<std::string>());
126 
127  if (!Strategy::canCreate(strategy)) {
128  NDN_THROW(ConfigFile::Error(
129  "Unknown strategy '" + prefixAndStrategy.second.get_value<std::string>() +
130  "' for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
131  }
132 
133  if (!choices.emplace(prefix, strategy).second) {
134  NDN_THROW(ConfigFile::Error(
135  "Duplicate strategy choice for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
136  }
137  }
138 
139  if (isDryRun) {
140  return;
141  }
142 
143  StrategyChoice& sc = m_forwarder.getStrategyChoice();
144  for (const auto& prefixAndStrategy : choices) {
145  if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
146  NDN_THROW(ConfigFile::Error(
147  "Failed to set strategy '" + prefixAndStrategy.second.toUri() + "' for prefix '" +
148  prefixAndStrategy.first.toUri() + "' in section 'strategy_choice'"));
149  }
150  }
152 }
153 
154 void
155 TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
156 {
157  if (isDryRun) {
158  return;
159  }
160 
161  auto& nrt = m_forwarder.getNetworkRegionTable();
162  nrt.clear();
163  for (const auto& pair : section) {
164  nrt.insert(Name(pair.first));
165  }
166 }
167 
168 } // namespace nfd
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:51
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
configuration file parsing utility
Definition: config-file.hpp:57
StrategyChoice & getStrategyChoice()
Definition: forwarder.hpp:151
static unique_ptr< UnsolicitedDataPolicy > create(const std::string &policyName)
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
static unique_ptr< Policy > create(const std::string &policyName)
Definition: cs-policy.cpp:46
TablesConfigSection(Forwarder &forwarder)
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
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
void setConfigFile(ConfigFile &configFile)
void setUnsolicitedDataPolicy(unique_ptr< fw::UnsolicitedDataPolicy > policy)
Definition: forwarder.hpp:73
represents a forwarding strategy
Definition: strategy.hpp:37
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:163