From faf3eb08237a18a8b9f4cb9963e87f7815e01b03 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Mon, 16 Feb 2015 10:50:36 -0700 Subject: [PATCH] fw: register strategy with macro refs #2410 Change-Id: Ifa79c194c58d0ebe1bca6236fc0a47debe2da05e --- daemon/fw/access-strategy.cpp | 1 + daemon/fw/available-strategies.hpp | 42 ---------- daemon/fw/best-route-strategy.cpp | 15 ++-- daemon/fw/best-route-strategy2.cpp | 1 + daemon/fw/broadcast-strategy.cpp | 15 ++-- daemon/fw/client-control-strategy.cpp | 15 ++-- daemon/fw/forwarder.cpp | 16 ++-- daemon/fw/ncc-strategy.cpp | 1 + ...e-strategies.cpp => strategy-registry.cpp} | 35 ++++----- daemon/fw/strategy-registry.hpp | 78 +++++++++++++++++++ daemon/fw/strategy.hpp | 15 ++-- 11 files changed, 138 insertions(+), 96 deletions(-) delete mode 100644 daemon/fw/available-strategies.hpp rename daemon/fw/{available-strategies.cpp => strategy-registry.cpp} (69%) create mode 100644 daemon/fw/strategy-registry.hpp diff --git a/daemon/fw/access-strategy.cpp b/daemon/fw/access-strategy.cpp index e726d27a..97766e11 100644 --- a/daemon/fw/access-strategy.cpp +++ b/daemon/fw/access-strategy.cpp @@ -32,6 +32,7 @@ namespace fw { NFD_LOG_INIT("AccessStrategy"); const Name AccessStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/access/%FD%01"); +NFD_REGISTER_STRATEGY(AccessStrategy); AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name) : Strategy(forwarder, name) diff --git a/daemon/fw/available-strategies.hpp b/daemon/fw/available-strategies.hpp deleted file mode 100644 index 616a1b44..00000000 --- a/daemon/fw/available-strategies.hpp +++ /dev/null @@ -1,42 +0,0 @@ -/* -*- 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 - * - * This file is part of NFD (Named Data Networking Forwarding Daemon). - * See AUTHORS.md for complete list of NFD authors and contributors. - * - * NFD is free software: you can redistribute it and/or modify it under the terms - * of the GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. - * - * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * NFD, e.g., in COPYING.md file. If not, see . - **/ - -#ifndef NFD_DAEMON_FW_AVAILABLE_STRATEGIES_HPP -#define NFD_DAEMON_FW_AVAILABLE_STRATEGIES_HPP - -#include "strategy.hpp" - -namespace nfd { -namespace fw { - -shared_ptr -makeDefaultStrategy(Forwarder& forwarder); - -void -installStrategies(Forwarder& forwarder); - -} // namespace fw -} // namespace nfd - -#endif // NFD_DAEMON_FW_AVAILABLE_STRATEGIES_HPP diff --git a/daemon/fw/best-route-strategy.cpp b/daemon/fw/best-route-strategy.cpp index f64bb667..45dd4a53 100644 --- a/daemon/fw/best-route-strategy.cpp +++ b/daemon/fw/best-route-strategy.cpp @@ -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-2015, 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. @@ -29,6 +29,7 @@ namespace nfd { namespace fw { const Name BestRouteStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%01"); +NFD_REGISTER_STRATEGY(BestRouteStrategy); BestRouteStrategy::BestRouteStrategy(Forwarder& forwarder, const Name& name) : Strategy(forwarder, name) diff --git a/daemon/fw/best-route-strategy2.cpp b/daemon/fw/best-route-strategy2.cpp index 8aca86a2..d243a37b 100644 --- a/daemon/fw/best-route-strategy2.cpp +++ b/daemon/fw/best-route-strategy2.cpp @@ -32,6 +32,7 @@ namespace fw { NFD_LOG_INIT("BestRouteStrategy2"); const Name BestRouteStrategy2::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%02"); +NFD_REGISTER_STRATEGY(BestRouteStrategy2); BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name) : Strategy(forwarder, name) diff --git a/daemon/fw/broadcast-strategy.cpp b/daemon/fw/broadcast-strategy.cpp index 48ab17b3..63aa4f39 100644 --- a/daemon/fw/broadcast-strategy.cpp +++ b/daemon/fw/broadcast-strategy.cpp @@ -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-2015, 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. @@ -29,6 +29,7 @@ namespace nfd { namespace fw { const Name BroadcastStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/broadcast/%FD%01"); +NFD_REGISTER_STRATEGY(BroadcastStrategy); BroadcastStrategy::BroadcastStrategy(Forwarder& forwarder, const Name& name) : Strategy(forwarder, name) diff --git a/daemon/fw/client-control-strategy.cpp b/daemon/fw/client-control-strategy.cpp index d3700e34..f65cf694 100644 --- a/daemon/fw/client-control-strategy.cpp +++ b/daemon/fw/client-control-strategy.cpp @@ -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-2015, 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. @@ -33,6 +33,7 @@ NFD_LOG_INIT("ClientControlStrategy"); const Name ClientControlStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/client-control/%FD%01"); +NFD_REGISTER_STRATEGY(ClientControlStrategy); ClientControlStrategy::ClientControlStrategy(Forwarder& forwarder, const Name& name) : BestRouteStrategy(forwarder, name) diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp index 1cd1d163..524fef57 100644 --- a/daemon/fw/forwarder.cpp +++ b/daemon/fw/forwarder.cpp @@ -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-2015, 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. @@ -26,7 +26,7 @@ #include "forwarder.hpp" #include "core/logger.hpp" #include "core/random.hpp" -#include "available-strategies.hpp" +#include "strategy.hpp" #include namespace nfd { diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp index b9a0ce43..9ecb2cd0 100644 --- a/daemon/fw/ncc-strategy.cpp +++ b/daemon/fw/ncc-strategy.cpp @@ -31,6 +31,7 @@ namespace nfd { namespace fw { const Name NccStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/ncc/%FD%01"); +NFD_REGISTER_STRATEGY(NccStrategy); NccStrategy::NccStrategy(Forwarder& forwarder, const Name& name) : Strategy(forwarder, name) diff --git a/daemon/fw/available-strategies.cpp b/daemon/fw/strategy-registry.cpp similarity index 69% rename from daemon/fw/available-strategies.cpp rename to daemon/fw/strategy-registry.cpp index 28cab803..de868877 100644 --- a/daemon/fw/available-strategies.cpp +++ b/daemon/fw/strategy-registry.cpp @@ -23,12 +23,8 @@ * NFD, e.g., in COPYING.md file. If not, see . */ -#include "best-route-strategy.hpp" -#include "broadcast-strategy.hpp" -#include "client-control-strategy.hpp" -#include "ncc-strategy.hpp" +#include "strategy-registry.hpp" #include "best-route-strategy2.hpp" -#include "access-strategy.hpp" namespace nfd { namespace fw { @@ -39,25 +35,28 @@ makeDefaultStrategy(Forwarder& forwarder) return make_shared(ref(forwarder)); } -template -inline void -installStrategy(Forwarder& forwarder) +static std::map& +getStrategyFactories() { - StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); - if (!strategyChoice.hasStrategy(S::STRATEGY_NAME)) { - strategyChoice.install(make_shared(ref(forwarder))); - } + static std::map strategyFactories; + return strategyFactories; +} + +void +registerStrategyImpl(const Name& strategyName, const StrategyCreateFunc& createFunc) +{ + getStrategyFactories().insert({strategyName, createFunc}); } void installStrategies(Forwarder& forwarder) { - installStrategy(forwarder); - installStrategy(forwarder); - installStrategy(forwarder); - installStrategy(forwarder); - installStrategy(forwarder); - installStrategy(forwarder); + StrategyChoice& sc = forwarder.getStrategyChoice(); + for (const auto& pair : getStrategyFactories()) { + if (!sc.hasStrategy(pair.first, true)) { + sc.install(pair.second(forwarder)); + } + } } } // namespace fw diff --git a/daemon/fw/strategy-registry.hpp b/daemon/fw/strategy-registry.hpp new file mode 100644 index 00000000..9f802889 --- /dev/null +++ b/daemon/fw/strategy-registry.hpp @@ -0,0 +1,78 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014-2015, 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. + * + * NFD is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NFD, e.g., in COPYING.md file. If not, see . + */ + +#ifndef NFD_DAEMON_FW_STRATEGY_REGISTRY_HPP +#define NFD_DAEMON_FW_STRATEGY_REGISTRY_HPP + +#include "common.hpp" + +namespace nfd { + +class Forwarder; + +namespace fw { + +class Strategy; + +shared_ptr +makeDefaultStrategy(Forwarder& forwarder); + +void +installStrategies(Forwarder& forwarder); + + +typedef std::function(Forwarder&)> StrategyCreateFunc; + +void +registerStrategyImpl(const Name& strategyName, const StrategyCreateFunc& createFunc); + +/** \brief registers a strategy to be installed later + */ +template +void +registerStrategy() +{ + registerStrategyImpl(S::STRATEGY_NAME, + [] (Forwarder& forwarder) { return make_shared(ref(forwarder)); }); +} + +/** \brief registers a built-in strategy + * + * This macro should appear once in .cpp of each built-in strategy. + */ +#define NFD_REGISTER_STRATEGY(StrategyType) \ +static class NfdAuto ## StrategyType ## StrategyRegistrationClass \ +{ \ +public: \ + NfdAuto ## StrategyType ## StrategyRegistrationClass() \ + { \ + ::nfd::fw::registerStrategy(); \ + } \ +} g_nfdAuto ## StrategyType ## StrategyRegistrationVariable + +} // namespace fw +} // namespace nfd + +#endif // NFD_DAEMON_FW_STRATEGY_REGISTRY_HPP diff --git a/daemon/fw/strategy.hpp b/daemon/fw/strategy.hpp index 813cce08..8eca1db7 100644 --- a/daemon/fw/strategy.hpp +++ b/daemon/fw/strategy.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-2015, 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. @@ -27,6 +27,7 @@ #define NFD_DAEMON_FW_STRATEGY_HPP #include "forwarder.hpp" +#include "strategy-registry.hpp" #include "table/measurements-accessor.hpp" namespace nfd {