blob: 11bf120c45974103969c9d43c26c687359a28063 [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06008#include "core/logger.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -07009#include "fw/strategy.hpp"
10#include "pit-entry.hpp"
Junxiao Shi7bb01512014-03-05 21:34:09 -070011#include "measurements-entry.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070012
13namespace nfd {
14
15using strategy_choice::Entry;
16using fw::Strategy;
17
18NFD_LOG_INIT("StrategyChoice");
19
20StrategyChoice::StrategyChoice(NameTree& nameTree, shared_ptr<Strategy> defaultStrategy)
21 : m_nameTree(nameTree)
22{
23 this->setDefaultStrategy(defaultStrategy);
24}
25
26bool
27StrategyChoice::hasStrategy(const Name& strategyName) const
28{
29 return m_strategyInstances.count(strategyName) > 0;
30}
31
32bool
33StrategyChoice::install(shared_ptr<Strategy> strategy)
34{
35 BOOST_ASSERT(static_cast<bool>(strategy));
36 const Name& strategyName = strategy->getName();
37
38 if (this->hasStrategy(strategyName)) {
39 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
40 return false;
41 }
42
43 m_strategyInstances[strategyName] = strategy;
44 return true;
45}
46
47bool
48StrategyChoice::insert(const Name& prefix, const Name& strategyName)
49{
50 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
51 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
52 shared_ptr<Strategy> oldStrategy;
53
54 if (static_cast<bool>(entry)) {
55 if (entry->getStrategy().getName() == strategyName) {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070056 NFD_LOG_TRACE("insert(" << prefix << "," << strategyName << ") not changing");
Junxiao Shibb5105f2014-03-03 12:06:45 -070057 return true;
58 }
59 oldStrategy = entry->getStrategy().shared_from_this();
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070060 NFD_LOG_TRACE("insert(" << prefix << "," << strategyName << ") "
61 "changing from " << oldStrategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -070062 }
63
64 shared_ptr<Strategy> strategy = this->getStrategy(strategyName);
65 if (!static_cast<bool>(strategy)) {
66 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
67 return false;
68 }
69
70 if (!static_cast<bool>(entry)) {
71 oldStrategy = this->findEffectiveStrategy(prefix).shared_from_this();
72 entry = make_shared<Entry>(prefix);
73 nameTreeEntry->setStrategyChoiceEntry(entry);
74 ++m_nItems;
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070075 NFD_LOG_TRACE("insert(" << prefix << "," << strategyName << ") new entry");
Junxiao Shibb5105f2014-03-03 12:06:45 -070076 }
77
78 this->changeStrategy(entry, oldStrategy, strategy);
79 return true;
80}
81
82void
83StrategyChoice::erase(const Name& prefix)
84{
85 BOOST_ASSERT(prefix.size() > 0);
86
87 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
88 if (!static_cast<bool>(nameTreeEntry)) {
89 return;
90 }
91
92 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
93 if (!static_cast<bool>(entry)) {
94 return;
95 }
96
97 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -070098
99 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
100 this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this());
101
Junxiao Shibb5105f2014-03-03 12:06:45 -0700102 nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>());
103 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700104}
105
106shared_ptr<const Name>
107StrategyChoice::get(const Name& prefix) const
108{
109 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
110 if (!static_cast<bool>(nameTreeEntry)) {
111 return shared_ptr<const Name>();
112 }
113
114 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
115 if (!static_cast<bool>(entry)) {
116 return shared_ptr<const Name>();
117 }
118
119 return entry->getStrategy().getName().shared_from_this();
120}
121
122static inline bool
123predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry)
124{
125 return static_cast<bool>(entry.getStrategyChoiceEntry());
126}
127
128Strategy&
129StrategyChoice::findEffectiveStrategy(const Name& prefix) const
130{
131 shared_ptr<name_tree::Entry> nameTreeEntry =
132 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry);
133 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
134 return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
135}
136
137Strategy&
HangZhangcb4fc832014-03-11 16:57:11 +0800138StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const
139{
140 shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
141 if (static_cast<bool>(entry))
142 return entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700143 nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
HangZhangcb4fc832014-03-11 16:57:11 +0800144 &predicate_NameTreeEntry_hasStrategyChoiceEntry);
145 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
146 return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
147}
148
149Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700150StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
151{
HangZhangcb4fc832014-03-11 16:57:11 +0800152 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
153
154 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
155
156 return findEffectiveStrategy(nameTreeEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700157}
158
Junxiao Shi7bb01512014-03-05 21:34:09 -0700159Strategy&
160StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
161{
HangZhangcb4fc832014-03-11 16:57:11 +0800162 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry);
163
164 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
165
166 return findEffectiveStrategy(nameTreeEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700167}
168
Junxiao Shibb5105f2014-03-03 12:06:45 -0700169shared_ptr<fw::Strategy>
170StrategyChoice::getStrategy(const Name& strategyName)
171{
172 StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName);
173 return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>();
174}
175
176void
177StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy)
178{
179 this->install(strategy);
180
181 // don't use .insert here, because it will invoke findEffectiveStrategy
182 // which expects an existing root entry
183 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name());
184 shared_ptr<Entry> entry = make_shared<Entry>(Name());
185 nameTreeEntry->setStrategyChoiceEntry(entry);
186 ++m_nItems;
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700187 NFD_LOG_INFO("Set default strategy " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700188
189 entry->setStrategy(strategy);
190}
191
Junxiao Shie349ea12014-03-12 01:32:42 -0700192/** \brief a predicate that decides whether StrategyInfo should be reset
193 *
194 * StrategyInfo on a NameTree entry needs to be reset,
195 * if its effective strategy is covered by the changing StrategyChoice entry.
196 */
197static inline std::pair<bool,bool>
198predicate_nameTreeEntry_needResetStrategyChoice(const name_tree::Entry& nameTreeEntry,
199 const name_tree::Entry& rootEntry)
200{
201 if (&nameTreeEntry == &rootEntry) {
202 return std::make_pair(true, true);
203 }
204 if (static_cast<bool>(nameTreeEntry.getStrategyChoiceEntry())) {
205 return std::make_pair(false, false);
206 }
207 return std::make_pair(true, true);
208}
209
210static inline void
211clearStrategyInfo_pitFaceRecord(const pit::FaceRecord& pitFaceRecord)
212{
213 const_cast<pit::FaceRecord&>(pitFaceRecord).clearStrategyInfo();
214}
215
216static inline void
217clearStrategyInfo_pitEntry(shared_ptr<pit::Entry> pitEntry)
218{
219 pitEntry->clearStrategyInfo();
220 std::for_each(pitEntry->getInRecords().begin(), pitEntry->getInRecords().end(),
221 &clearStrategyInfo_pitFaceRecord);
222 std::for_each(pitEntry->getOutRecords().begin(), pitEntry->getOutRecords().end(),
223 &clearStrategyInfo_pitFaceRecord);
224}
225
226static inline void
227clearStrategyInfo(const name_tree::Entry& nameTreeEntry)
228{
229 NFD_LOG_TRACE("clearStrategyInfo " << nameTreeEntry.getPrefix());
230
231 std::for_each(nameTreeEntry.getPitEntries().begin(), nameTreeEntry.getPitEntries().end(),
232 &clearStrategyInfo_pitEntry);
233 if (static_cast<bool>(nameTreeEntry.getMeasurementsEntry())) {
234 nameTreeEntry.getMeasurementsEntry()->clearStrategyInfo();
235 }
236}
237
Junxiao Shibb5105f2014-03-03 12:06:45 -0700238void
239StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry,
240 shared_ptr<fw::Strategy> oldStrategy,
241 shared_ptr<fw::Strategy> newStrategy)
242{
243 entry->setStrategy(newStrategy);
244 if (oldStrategy == newStrategy) {
245 return;
246 }
247
Junxiao Shie349ea12014-03-12 01:32:42 -0700248 std::for_each(m_nameTree.partialEnumerate(entry->getPrefix(),
249 bind(&predicate_nameTreeEntry_needResetStrategyChoice,
250 _1, boost::cref(*m_nameTree.get(*entry)))),
251 m_nameTree.end(),
252 &clearStrategyInfo);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700253}
254
255} // namespace nfd