blob: 9ce2c9916b8c374a8662d48864c8fe33d9c38cbf [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();
97 nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>());
98 --m_nItems;
99
100 Strategy& parentStrategy = this->findEffectiveStrategy(prefix);
101 this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this());
102}
103
104shared_ptr<const Name>
105StrategyChoice::get(const Name& prefix) const
106{
107 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
108 if (!static_cast<bool>(nameTreeEntry)) {
109 return shared_ptr<const Name>();
110 }
111
112 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
113 if (!static_cast<bool>(entry)) {
114 return shared_ptr<const Name>();
115 }
116
117 return entry->getStrategy().getName().shared_from_this();
118}
119
120static inline bool
121predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry)
122{
123 return static_cast<bool>(entry.getStrategyChoiceEntry());
124}
125
126Strategy&
127StrategyChoice::findEffectiveStrategy(const Name& prefix) const
128{
129 shared_ptr<name_tree::Entry> nameTreeEntry =
130 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry);
131 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
132 return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
133}
134
135Strategy&
HangZhangcb4fc832014-03-11 16:57:11 +0800136StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const
137{
138 shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
139 if (static_cast<bool>(entry))
140 return entry->getStrategy();
141 nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
142 &predicate_NameTreeEntry_hasStrategyChoiceEntry);
143 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
144 return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
145}
146
147Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700148StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
149{
HangZhangcb4fc832014-03-11 16:57:11 +0800150 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
151
152 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
153
154 return findEffectiveStrategy(nameTreeEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700155}
156
Junxiao Shi7bb01512014-03-05 21:34:09 -0700157Strategy&
158StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
159{
HangZhangcb4fc832014-03-11 16:57:11 +0800160 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry);
161
162 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
163
164 return findEffectiveStrategy(nameTreeEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700165}
166
Junxiao Shibb5105f2014-03-03 12:06:45 -0700167shared_ptr<fw::Strategy>
168StrategyChoice::getStrategy(const Name& strategyName)
169{
170 StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName);
171 return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>();
172}
173
174void
175StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy)
176{
177 this->install(strategy);
178
179 // don't use .insert here, because it will invoke findEffectiveStrategy
180 // which expects an existing root entry
181 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name());
182 shared_ptr<Entry> entry = make_shared<Entry>(Name());
183 nameTreeEntry->setStrategyChoiceEntry(entry);
184 ++m_nItems;
185 NFD_LOG_INFO("setDefaultStrategy(" << strategy->getName() << ") new entry");
186
187 entry->setStrategy(strategy);
188}
189
190void
191StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry,
192 shared_ptr<fw::Strategy> oldStrategy,
193 shared_ptr<fw::Strategy> newStrategy)
194{
195 entry->setStrategy(newStrategy);
196 if (oldStrategy == newStrategy) {
197 return;
198 }
199
200 // TODO delete incompatible StrategyInfo
201 NFD_LOG_WARN("changeStrategy(" << entry->getPrefix() << ") " <<
202 "runtime strategy change not implemented");
203}
204
205} // namespace nfd