fw: register strategy with macro
refs #2410
Change-Id: Ifa79c194c58d0ebe1bca6236fc0a47debe2da05e
diff --git a/daemon/fw/access-strategy.cpp b/daemon/fw/access-strategy.cpp
index e726d27..97766e1 100644
--- a/daemon/fw/access-strategy.cpp
+++ b/daemon/fw/access-strategy.cpp
@@ -32,6 +32,7 @@
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 616a1b4..0000000
--- 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 <http://www.gnu.org/licenses/>.
- **/
-
-#ifndef NFD_DAEMON_FW_AVAILABLE_STRATEGIES_HPP
-#define NFD_DAEMON_FW_AVAILABLE_STRATEGIES_HPP
-
-#include "strategy.hpp"
-
-namespace nfd {
-namespace fw {
-
-shared_ptr<Strategy>
-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 f64bb66..45dd4a5 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 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 8aca86a..d243a37 100644
--- a/daemon/fw/best-route-strategy2.cpp
+++ b/daemon/fw/best-route-strategy2.cpp
@@ -32,6 +32,7 @@
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 48ab17b..63aa4f3 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 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 d3700e3..f65cf69 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 @@
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 1cd1d16..524fef5 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 <boost/random/uniform_int_distribution.hpp>
namespace nfd {
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index b9a0ce4..9ecb2cd 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -31,6 +31,7 @@
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 28cab80..de86887 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 <http://www.gnu.org/licenses/>.
*/
-#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 @@
return make_shared<BestRouteStrategy2>(ref(forwarder));
}
-template<typename S>
-inline void
-installStrategy(Forwarder& forwarder)
+static std::map<Name, StrategyCreateFunc>&
+getStrategyFactories()
{
- StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
- if (!strategyChoice.hasStrategy(S::STRATEGY_NAME)) {
- strategyChoice.install(make_shared<S>(ref(forwarder)));
- }
+ static std::map<Name, StrategyCreateFunc> strategyFactories;
+ return strategyFactories;
+}
+
+void
+registerStrategyImpl(const Name& strategyName, const StrategyCreateFunc& createFunc)
+{
+ getStrategyFactories().insert({strategyName, createFunc});
}
void
installStrategies(Forwarder& forwarder)
{
- installStrategy<BestRouteStrategy>(forwarder);
- installStrategy<BroadcastStrategy>(forwarder);
- installStrategy<ClientControlStrategy>(forwarder);
- installStrategy<NccStrategy>(forwarder);
- installStrategy<BestRouteStrategy2>(forwarder);
- installStrategy<AccessStrategy>(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 0000000..9f80288
--- /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 <http://www.gnu.org/licenses/>.
+ */
+
+#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<Strategy>
+makeDefaultStrategy(Forwarder& forwarder);
+
+void
+installStrategies(Forwarder& forwarder);
+
+
+typedef std::function<shared_ptr<Strategy>(Forwarder&)> StrategyCreateFunc;
+
+void
+registerStrategyImpl(const Name& strategyName, const StrategyCreateFunc& createFunc);
+
+/** \brief registers a strategy to be installed later
+ */
+template<typename S>
+void
+registerStrategy()
+{
+ registerStrategyImpl(S::STRATEGY_NAME,
+ [] (Forwarder& forwarder) { return make_shared<S>(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<StrategyType>(); \
+ } \
+} 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 813cce0..8eca1db 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 {