NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
tables-config-section.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 
28 #include "common.hpp"
29 #include "core/logger.hpp"
30 #include "core/config-file.hpp"
31 
32 namespace nfd {
33 
34 NFD_LOG_INIT("TablesConfigSection");
35 
36 const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
37 
39  Pit& pit,
40  Fib& fib,
41  StrategyChoice& strategyChoice,
42  Measurements& measurements,
43  NetworkRegionTable& networkRegionTable)
44  : m_cs(cs)
45  // , m_pit(pit)
46  // , m_fib(fib)
47  , m_strategyChoice(strategyChoice)
48  // , m_measurements(measurements)
49  , m_networkRegionTable(networkRegionTable)
50  , m_areTablesConfigured(false)
51 {
52 
53 }
54 
55 void
57 {
58  configFile.addSectionHandler("tables",
59  bind(&TablesConfigSection::processConfig, this, _1, _2, _3));
60 }
61 
62 
63 void
65 {
66  if (m_areTablesConfigured) {
67  return;
68  }
69 
70  NFD_LOG_INFO("Setting CS max packets to " << DEFAULT_CS_MAX_PACKETS);
71  m_cs.setLimit(DEFAULT_CS_MAX_PACKETS);
72 
73  m_areTablesConfigured = true;
74 }
75 
76 void
77 TablesConfigSection::processConfig(const ConfigSection& configSection,
78  bool isDryRun,
79  const std::string& filename)
80 {
81  // tables
82  // {
83  // cs_max_packets 65536
84  //
85  // strategy_choice
86  // {
87  // / /localhost/nfd/strategy/best-route
88  // /localhost /localhost/nfd/strategy/multicast
89  // /localhost/nfd /localhost/nfd/strategy/best-route
90  // /ndn/broadcast /localhost/nfd/strategy/multicast
91  // }
92  //
93  // network_region
94  // {
95  // /example/region1
96  // /example/region2
97  // }
98  // }
99 
100  size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
101 
102  boost::optional<const ConfigSection&> csMaxPacketsNode =
103  configSection.get_child_optional("cs_max_packets");
104 
105  if (csMaxPacketsNode) {
106  boost::optional<size_t> valCsMaxPackets =
107  configSection.get_optional<size_t>("cs_max_packets");
108 
109  if (!valCsMaxPackets) {
110  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"cs_max_packets\""
111  " in \"tables\" section"));
112  }
113 
114  nCsMaxPackets = *valCsMaxPackets;
115  }
116 
117  boost::optional<const ConfigSection&> strategyChoiceSection =
118  configSection.get_child_optional("strategy_choice");
119 
120  if (strategyChoiceSection) {
121  processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
122  }
123 
124  boost::optional<const ConfigSection&> networkRegionSection =
125  configSection.get_child_optional("network_region");
126 
127  if (networkRegionSection) {
128  processNetworkRegionSection(*networkRegionSection, isDryRun);
129  }
130 
131  if (!isDryRun) {
132  NFD_LOG_INFO("Setting CS max packets to " << nCsMaxPackets);
133 
134  m_cs.setLimit(nCsMaxPackets);
135  m_areTablesConfigured = true;
136  }
137 }
138 
139 void
140 TablesConfigSection::processStrategyChoiceSection(const ConfigSection& configSection,
141  bool isDryRun)
142 {
143  // strategy_choice
144  // {
145  // / /localhost/nfd/strategy/best-route
146  // /localhost /localhost/nfd/strategy/multicast
147  // /localhost/nfd /localhost/nfd/strategy/best-route
148  // /ndn/broadcast /localhost/nfd/strategy/multicast
149  // }
150 
151  std::map<Name, Name> choices;
152 
153  for (const auto& prefixAndStrategy : configSection) {
154  const Name prefix(prefixAndStrategy.first);
155  if (choices.find(prefix) != choices.end()) {
156  BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate strategy choice for prefix \"" +
157  prefix.toUri() + "\" in \"strategy_choice\" "
158  "section"));
159  }
160 
161  const std::string strategyString(prefixAndStrategy.second.get_value<std::string>());
162  if (strategyString.empty()) {
163  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"\" for prefix \"" +
164  prefix.toUri() + "\" in \"strategy_choice\" "
165  "section"));
166  }
167 
168  const Name strategyName(strategyString);
169  if (!m_strategyChoice.hasStrategy(strategyName)) {
170  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"" +
171  strategyName.toUri() + "\" for prefix \"" +
172  prefix.toUri() + "\" in \"strategy_choice\" "
173  "section"));
174  }
175 
176  choices[prefix] = strategyName;
177  }
178 
179 
180  for (const auto& prefixAndStrategy : choices) {
181  if (!isDryRun && !m_strategyChoice.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
182  BOOST_THROW_EXCEPTION(ConfigFile::Error("Failed to set strategy \"" +
183  prefixAndStrategy.second.toUri() + "\" for "
184  "prefix \"" + prefixAndStrategy.first.toUri() +
185  "\" in \"strategy_choicev\""));
186  }
187  }
188 }
189 
190 void
191 TablesConfigSection::processNetworkRegionSection(const ConfigSection& configSection,
192  bool isDryRun)
193 {
194  // network_region
195  // {
196  // /example/region1
197  // /example/region2
198  // }
199 
200  if (!isDryRun) {
201  m_networkRegionTable.clear();
202  }
203 
204  for (const auto& pair : configSection) {
205  const Name region(pair.first);
206 
207  if (!isDryRun) {
208  m_networkRegionTable.insert(region);
209  }
210  }
211 }
212 
213 
214 } // namespace nfd
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:183
represents the Strategy Choice table
bool hasStrategy(const Name &strategyName, bool isExact=false) const
determines if a strategy is installed
bool insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
represents the FIB
Definition: fib.hpp:44
stores a collection of producer region names
represents the Measurements table
represents the Interest Table
Definition: pit.hpp:48
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:79
void setLimit(size_t nMaxPackets)
changes capacity (in number of packets)
Definition: cs.cpp:60
boost::property_tree::ptree ConfigSection
Name abstraction to represent an absolute name.
Definition: name.hpp:46
represents the ContentStore
Definition: cs.hpp:65
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
void setConfigFile(ConfigFile &configFile)
TablesConfigSection(Cs &cs, Pit &pit, Fib &fib, StrategyChoice &strategyChoice, Measurements &measurements, NetworkRegionTable &networkRegionTable)