diff --git a/src/base/net/Pools.cpp b/src/base/net/Pools.cpp index 247e13d96..9d4e6db46 100644 --- a/src/base/net/Pools.cpp +++ b/src/base/net/Pools.cpp @@ -25,6 +25,8 @@ #include "base/net/Pools.h" #include "common/log/Log.h" +#include "common/net/strategies/FailoverStrategy.h" +#include "common/net/strategies/SinglePoolStrategy.h" #include "rapidjson/document.h" @@ -68,6 +70,27 @@ bool xmrig::Pools::setUrl(const char *url) } +xmrig::IStrategy *xmrig::Pools::createStrategy(IStrategyListener *listener) const +{ + if (active() == 1) { + for (const Pool &pool : m_data) { + if (pool.isEnabled()) { + return new SinglePoolStrategy(pool, retryPause(), retries(), listener); + } + } + } + + FailoverStrategy *strategy = new FailoverStrategy(retryPause(), retries(), listener); + for (const Pool &pool : m_data) { + if (pool.isEnabled()) { + strategy->add(pool); + } + } + + return strategy; +} + + rapidjson::Value xmrig::Pools::toJSON(rapidjson::Document &doc) const { using namespace rapidjson; diff --git a/src/base/net/Pools.h b/src/base/net/Pools.h index 0822af074..d0755aac7 100644 --- a/src/base/net/Pools.h +++ b/src/base/net/Pools.h @@ -35,6 +35,10 @@ namespace xmrig { +class IStrategy; +class IStrategyListener; + + class Pools { public: @@ -56,6 +60,7 @@ public: inline void setVariant(int variant) { current().algorithm().parseVariant(variant); } bool setUrl(const char *url); + IStrategy *createStrategy(IStrategyListener *listener) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; size_t active() const; void adjust(const Algorithm &algorithm); diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 77d4b202b..103e69d24 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -51,12 +51,10 @@ public: inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } inline const char *userAgent() const { return m_userAgent.data(); } - inline const std::vector &pools() const { return m_pools.data(); } + inline const Pools &pools() const { return m_pools; } inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } - inline int retries() const { return m_pools.retries(); } - inline int retryPause() const { return m_pools.retryPause(); } inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } inline const Algorithm &algorithm() const override { return m_algorithm; } diff --git a/src/common/net/strategies/FailoverStrategy.cpp b/src/common/net/strategies/FailoverStrategy.cpp index 95883f701..c833f6e6e 100644 --- a/src/common/net/strategies/FailoverStrategy.cpp +++ b/src/common/net/strategies/FailoverStrategy.cpp @@ -29,7 +29,7 @@ #include "common/Platform.h" -xmrig::FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : +xmrig::FailoverStrategy::FailoverStrategy(const std::vector &pools, int retryPause, int retries, IStrategyListener *listener, bool quiet) : m_quiet(quiet), m_retries(retries), m_retryPause(retryPause), @@ -37,12 +37,23 @@ xmrig::FailoverStrategy::FailoverStrategy(const std::vector &urls, int ret m_index(0), m_listener(listener) { - for (const Pool &url : urls) { - add(url); + for (const Pool &pool : pools) { + add(pool); } } +xmrig::FailoverStrategy::FailoverStrategy(int retryPause, int retries, IStrategyListener *listener, bool quiet) : + m_quiet(quiet), + m_retries(retries), + m_retryPause(retryPause), + m_active(-1), + m_index(0), + m_listener(listener) +{ +} + + xmrig::FailoverStrategy::~FailoverStrategy() { for (Client *client : m_pools) { @@ -51,19 +62,31 @@ xmrig::FailoverStrategy::~FailoverStrategy() } +void xmrig::FailoverStrategy::add(const Pool &pool) +{ + Client *client = new Client(static_cast(m_pools.size()), Platform::userAgent(), this); + client->setPool(pool); + client->setRetries(m_retries); + client->setRetryPause(m_retryPause * 1000); + client->setQuiet(m_quiet); + + m_pools.push_back(client); +} + + int64_t xmrig::FailoverStrategy::submit(const JobResult &result) { if (m_active == -1) { return -1; } - return m_pools[m_active]->submit(result); + return active()->submit(result); } void xmrig::FailoverStrategy::connect() { - m_pools[m_index]->connect(); + m_pools[static_cast(m_index)]->connect(); } @@ -73,7 +96,7 @@ void xmrig::FailoverStrategy::resume() return; } - m_listener->onJob(this, m_pools[m_active], m_pools[m_active]->job()); + m_listener->onJob(this, active(), active()->job()); } @@ -121,8 +144,8 @@ void xmrig::FailoverStrategy::onClose(Client *client, int failures) return; } - if (m_index == client->id() && (m_pools.size() - m_index) > 1) { - m_pools[++m_index]->connect(); + if (m_index == client->id() && (m_pools.size() - static_cast(m_index)) > 1) { + m_pools[static_cast(++m_index)]->connect(); } } @@ -160,15 +183,3 @@ void xmrig::FailoverStrategy::onResultAccepted(Client *client, const SubmitResul { m_listener->onResultAccepted(this, client, result, error); } - - -void xmrig::FailoverStrategy::add(const Pool &pool) -{ - Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); - client->setPool(pool); - client->setRetries(m_retries); - client->setRetryPause(m_retryPause * 1000); - client->setQuiet(m_quiet); - - m_pools.push_back(client); -} diff --git a/src/common/net/strategies/FailoverStrategy.h b/src/common/net/strategies/FailoverStrategy.h index 0bff0e0ab..d2231a30b 100644 --- a/src/common/net/strategies/FailoverStrategy.h +++ b/src/common/net/strategies/FailoverStrategy.h @@ -44,9 +44,12 @@ class IStrategyListener; class FailoverStrategy : public IStrategy, public IClientListener { public: - FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); + FailoverStrategy(const std::vector &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); + FailoverStrategy(int retryPause, int retries, IStrategyListener *listener, bool quiet = false); ~FailoverStrategy() override; + void add(const Pool &pool); + public: inline bool isActive() const override { return m_active >= 0; } @@ -64,7 +67,7 @@ protected: void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: - void add(const Pool &pool); + inline Client *active() const { return m_pools[static_cast(m_active)]; } const bool m_quiet; const int m_retries; diff --git a/src/common/net/strategies/SinglePoolStrategy.cpp b/src/common/net/strategies/SinglePoolStrategy.cpp index f0a51a493..d17a43da1 100644 --- a/src/common/net/strategies/SinglePoolStrategy.cpp +++ b/src/common/net/strategies/SinglePoolStrategy.cpp @@ -87,7 +87,7 @@ void xmrig::SinglePoolStrategy::tick(uint64_t now) } -void xmrig::SinglePoolStrategy::onClose(Client *client, int failures) +void xmrig::SinglePoolStrategy::onClose(Client *, int) { if (!isActive()) { return; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index b477c1e53..82a961178 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -105,8 +105,8 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator); doc.AddMember("pools", m_pools.toJSON(doc), allocator); doc.AddMember("print-time", printTime(), allocator); - doc.AddMember("retries", retries(), allocator); - doc.AddMember("retry-pause", retryPause(), allocator); + doc.AddMember("retries", m_pools.retries(), allocator); + doc.AddMember("retry-pause", m_pools.retryPause(), allocator); doc.AddMember("safe", m_safe, allocator); if (threadsMode() != Simple) { diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 78ace87c3..7be630fa3 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -34,8 +34,6 @@ #include "api/Api.h" #include "common/log/Log.h" #include "common/net/Client.h" -#include "common/net/strategies/FailoverStrategy.h" -#include "common/net/strategies/SinglePoolStrategy.h" #include "common/net/SubmitResult.h" #include "core/Config.h" #include "core/Controller.h" @@ -50,17 +48,11 @@ xmrig::Network::Network(Controller *controller) : { Workers::setListener(this); - const std::vector &pools = controller->config()->pools(); - - if (pools.size() > 1) { - m_strategy = new FailoverStrategy(pools, controller->config()->retryPause(), controller->config()->retries(), this); - } - else { - m_strategy = new SinglePoolStrategy(pools.front(), controller->config()->retryPause(), controller->config()->retries(), this); - } + const Pools &pools = controller->config()->pools(); + m_strategy = pools.createStrategy(this); if (controller->config()->donateLevel() > 0) { - m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front().user(), controller->config()->algorithm().algo(), this); + m_donate = new DonateStrategy(controller->config()->donateLevel(), pools.data().front().user(), controller->config()->algorithm().algo(), this); } m_timer.data = this;