blob: 9e5cb374404f7c8a54e6546fa85ca8b979a22071 [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"
10
11namespace nfd {
12
13using strategy_choice::Entry;
14using fw::Strategy;
15
16NFD_LOG_INIT("StrategyChoice");
17
18StrategyChoice::StrategyChoice(NameTree& nameTree, shared_ptr<Strategy> defaultStrategy)
19 : m_nameTree(nameTree)
20{
21 this->setDefaultStrategy(defaultStrategy);
22}
23
24bool
25StrategyChoice::hasStrategy(const Name& strategyName) const
26{
27 return m_strategyInstances.count(strategyName) > 0;
28}
29
30bool
31StrategyChoice::install(shared_ptr<Strategy> strategy)
32{
33 BOOST_ASSERT(static_cast<bool>(strategy));
34 const Name& strategyName = strategy->getName();
35
36 if (this->hasStrategy(strategyName)) {
37 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
38 return false;
39 }
40
41 m_strategyInstances[strategyName] = strategy;
42 return true;
43}
44
45bool
46StrategyChoice::insert(const Name& prefix, const Name& strategyName)
47{
48 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
49 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
50 shared_ptr<Strategy> oldStrategy;
51
52 if (static_cast<bool>(entry)) {
53 if (entry->getStrategy().getName() == strategyName) {
54 NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") not changing");
55 return true;
56 }
57 oldStrategy = entry->getStrategy().shared_from_this();
58 NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") "
59 "changing from " << oldStrategy->getName());
60 }
61
62 shared_ptr<Strategy> strategy = this->getStrategy(strategyName);
63 if (!static_cast<bool>(strategy)) {
64 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
65 return false;
66 }
67
68 if (!static_cast<bool>(entry)) {
69 oldStrategy = this->findEffectiveStrategy(prefix).shared_from_this();
70 entry = make_shared<Entry>(prefix);
71 nameTreeEntry->setStrategyChoiceEntry(entry);
72 ++m_nItems;
73 NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") new entry");
74 }
75
76 this->changeStrategy(entry, oldStrategy, strategy);
77 return true;
78}
79
80void
81StrategyChoice::erase(const Name& prefix)
82{
83 BOOST_ASSERT(prefix.size() > 0);
84
85 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
86 if (!static_cast<bool>(nameTreeEntry)) {
87 return;
88 }
89
90 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
91 if (!static_cast<bool>(entry)) {
92 return;
93 }
94
95 Strategy& oldStrategy = entry->getStrategy();
96 nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>());
97 --m_nItems;
98
99 Strategy& parentStrategy = this->findEffectiveStrategy(prefix);
100 this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this());
101}
102
103shared_ptr<const Name>
104StrategyChoice::get(const Name& prefix) const
105{
106 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
107 if (!static_cast<bool>(nameTreeEntry)) {
108 return shared_ptr<const Name>();
109 }
110
111 shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
112 if (!static_cast<bool>(entry)) {
113 return shared_ptr<const Name>();
114 }
115
116 return entry->getStrategy().getName().shared_from_this();
117}
118
119static inline bool
120predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry)
121{
122 return static_cast<bool>(entry.getStrategyChoiceEntry());
123}
124
125Strategy&
126StrategyChoice::findEffectiveStrategy(const Name& prefix) const
127{
128 shared_ptr<name_tree::Entry> nameTreeEntry =
129 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry);
130 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
131 return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
132}
133
134Strategy&
135StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
136{
137 return this->findEffectiveStrategy(pitEntry.getName());
138}
139
140shared_ptr<fw::Strategy>
141StrategyChoice::getStrategy(const Name& strategyName)
142{
143 StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName);
144 return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>();
145}
146
147void
148StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy)
149{
150 this->install(strategy);
151
152 // don't use .insert here, because it will invoke findEffectiveStrategy
153 // which expects an existing root entry
154 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name());
155 shared_ptr<Entry> entry = make_shared<Entry>(Name());
156 nameTreeEntry->setStrategyChoiceEntry(entry);
157 ++m_nItems;
158 NFD_LOG_INFO("setDefaultStrategy(" << strategy->getName() << ") new entry");
159
160 entry->setStrategy(strategy);
161}
162
163void
164StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry,
165 shared_ptr<fw::Strategy> oldStrategy,
166 shared_ptr<fw::Strategy> newStrategy)
167{
168 entry->setStrategy(newStrategy);
169 if (oldStrategy == newStrategy) {
170 return;
171 }
172
173 // TODO delete incompatible StrategyInfo
174 NFD_LOG_WARN("changeStrategy(" << entry->getPrefix() << ") " <<
175 "runtime strategy change not implemented");
176}
177
178} // namespace nfd