blob: db4d498911944f94f604c58ffe70d420d27d9830 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "strategy-choice.hpp"
8#include "fw/strategy.hpp"
9#include "pit-entry.hpp"
Junxiao Shi7bb01512014-03-05 21:34:09 -070010#include "measurements-entry.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070011
12namespace nfd {
13
14using strategy_choice::Entry;
15using fw::Strategy;
16
17NFD_LOG_INIT("StrategyChoice");
18
19StrategyChoice::StrategyChoice(NameTree& nameTree, shared_ptr<Strategy> defaultStrategy)
20 : m_nameTree(nameTree)
21{
22 this->setDefaultStrategy(defaultStrategy);
23}
24
25bool
26StrategyChoice::hasStrategy(const Name& strategyName) const
27{
28 return m_strategyInstances.count(strategyName) > 0;
29}
30
31bool
32StrategyChoice::install(shared_ptr<Strategy> strategy)
33{
34 BOOST_ASSERT(static_cast<bool>(strategy));
35 const Name& strategyName = strategy->getName();
36
37 if (this->hasStrategy(strategyName)) {
38 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
39 return false;
40 }
41
42 m_strategyInstances[strategyName] = strategy;
43 return true;
44}
45
46bool
47StrategyChoice::insert(const Name& prefix, const Name& strategyName)
48{
49 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
50 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
51 shared_ptr<Strategy> oldStrategy;
52
53 if (static_cast<bool>(entry)) {
54 if (entry->getStrategy().getName() == strategyName) {
55 NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") not changing");
56 return true;
57 }
58 oldStrategy = entry->getStrategy().shared_from_this();
59 NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") "
60 "changing from " << oldStrategy->getName());
61 }
62
63 shared_ptr<Strategy> strategy = this->getStrategy(strategyName);
64 if (!static_cast<bool>(strategy)) {
65 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
66 return false;
67 }
68
69 if (!static_cast<bool>(entry)) {
70 oldStrategy = this->findEffectiveStrategy(prefix).shared_from_this();
71 entry = make_shared<Entry>(prefix);
72 nameTreeEntry->setStrategyChoiceEntry(entry);
73 ++m_nItems;
74 NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") new entry");
75 }
76
77 this->changeStrategy(entry, oldStrategy, strategy);
78 return true;
79}
80
81void
82StrategyChoice::erase(const Name& prefix)
83{
84 BOOST_ASSERT(prefix.size() > 0);
85
86 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
87 if (!static_cast<bool>(nameTreeEntry)) {
88 return;
89 }
90
91 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
92 if (!static_cast<bool>(entry)) {
93 return;
94 }
95
96 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -070097
98 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
99 this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this());
100
Junxiao Shibb5105f2014-03-03 12:06:45 -0700101 nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>());
102 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700103}
104
105shared_ptr<const Name>
106StrategyChoice::get(const Name& prefix) const
107{
108 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
109 if (!static_cast<bool>(nameTreeEntry)) {
110 return shared_ptr<const Name>();
111 }
112
113 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
114 if (!static_cast<bool>(entry)) {
115 return shared_ptr<const Name>();
116 }
117
118 return entry->getStrategy().getName().shared_from_this();
119}
120
121static inline bool
122predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry)
123{
124 return static_cast<bool>(entry.getStrategyChoiceEntry());
125}
126
127Strategy&
128StrategyChoice::findEffectiveStrategy(const Name& prefix) const
129{
130 shared_ptr<name_tree::Entry> nameTreeEntry =
131 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry);
132 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
133 return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
134}
135
136Strategy&
HangZhangcb4fc832014-03-11 16:57:11 +0800137StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const
138{
139 shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
140 if (static_cast<bool>(entry))
141 return entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700142 nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
HangZhangcb4fc832014-03-11 16:57:11 +0800143 &predicate_NameTreeEntry_hasStrategyChoiceEntry);
144 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
145 return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
146}
147
148Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700149StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
150{
HangZhangcb4fc832014-03-11 16:57:11 +0800151 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
152
153 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
154
155 return findEffectiveStrategy(nameTreeEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700156}
157
Junxiao Shi7bb01512014-03-05 21:34:09 -0700158Strategy&
159StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
160{
HangZhangcb4fc832014-03-11 16:57:11 +0800161 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry);
162
163 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
164
165 return findEffectiveStrategy(nameTreeEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700166}
167
Junxiao Shibb5105f2014-03-03 12:06:45 -0700168shared_ptr<fw::Strategy>
169StrategyChoice::getStrategy(const Name& strategyName)
170{
171 StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName);
172 return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>();
173}
174
175void
176StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy)
177{
178 this->install(strategy);
179
180 // don't use .insert here, because it will invoke findEffectiveStrategy
181 // which expects an existing root entry
182 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name());
183 shared_ptr<Entry> entry = make_shared<Entry>(Name());
184 nameTreeEntry->setStrategyChoiceEntry(entry);
185 ++m_nItems;
186 NFD_LOG_INFO("setDefaultStrategy(" << strategy->getName() << ") new entry");
187
188 entry->setStrategy(strategy);
189}
190
Junxiao Shie349ea12014-03-12 01:32:42 -0700191/** \brief a predicate that decides whether StrategyInfo should be reset
192 *
193 * StrategyInfo on a NameTree entry needs to be reset,
194 * if its effective strategy is covered by the changing StrategyChoice entry.
195 */
196static inline std::pair<bool,bool>
197predicate_nameTreeEntry_needResetStrategyChoice(const name_tree::Entry& nameTreeEntry,
198 const name_tree::Entry& rootEntry)
199{
200 if (&nameTreeEntry == &rootEntry) {
201 return std::make_pair(true, true);
202 }
203 if (static_cast<bool>(nameTreeEntry.getStrategyChoiceEntry())) {
204 return std::make_pair(false, false);
205 }
206 return std::make_pair(true, true);
207}
208
209static inline void
210clearStrategyInfo_pitFaceRecord(const pit::FaceRecord& pitFaceRecord)
211{
212 const_cast<pit::FaceRecord&>(pitFaceRecord).clearStrategyInfo();
213}
214
215static inline void
216clearStrategyInfo_pitEntry(shared_ptr<pit::Entry> pitEntry)
217{
218 pitEntry->clearStrategyInfo();
219 std::for_each(pitEntry->getInRecords().begin(), pitEntry->getInRecords().end(),
220 &clearStrategyInfo_pitFaceRecord);
221 std::for_each(pitEntry->getOutRecords().begin(), pitEntry->getOutRecords().end(),
222 &clearStrategyInfo_pitFaceRecord);
223}
224
225static inline void
226clearStrategyInfo(const name_tree::Entry& nameTreeEntry)
227{
228 NFD_LOG_TRACE("clearStrategyInfo " << nameTreeEntry.getPrefix());
229
230 std::for_each(nameTreeEntry.getPitEntries().begin(), nameTreeEntry.getPitEntries().end(),
231 &clearStrategyInfo_pitEntry);
232 if (static_cast<bool>(nameTreeEntry.getMeasurementsEntry())) {
233 nameTreeEntry.getMeasurementsEntry()->clearStrategyInfo();
234 }
235}
236
Junxiao Shibb5105f2014-03-03 12:06:45 -0700237void
238StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry,
239 shared_ptr<fw::Strategy> oldStrategy,
240 shared_ptr<fw::Strategy> newStrategy)
241{
242 entry->setStrategy(newStrategy);
243 if (oldStrategy == newStrategy) {
244 return;
245 }
246
Junxiao Shie349ea12014-03-12 01:32:42 -0700247 std::for_each(m_nameTree.partialEnumerate(entry->getPrefix(),
248 bind(&predicate_nameTreeEntry_needResetStrategyChoice,
249 _1, boost::cref(*m_nameTree.get(*entry)))),
250 m_nameTree.end(),
251 &clearStrategyInfo);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700252}
253
254} // namespace nfd