blob: c24a5b0b4b9b152a04bbcd8165047043554da5b0 [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&
136StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
137{
138 return this->findEffectiveStrategy(pitEntry.getName());
139}
140
Junxiao Shi7bb01512014-03-05 21:34:09 -0700141Strategy&
142StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
143{
144 return this->findEffectiveStrategy(measurementsEntry.getName());
145}
146
Junxiao Shibb5105f2014-03-03 12:06:45 -0700147shared_ptr<fw::Strategy>
148StrategyChoice::getStrategy(const Name& strategyName)
149{
150 StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName);
151 return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>();
152}
153
154void
155StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy)
156{
157 this->install(strategy);
158
159 // don't use .insert here, because it will invoke findEffectiveStrategy
160 // which expects an existing root entry
161 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name());
162 shared_ptr<Entry> entry = make_shared<Entry>(Name());
163 nameTreeEntry->setStrategyChoiceEntry(entry);
164 ++m_nItems;
165 NFD_LOG_INFO("setDefaultStrategy(" << strategy->getName() << ") new entry");
166
167 entry->setStrategy(strategy);
168}
169
170void
171StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry,
172 shared_ptr<fw::Strategy> oldStrategy,
173 shared_ptr<fw::Strategy> newStrategy)
174{
175 entry->setStrategy(newStrategy);
176 if (oldStrategy == newStrategy) {
177 return;
178 }
179
180 // TODO delete incompatible StrategyInfo
181 NFD_LOG_WARN("changeStrategy(" << entry->getPrefix() << ") " <<
182 "runtime strategy change not implemented");
183}
184
185} // namespace nfd