diff --git a/daemon/mgmt/strategy-choice-manager.cpp b/daemon/mgmt/strategy-choice-manager.cpp index a28e237a..1f0c2088 100644 --- a/daemon/mgmt/strategy-choice-manager.cpp +++ b/daemon/mgmt/strategy-choice-manager.cpp @@ -35,7 +35,7 @@ StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice, Dispatcher& dispatcher, CommandValidator& validator) : NfdManagerBase(dispatcher, validator, "strategy-choice") - , m_strategyChoice(strategyChoice) + , m_table(strategyChoice) { registerCommandHandler("set", bind(&StrategyChoiceManager::setStrategy, this, _2, _3, _4, _5)); @@ -54,15 +54,15 @@ StrategyChoiceManager::setStrategy(const Name& topPrefix, const Interest& intere const Name& prefix = parameters.getName(); const Name& selectedStrategy = parameters.getStrategy(); - if (!m_strategyChoice.hasStrategy(selectedStrategy)) { + if (!m_table.hasStrategy(selectedStrategy)) { NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: " << parameters.getStrategy()); return done(ControlResponse(504, "Unsupported strategy")); } - if (m_strategyChoice.insert(prefix, selectedStrategy)) { + if (m_table.insert(prefix, selectedStrategy)) { NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); - auto currentStrategyChoice = m_strategyChoice.get(prefix); + auto currentStrategyChoice = m_table.get(prefix); BOOST_ASSERT(currentStrategyChoice.first); parameters.setStrategy(currentStrategyChoice.second); return done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); @@ -78,7 +78,7 @@ StrategyChoiceManager::unsetStrategy(const Name& topPrefix, const Interest& inte ControlParameters parameters, const ndn::mgmt::CommandContinuation& done) { - m_strategyChoice.erase(parameters.getName()); + m_table.erase(parameters.getName()); NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); @@ -88,7 +88,7 @@ void StrategyChoiceManager::listChoices(const Name& topPrefix, const Interest& interest, ndn::mgmt::StatusDatasetContext& context) { - for (auto&& i : m_strategyChoice) { + for (auto&& i : m_table) { ndn::nfd::StrategyChoice entry; entry.setName(i.getPrefix()).setStrategy(i.getStrategyName()); context.append(entry.wireEncode()); diff --git a/daemon/mgmt/strategy-choice-manager.hpp b/daemon/mgmt/strategy-choice-manager.hpp index eff3418f..9fd4ad30 100644 --- a/daemon/mgmt/strategy-choice-manager.hpp +++ b/daemon/mgmt/strategy-choice-manager.hpp @@ -30,7 +30,9 @@ namespace nfd { +namespace strategy_choice { class StrategyChoice; +} // namespace strategy_choice /** * @brief implement the Strategy Choice Management of NFD Management Protocol. @@ -39,7 +41,7 @@ class StrategyChoice; class StrategyChoiceManager : public NfdManagerBase { public: - StrategyChoiceManager(StrategyChoice& strategyChoice, + StrategyChoiceManager(strategy_choice::StrategyChoice& table, Dispatcher& dispatcher, CommandValidator& validator); @@ -59,7 +61,7 @@ private: ndn::mgmt::StatusDatasetContext& context); private: - StrategyChoice& m_strategyChoice; + strategy_choice::StrategyChoice& m_table; }; } // namespace nfd diff --git a/daemon/table/name-tree-entry.cpp b/daemon/table/name-tree-entry.cpp index 87853f2c..32440596 100644 --- a/daemon/table/name-tree-entry.cpp +++ b/daemon/table/name-tree-entry.cpp @@ -119,15 +119,15 @@ Entry::setMeasurementsEntry(shared_ptr measurementsEntry) } void -Entry::setStrategyChoiceEntry(shared_ptr strategyChoiceEntry) +Entry::setStrategyChoiceEntry(unique_ptr strategyChoiceEntry) { BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry.expired()); if (m_strategyChoiceEntry != nullptr) { m_strategyChoiceEntry->m_nameTreeEntry.reset(); } + m_strategyChoiceEntry = std::move(strategyChoiceEntry); - m_strategyChoiceEntry = strategyChoiceEntry; if (m_strategyChoiceEntry != nullptr) { m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this(); } diff --git a/daemon/table/name-tree-entry.hpp b/daemon/table/name-tree-entry.hpp index c6505870..7bade1f4 100644 --- a/daemon/table/name-tree-entry.hpp +++ b/daemon/table/name-tree-entry.hpp @@ -120,9 +120,9 @@ public: // attached table entries getMeasurementsEntry() const; void - setStrategyChoiceEntry(shared_ptr strategyChoiceEntry); + setStrategyChoiceEntry(unique_ptr strategyChoiceEntry); - shared_ptr + strategy_choice::Entry* getStrategyChoiceEntry() const; private: @@ -136,7 +136,7 @@ private: unique_ptr m_fibEntry; std::vector > m_pitEntries; shared_ptr m_measurementsEntry; - shared_ptr m_strategyChoiceEntry; + unique_ptr m_strategyChoiceEntry; // get the Name Tree Node that is associated with this Name Tree Entry Node* m_node; @@ -211,10 +211,10 @@ Entry::getMeasurementsEntry() const return m_measurementsEntry; } -inline shared_ptr +inline strategy_choice::Entry* Entry::getStrategyChoiceEntry() const { - return m_strategyChoiceEntry; + return m_strategyChoiceEntry.get(); } } // namespace name_tree diff --git a/daemon/table/name-tree.cpp b/daemon/table/name-tree.cpp index 796e701b..bc7d7c74 100644 --- a/daemon/table/name-tree.cpp +++ b/daemon/table/name-tree.cpp @@ -277,7 +277,7 @@ shared_ptr NameTree::lookup(const strategy_choice::Entry& strategyChoiceEntry) const { shared_ptr nte = this->getEntry(strategyChoiceEntry); - BOOST_ASSERT(nte == nullptr || nte->getStrategyChoiceEntry().get() == &strategyChoiceEntry); + BOOST_ASSERT(nte == nullptr || nte->getStrategyChoiceEntry() == &strategyChoiceEntry); return nte; } diff --git a/daemon/table/strategy-choice.cpp b/daemon/table/strategy-choice.cpp index de7cb139..5778237d 100644 --- a/daemon/table/strategy-choice.cpp +++ b/daemon/table/strategy-choice.cpp @@ -30,8 +30,8 @@ #include "measurements-entry.hpp" namespace nfd { +namespace strategy_choice { -using strategy_choice::Entry; using fw::Strategy; NFD_LOG_INIT("StrategyChoice"); @@ -96,7 +96,7 @@ StrategyChoice::insert(const Name& prefix, const Name& strategyName) } shared_ptr nte = m_nameTree.lookup(prefix); - shared_ptr entry = nte->getStrategyChoiceEntry(); + Entry* entry = nte->getStrategyChoiceEntry(); Strategy* oldStrategy = nullptr; if (entry != nullptr) { if (entry->getStrategy().getName() == strategy->getName()) { @@ -110,8 +110,9 @@ StrategyChoice::insert(const Name& prefix, const Name& strategyName) if (entry == nullptr) { oldStrategy = &this->findEffectiveStrategy(prefix); - entry = make_shared(prefix); - nte->setStrategyChoiceEntry(entry); + auto newEntry = make_unique(prefix); + entry = newEntry.get(); + nte->setStrategyChoiceEntry(std::move(newEntry)); ++m_nItems; NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName()); } @@ -131,7 +132,7 @@ StrategyChoice::erase(const Name& prefix) return; } - shared_ptr entry = nte->getStrategyChoiceEntry(); + Entry* entry = nte->getStrategyChoiceEntry(); if (entry == nullptr) { return; } @@ -141,7 +142,7 @@ StrategyChoice::erase(const Name& prefix) Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1)); this->changeStrategy(*entry, oldStrategy, parentStrategy); - nte->setStrategyChoiceEntry(shared_ptr()); + nte->setStrategyChoiceEntry(nullptr); m_nameTree.eraseEntryIfEmpty(nte); --m_nItems; } @@ -151,24 +152,22 @@ StrategyChoice::get(const Name& prefix) const { shared_ptr nte = m_nameTree.findExactMatch(prefix); if (nte == nullptr) { - return { false, Name() }; + return {false, Name()}; } - shared_ptr entry = nte->getStrategyChoiceEntry(); + Entry* entry = nte->getStrategyChoiceEntry(); if (entry == nullptr) { - return { false, Name() }; + return {false, Name()}; } - return { true, entry->getStrategy().getName() }; + return {true, entry->getStrategy().getName()}; } Strategy& StrategyChoice::findEffectiveStrategy(const Name& prefix) const { shared_ptr nte = m_nameTree.findLongestPrefixMatch(prefix, - [] (const name_tree::Entry& entry) { - return entry.getStrategyChoiceEntry() != nullptr; - }); + [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; }); BOOST_ASSERT(nte != nullptr); return nte->getStrategyChoiceEntry()->getStrategy(); @@ -177,14 +176,12 @@ StrategyChoice::findEffectiveStrategy(const Name& prefix) const Strategy& StrategyChoice::findEffectiveStrategy(shared_ptr nte) const { - shared_ptr entry = nte->getStrategyChoiceEntry(); + Entry* entry = nte->getStrategyChoiceEntry(); if (entry != nullptr) return entry->getStrategy(); nte = m_nameTree.findLongestPrefixMatch(nte, - [] (const name_tree::Entry& entry) { - return entry.getStrategyChoiceEntry() != nullptr; - }); + [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; }); BOOST_ASSERT(nte != nullptr); return nte->getStrategyChoiceEntry()->getStrategy(); @@ -213,15 +210,15 @@ StrategyChoice::setDefaultStrategy(shared_ptr strategy) { this->install(strategy); + auto entry = make_unique(Name()); + entry->setStrategy(*strategy); + // don't use .insert here, because it will invoke findEffectiveStrategy // which expects an existing root entry shared_ptr nte = m_nameTree.lookup(Name()); - shared_ptr entry = make_shared(Name()); - nte->setStrategyChoiceEntry(entry); + nte->setStrategyChoiceEntry(std::move(entry)); ++m_nItems; NFD_LOG_INFO("setDefaultStrategy " << strategy->getName()); - - entry->setStrategy(*strategy); } static inline void @@ -244,7 +241,7 @@ clearStrategyInfo(const name_tree::Entry& nte) } void -StrategyChoice::changeStrategy(strategy_choice::Entry& entry, +StrategyChoice::changeStrategy(Entry& entry, fw::Strategy& oldStrategy, fw::Strategy& newStrategy) { @@ -284,4 +281,5 @@ StrategyChoice::begin() const return const_iterator(enumerable.begin()); } +} // namespace strategy_choice } // namespace nfd diff --git a/daemon/table/strategy-choice.hpp b/daemon/table/strategy-choice.hpp index 68ed3953..7082092f 100644 --- a/daemon/table/strategy-choice.hpp +++ b/daemon/table/strategy-choice.hpp @@ -1,12 +1,12 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014, Regents of the University of California, - * Arizona Board of Regents, - * Colorado State University, - * University Pierre & Marie Curie, Sorbonne University, - * Washington University in St. Louis, - * Beijing Institute of Technology, - * The University of Memphis + * Copyright (c) 2014-2016, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. * * This file is part of NFD (Named Data Networking Forwarding Daemon). * See AUTHORS.md for complete list of NFD authors and contributors. @@ -30,6 +30,7 @@ #include "name-tree.hpp" namespace nfd { +namespace strategy_choice { /** \brief represents the Strategy Choice table * @@ -105,7 +106,7 @@ public: // effective strategy public: // enumeration class const_iterator - : public std::iterator + : public std::iterator { public: explicit @@ -113,10 +114,10 @@ public: // enumeration ~const_iterator(); - const strategy_choice::Entry& + const Entry& operator*() const; - shared_ptr + const Entry* operator->() const; const_iterator& @@ -156,7 +157,7 @@ private: setDefaultStrategy(shared_ptr strategy); void - changeStrategy(strategy_choice::Entry& entry, + changeStrategy(Entry& entry, fw::Strategy& oldStrategy, fw::Strategy& newStrategy); @@ -167,7 +168,7 @@ private: NameTree& m_nameTree; size_t m_nItems; - typedef std::map > StrategyInstanceTable; + typedef std::map> StrategyInstanceTable; StrategyInstanceTable m_strategyInstances; }; @@ -210,13 +211,13 @@ StrategyChoice::const_iterator::operator++() return *this; } -inline const strategy_choice::Entry& +inline const Entry& StrategyChoice::const_iterator::operator*() const { return *(m_nameTreeIterator->getStrategyChoiceEntry()); } -inline shared_ptr +inline const Entry* StrategyChoice::const_iterator::operator->() const { return m_nameTreeIterator->getStrategyChoiceEntry(); @@ -234,6 +235,10 @@ StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& return m_nameTreeIterator != other.m_nameTreeIterator; } +} // namespace strategy_choice + +using strategy_choice::StrategyChoice; + } // namespace nfd #endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP