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  : m_cs(cs)
44  // , m_pit(pit)
45  // , m_fib(fib)
46  , m_strategyChoice(strategyChoice)
47  // , m_measurements(measurements)
48  , m_areTablesConfigured(false)
49 {
50 
51 }
52 
53 void
55 {
56  configFile.addSectionHandler("tables",
57  bind(&TablesConfigSection::onConfig, this, _1, _2, _3));
58 }
59 
60 
61 void
63 {
64  if (m_areTablesConfigured)
65  {
66  return;
67  }
68 
69  NFD_LOG_INFO("Setting CS max packets to " << DEFAULT_CS_MAX_PACKETS);
70  m_cs.setLimit(DEFAULT_CS_MAX_PACKETS);
71 
72  m_areTablesConfigured = true;
73 }
74 
75 void
76 TablesConfigSection::onConfig(const ConfigSection& configSection,
77  bool isDryRun,
78  const std::string& filename)
79 {
80  // tables
81  // {
82  // cs_max_packets 65536
83  //
84  // strategy_choice
85  // {
86  // / /localhost/nfd/strategy/best-route
87  // /localhost /localhost/nfd/strategy/multicast
88  // /localhost/nfd /localhost/nfd/strategy/best-route
89  // /ndn/broadcast /localhost/nfd/strategy/multicast
90  // }
91  // }
92 
93  size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
94 
95  boost::optional<const ConfigSection&> csMaxPacketsNode =
96  configSection.get_child_optional("cs_max_packets");
97 
98  if (csMaxPacketsNode)
99  {
100  boost::optional<size_t> valCsMaxPackets =
101  configSection.get_optional<size_t>("cs_max_packets");
102 
103  if (!valCsMaxPackets)
104  {
105  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"cs_max_packets\""
106  " in \"tables\" section"));
107  }
108 
109  nCsMaxPackets = *valCsMaxPackets;
110  }
111 
112  boost::optional<const ConfigSection&> strategyChoiceSection =
113  configSection.get_child_optional("strategy_choice");
114 
115  if (strategyChoiceSection)
116  {
117  processSectionStrategyChoice(*strategyChoiceSection, isDryRun);
118  }
119 
120  if (!isDryRun)
121  {
122  NFD_LOG_INFO("Setting CS max packets to " << nCsMaxPackets);
123 
124  m_cs.setLimit(nCsMaxPackets);
125  m_areTablesConfigured = true;
126  }
127 }
128 
129 void
130 TablesConfigSection::processSectionStrategyChoice(const ConfigSection& configSection,
131  bool isDryRun)
132 {
133  // strategy_choice
134  // {
135  // / /localhost/nfd/strategy/best-route
136  // /localhost /localhost/nfd/strategy/multicast
137  // /localhost/nfd /localhost/nfd/strategy/best-route
138  // /ndn/broadcast /localhost/nfd/strategy/multicast
139  // }
140 
141  std::map<Name, Name> choices;
142 
143  for (const auto& prefixAndStrategy : configSection)
144  {
145  const Name prefix(prefixAndStrategy.first);
146  if (choices.find(prefix) != choices.end())
147  {
148  BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate strategy choice for prefix \"" +
149  prefix.toUri() + "\" in \"strategy_choice\" "
150  "section"));
151  }
152 
153  const std::string strategyString(prefixAndStrategy.second.get_value<std::string>());
154  if (strategyString.empty())
155  {
156  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"\" for prefix \"" +
157  prefix.toUri() + "\" in \"strategy_choice\" "
158  "section"));
159  }
160 
161  const Name strategyName(strategyString);
162  if (!m_strategyChoice.hasStrategy(strategyName))
163  {
164  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"" +
165  strategyName.toUri() + "\" for prefix \"" +
166  prefix.toUri() + "\" in \"strategy_choice\" "
167  "section"));
168  }
169 
170  choices[prefix] = strategyName;
171  }
172 
173 
174  for (const auto& prefixAndStrategy : choices)
175  {
176  if (!isDryRun && !m_strategyChoice.insert(prefixAndStrategy.first, prefixAndStrategy.second))
177  {
178  BOOST_THROW_EXCEPTION(ConfigFile::Error("Failed to set strategy \"" +
179  prefixAndStrategy.second.toUri() + "\" for "
180  "prefix \"" + prefixAndStrategy.first.toUri() +
181  "\" in \"strategy_choicev\""));
182  }
183  }
184 }
185 
186 
187 
188 } // namespace nfd
TablesConfigSection(Cs &cs, Pit &pit, Fib &fib, StrategyChoice &strategyChoice, Measurements &measurements)
represents the Strategy Choice table
bool insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
represents the FIB
Definition: fib.hpp:44
represents the Measurements table
represents the Interest Table
Definition: pit.hpp:48
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:183
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:37
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:61
boost::property_tree::ptree ConfigSection
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool hasStrategy(const Name &strategyName, bool isExact=false) const
determines if a strategy is installed
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
void setConfigFile(ConfigFile &configFile)