Create network strategies in Pools class.

This commit is contained in:
XMRig 2019-02-17 11:25:25 +07:00
parent ee4f6e28f0
commit 3a0fdcac6f
8 changed files with 71 additions and 39 deletions

View file

@ -25,6 +25,8 @@
#include "base/net/Pools.h" #include "base/net/Pools.h"
#include "common/log/Log.h" #include "common/log/Log.h"
#include "common/net/strategies/FailoverStrategy.h"
#include "common/net/strategies/SinglePoolStrategy.h"
#include "rapidjson/document.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 rapidjson::Value xmrig::Pools::toJSON(rapidjson::Document &doc) const
{ {
using namespace rapidjson; using namespace rapidjson;

View file

@ -35,6 +35,10 @@
namespace xmrig { namespace xmrig {
class IStrategy;
class IStrategyListener;
class Pools class Pools
{ {
public: public:
@ -56,6 +60,7 @@ public:
inline void setVariant(int variant) { current().algorithm().parseVariant(variant); } inline void setVariant(int variant) { current().algorithm().parseVariant(variant); }
bool setUrl(const char *url); bool setUrl(const char *url);
IStrategy *createStrategy(IStrategyListener *listener) const;
rapidjson::Value toJSON(rapidjson::Document &doc) const; rapidjson::Value toJSON(rapidjson::Document &doc) const;
size_t active() const; size_t active() const;
void adjust(const Algorithm &algorithm); void adjust(const Algorithm &algorithm);

View file

@ -51,12 +51,10 @@ public:
inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *apiWorkerId() const { return m_apiWorkerId.data(); }
inline const char *logFile() const { return m_logFile.data(); } inline const char *logFile() const { return m_logFile.data(); }
inline const char *userAgent() const { return m_userAgent.data(); } inline const char *userAgent() const { return m_userAgent.data(); }
inline const std::vector<Pool> &pools() const { return m_pools.data(); } inline const Pools &pools() const { return m_pools; }
inline int apiPort() const { return m_apiPort; } inline int apiPort() const { return m_apiPort; }
inline int donateLevel() const { return m_donateLevel; } inline int donateLevel() const { return m_donateLevel; }
inline int printTime() const { return m_printTime; } 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 bool isWatch() const override { return m_watch && !m_fileName.isNull(); }
inline const Algorithm &algorithm() const override { return m_algorithm; } inline const Algorithm &algorithm() const override { return m_algorithm; }

View file

@ -29,7 +29,7 @@
#include "common/Platform.h" #include "common/Platform.h"
xmrig::FailoverStrategy::FailoverStrategy(const std::vector<Pool> &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : xmrig::FailoverStrategy::FailoverStrategy(const std::vector<Pool> &pools, int retryPause, int retries, IStrategyListener *listener, bool quiet) :
m_quiet(quiet), m_quiet(quiet),
m_retries(retries), m_retries(retries),
m_retryPause(retryPause), m_retryPause(retryPause),
@ -37,12 +37,23 @@ xmrig::FailoverStrategy::FailoverStrategy(const std::vector<Pool> &urls, int ret
m_index(0), m_index(0),
m_listener(listener) m_listener(listener)
{ {
for (const Pool &url : urls) { for (const Pool &pool : pools) {
add(url); 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() xmrig::FailoverStrategy::~FailoverStrategy()
{ {
for (Client *client : m_pools) { 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<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);
}
int64_t xmrig::FailoverStrategy::submit(const JobResult &result) int64_t xmrig::FailoverStrategy::submit(const JobResult &result)
{ {
if (m_active == -1) { if (m_active == -1) {
return -1; return -1;
} }
return m_pools[m_active]->submit(result); return active()->submit(result);
} }
void xmrig::FailoverStrategy::connect() void xmrig::FailoverStrategy::connect()
{ {
m_pools[m_index]->connect(); m_pools[static_cast<size_t>(m_index)]->connect();
} }
@ -73,7 +96,7 @@ void xmrig::FailoverStrategy::resume()
return; 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; return;
} }
if (m_index == client->id() && (m_pools.size() - m_index) > 1) { if (m_index == client->id() && (m_pools.size() - static_cast<size_t>(m_index)) > 1) {
m_pools[++m_index]->connect(); m_pools[static_cast<size_t>(++m_index)]->connect();
} }
} }
@ -160,15 +183,3 @@ void xmrig::FailoverStrategy::onResultAccepted(Client *client, const SubmitResul
{ {
m_listener->onResultAccepted(this, client, result, error); 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);
}

View file

@ -44,9 +44,12 @@ class IStrategyListener;
class FailoverStrategy : public IStrategy, public IClientListener class FailoverStrategy : public IStrategy, public IClientListener
{ {
public: public:
FailoverStrategy(const std::vector<Pool> &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); FailoverStrategy(const std::vector<Pool> &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet = false);
FailoverStrategy(int retryPause, int retries, IStrategyListener *listener, bool quiet = false);
~FailoverStrategy() override; ~FailoverStrategy() override;
void add(const Pool &pool);
public: public:
inline bool isActive() const override { return m_active >= 0; } 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; void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override;
private: private:
void add(const Pool &pool); inline Client *active() const { return m_pools[static_cast<size_t>(m_active)]; }
const bool m_quiet; const bool m_quiet;
const int m_retries; const int m_retries;

View file

@ -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()) { if (!isActive()) {
return; return;

View file

@ -105,8 +105,8 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const
doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator); doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator);
doc.AddMember("pools", m_pools.toJSON(doc), allocator); doc.AddMember("pools", m_pools.toJSON(doc), allocator);
doc.AddMember("print-time", printTime(), allocator); doc.AddMember("print-time", printTime(), allocator);
doc.AddMember("retries", retries(), allocator); doc.AddMember("retries", m_pools.retries(), allocator);
doc.AddMember("retry-pause", retryPause(), allocator); doc.AddMember("retry-pause", m_pools.retryPause(), allocator);
doc.AddMember("safe", m_safe, allocator); doc.AddMember("safe", m_safe, allocator);
if (threadsMode() != Simple) { if (threadsMode() != Simple) {

View file

@ -34,8 +34,6 @@
#include "api/Api.h" #include "api/Api.h"
#include "common/log/Log.h" #include "common/log/Log.h"
#include "common/net/Client.h" #include "common/net/Client.h"
#include "common/net/strategies/FailoverStrategy.h"
#include "common/net/strategies/SinglePoolStrategy.h"
#include "common/net/SubmitResult.h" #include "common/net/SubmitResult.h"
#include "core/Config.h" #include "core/Config.h"
#include "core/Controller.h" #include "core/Controller.h"
@ -50,17 +48,11 @@ xmrig::Network::Network(Controller *controller) :
{ {
Workers::setListener(this); Workers::setListener(this);
const std::vector<Pool> &pools = controller->config()->pools(); const Pools &pools = controller->config()->pools();
m_strategy = pools.createStrategy(this);
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);
}
if (controller->config()->donateLevel() > 0) { 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; m_timer.data = this;