Merge branch 'feature-donate-failover'

This commit is contained in:
XMRig 2018-03-17 17:57:02 +07:00
commit e58429a044
15 changed files with 197 additions and 103 deletions

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -39,6 +39,7 @@ public:
virtual bool isActive() const = 0; virtual bool isActive() const = 0;
virtual int64_t submit(const JobResult &result) = 0; virtual int64_t submit(const JobResult &result) = 0;
virtual void connect() = 0; virtual void connect() = 0;
virtual void release() = 0;
virtual void resume() = 0; virtual void resume() = 0;
virtual void stop() = 0; virtual void stop() = 0;
virtual void tick(uint64_t now) = 0; virtual void tick(uint64_t now) = 0;

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -39,10 +39,10 @@ class IStrategyListener
public: public:
virtual ~IStrategyListener() {} virtual ~IStrategyListener() {}
virtual void onActive(Client *client) = 0; virtual void onActive(IStrategy *strategy, Client *client) = 0;
virtual void onJob(Client *client, const Job &job) = 0; virtual void onJob(IStrategy *strategy, Client *client, const Job &job) = 0;
virtual void onPause(IStrategy *strategy) = 0; virtual void onPause(IStrategy *strategy) = 0;
virtual void onResultAccepted(Client *client, const SubmitResult &result, const char *error) = 0; virtual void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) = 0;
}; };

View file

@ -61,6 +61,7 @@ public:
inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint32_t diff() const { return (uint32_t) m_diff; }
inline uint64_t target() const { return m_target; } inline uint64_t target() const { return m_target; }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline void setPoolId(int poolId) { m_poolId = poolId; }
inline void setThreadId(int threadId) { m_threadId = threadId; } inline void setThreadId(int threadId) { m_threadId = threadId; }
static bool fromHex(const char* in, unsigned int len, unsigned char* out); static bool fromHex(const char* in, unsigned int len, unsigned char* out);

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -40,7 +40,6 @@
#include "net/SubmitResult.h" #include "net/SubmitResult.h"
#include "net/Url.h" #include "net/Url.h"
#include "Options.h" #include "Options.h"
#include "Platform.h"
#include "workers/Workers.h" #include "workers/Workers.h"
@ -55,14 +54,14 @@ Network::Network(const Options *options) :
const std::vector<Url*> &pools = options->pools(); const std::vector<Url*> &pools = options->pools();
if (pools.size() > 1) { if (pools.size() > 1) {
m_strategy = new FailoverStrategy(pools, Platform::userAgent(), this); m_strategy = new FailoverStrategy(pools, options->retryPause(), options->retries(), this);
} }
else { else {
m_strategy = new SinglePoolStrategy(pools.front(), Platform::userAgent(), this); m_strategy = new SinglePoolStrategy(pools.front(), options->retryPause(), this);
} }
if (m_options->donateLevel() > 0) { if (m_options->donateLevel() > 0) {
m_donate = new DonateStrategy(Platform::userAgent(), this); m_donate = new DonateStrategy(options->donateLevel(), options->pools().front()->user(), options->algo(), this);
} }
m_timer.data = this; m_timer.data = this;
@ -93,9 +92,9 @@ void Network::stop()
} }
void Network::onActive(Client *client) void Network::onActive(IStrategy *strategy, Client *client)
{ {
if (client->id() == -1) { if (m_donate && m_donate == strategy) {
LOG_NOTICE("dev donate started"); LOG_NOTICE("dev donate started");
return; return;
} }
@ -106,13 +105,13 @@ void Network::onActive(Client *client)
} }
void Network::onJob(Client *client, const Job &job) void Network::onJob(IStrategy *strategy, Client *client, const Job &job)
{ {
if (m_donate && m_donate->isActive() && client->id() != -1) { if (m_donate && m_donate->isActive() && m_donate != strategy) {
return; return;
} }
setJob(client, job); setJob(client, job, m_donate == strategy);
} }
@ -142,7 +141,7 @@ void Network::onPause(IStrategy *strategy)
} }
void Network::onResultAccepted(Client *client, const SubmitResult &result, const char *error) void Network::onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error)
{ {
m_state.add(result, error); m_state.add(result, error);
@ -159,7 +158,7 @@ void Network::onResultAccepted(Client *client, const SubmitResult &result, const
} }
void Network::setJob(Client *client, const Job &job) void Network::setJob(Client *client, const Job &job, bool donate)
{ {
if (m_options->colors()) { if (m_options->colors()) {
LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff()); LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff());
@ -169,7 +168,7 @@ void Network::setJob(Client *client, const Job &job)
} }
m_state.diff = job.diff(); m_state.diff = job.diff();
Workers::setJob(job); Workers::setJob(job, donate);
} }

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -49,16 +49,16 @@ public:
void stop(); void stop();
protected: protected:
void onActive(Client *client) override; void onActive(IStrategy *strategy, Client *client) override;
void onJob(Client *client, const Job &job) override; void onJob(IStrategy *strategy, Client *client, const Job &job) override;
void onJobResult(const JobResult &result) override; void onJobResult(const JobResult &result) override;
void onPause(IStrategy *strategy) override; void onPause(IStrategy *strategy) override;
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override;
private: private:
constexpr static int kTickInterval = 1 * 1000; constexpr static int kTickInterval = 1 * 1000;
void setJob(Client *client, const Job &job); void setJob(Client *client, const Job &job, bool donate);
void tick(); void tick();
static void onTick(uv_timer_t *handle); static void onTick(uv_timer_t *handle);

View file

@ -26,7 +26,8 @@
#include "net/Client.h" #include "net/Client.h"
#include "net/Job.h" #include "net/Job.h"
#include "net/strategies/DonateStrategy.h" #include "net/strategies/DonateStrategy.h"
#include "Options.h" #include "net/strategies/FailoverStrategy.h"
#include "Platform.h"
#include "xmrig.h" #include "xmrig.h"
@ -36,27 +37,36 @@ extern "C"
} }
DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : const static char *kDonatePool = "thanks.xmrig.com";
const static char *kDonatePoolIP = "45.76.34.221";
DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) :
m_active(false), m_active(false),
m_donateTime(Options::i()->donateLevel() * 60 * 1000), m_donateTime(level * 60 * 1000),
m_idleTime((100 - Options::i()->donateLevel()) * 60 * 1000), m_idleTime((100 - level) * 60 * 1000),
m_strategy(nullptr),
m_listener(listener) m_listener(listener)
{ {
uint8_t hash[200]; uint8_t hash[200];
char userId[65] = { 0 }; char userId[65] = { 0 };
const char *user = Options::i()->pools().front()->user();
keccak(reinterpret_cast<const uint8_t *>(user), static_cast<int>(strlen(user)), hash, sizeof(hash)); keccak(reinterpret_cast<const uint8_t *>(user), static_cast<int>(strlen(user)), hash, sizeof(hash));
Job::toHex(hash, 32, userId); Job::toHex(hash, 32, userId);
Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); if (algo == xmrig::ALGO_CRYPTONIGHT) {
m_pools.push_back(new Url(kDonatePool, 80, userId, nullptr, false, true));
m_pools.push_back(new Url(kDonatePool, 443, userId, nullptr, false, true));
m_pools.push_back(new Url(kDonatePoolIP, 80, userId, nullptr, false, true));
m_pools.push_back(new Url(kDonatePoolIP, 443, userId, nullptr, false, true));
m_pools.push_back(new Url("emergency.xmrig.com", 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false));
}
else {
m_pools.push_back(new Url(kDonatePool, 5555, userId, nullptr, false, true));
m_pools.push_back(new Url(kDonatePoolIP, 5555, userId, nullptr, false, true));
}
m_client = new Client(-1, agent, this); m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true);
m_client->setUrl(url);
m_client->setRetryPause(Options::i()->retryPause() * 1000);
m_client->setQuiet(true);
delete url;
m_timer.data = this; m_timer.data = this;
uv_timer_init(uv_default_loop(), &m_timer); uv_timer_init(uv_default_loop(), &m_timer);
@ -65,56 +75,66 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) :
} }
DonateStrategy::~DonateStrategy()
{
}
int64_t DonateStrategy::submit(const JobResult &result) int64_t DonateStrategy::submit(const JobResult &result)
{ {
return m_client->submit(result); return m_strategy->submit(result);
} }
void DonateStrategy::connect() void DonateStrategy::connect()
{ {
m_client->connect(); m_strategy->connect();
}
void DonateStrategy::release()
{
} }
void DonateStrategy::stop() void DonateStrategy::stop()
{ {
uv_timer_stop(&m_timer); uv_timer_stop(&m_timer);
m_client->disconnect(); m_strategy->stop();
} }
void DonateStrategy::tick(uint64_t now) void DonateStrategy::tick(uint64_t now)
{ {
m_client->tick(now); m_strategy->tick(now);
} }
void DonateStrategy::onClose(Client *client, int failures) void DonateStrategy::onActive(IStrategy *strategy, Client *client)
{
}
void DonateStrategy::onJobReceived(Client *client, const Job &job)
{
m_listener->onJob(client, job);
}
void DonateStrategy::onLoginSuccess(Client *client)
{ {
if (!isActive()) { if (!isActive()) {
uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0); uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0);
} }
m_active = true; m_active = true;
m_listener->onActive(client); m_listener->onActive(this, client);
} }
void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) void DonateStrategy::onJob(IStrategy *strategy, Client *client, const Job &job)
{ {
m_listener->onResultAccepted(client, result, error); m_listener->onJob(this, client, job);
}
void DonateStrategy::onPause(IStrategy *strategy)
{
}
void DonateStrategy::onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error)
{
m_listener->onResultAccepted(this, client, result, error);
} }
@ -126,7 +146,7 @@ void DonateStrategy::idle()
void DonateStrategy::suspend() void DonateStrategy::suspend()
{ {
m_client->disconnect(); m_strategy->stop();
m_active = false; m_active = false;
m_listener->onPause(this); m_listener->onPause(this);

View file

@ -26,10 +26,12 @@
#include <uv.h> #include <uv.h>
#include <vector>
#include "interfaces/IClientListener.h" #include "interfaces/IClientListener.h"
#include "interfaces/IStrategy.h" #include "interfaces/IStrategy.h"
#include "interfaces/IStrategyListener.h"
class Client; class Client;
@ -37,10 +39,11 @@ class IStrategyListener;
class Url; class Url;
class DonateStrategy : public IStrategy, public IClientListener class DonateStrategy : public IStrategy, public IStrategyListener
{ {
public: public:
DonateStrategy(const char *agent, IStrategyListener *listener); DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener);
~DonateStrategy();
public: public:
inline bool isActive() const override { return m_active; } inline bool isActive() const override { return m_active; }
@ -48,14 +51,15 @@ public:
int64_t submit(const JobResult &result) override; int64_t submit(const JobResult &result) override;
void connect() override; void connect() override;
void release() override;
void stop() override; void stop() override;
void tick(uint64_t now) override; void tick(uint64_t now) override;
protected: protected:
void onClose(Client *client, int failures) override; void onActive(IStrategy *strategy, Client *client) override;
void onJobReceived(Client *client, const Job &job) override; void onJob(IStrategy *strategy, Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override; void onPause(IStrategy *strategy) override;
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override;
private: private:
void idle(); void idle();
@ -64,10 +68,11 @@ private:
static void onTimer(uv_timer_t *handle); static void onTimer(uv_timer_t *handle);
bool m_active; bool m_active;
Client *m_client;
const int m_donateTime; const int m_donateTime;
const int m_idleTime; const int m_idleTime;
IStrategy *m_strategy;
IStrategyListener *m_listener; IStrategyListener *m_listener;
std::vector<Url*> m_pools;
uv_timer_t m_timer; uv_timer_t m_timer;
}; };

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -25,16 +25,29 @@
#include "interfaces/IStrategyListener.h" #include "interfaces/IStrategyListener.h"
#include "net/Client.h" #include "net/Client.h"
#include "net/strategies/FailoverStrategy.h" #include "net/strategies/FailoverStrategy.h"
#include "Options.h" #include "Platform.h"
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, const char *agent, IStrategyListener *listener) : FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) :
m_release(false),
m_quiet(quiet),
m_retries(retries),
m_retryPause(retryPause),
m_active(-1), m_active(-1),
m_index(0), m_index(0),
m_remaining(0),
m_listener(listener) m_listener(listener)
{ {
for (const Url *url : urls) { for (const Url *url : urls) {
add(url, agent); add(url);
}
}
FailoverStrategy::~FailoverStrategy()
{
for (Client *client : m_pools) {
delete client;
} }
} }
@ -51,13 +64,25 @@ void FailoverStrategy::connect()
} }
void FailoverStrategy::release()
{
m_release = true;
for (size_t i = 0; i < m_pools.size(); ++i) {
if (m_pools[i]->disconnect()) {
m_remaining++;
}
}
}
void FailoverStrategy::resume() void FailoverStrategy::resume()
{ {
if (!isActive()) { if (!isActive()) {
return; return;
} }
m_listener->onJob( m_pools[m_active], m_pools[m_active]->job()); m_listener->onJob(this, m_pools[m_active], m_pools[m_active]->job());
} }
@ -85,6 +110,14 @@ void FailoverStrategy::tick(uint64_t now)
void FailoverStrategy::onClose(Client *client, int failures) void FailoverStrategy::onClose(Client *client, int failures)
{ {
if (failures == -1) { if (failures == -1) {
if (m_release) {
m_remaining--;
if (m_remaining == 0) {
delete this;
}
}
return; return;
} }
@ -93,7 +126,7 @@ void FailoverStrategy::onClose(Client *client, int failures)
m_listener->onPause(this); m_listener->onPause(this);
} }
if (m_index == 0 && failures < Options::i()->retries()) { if (m_index == 0 && failures < m_retries) {
return; return;
} }
@ -106,7 +139,7 @@ void FailoverStrategy::onClose(Client *client, int failures)
void FailoverStrategy::onJobReceived(Client *client, const Job &job) void FailoverStrategy::onJobReceived(Client *client, const Job &job)
{ {
if (m_active == client->id()) { if (m_active == client->id()) {
m_listener->onJob(client, job); m_listener->onJob(this, client, job);
} }
} }
@ -127,22 +160,23 @@ void FailoverStrategy::onLoginSuccess(Client *client)
if (active >= 0 && active != m_active) { if (active >= 0 && active != m_active) {
m_index = m_active = active; m_index = m_active = active;
m_listener->onActive(client); m_listener->onActive(this, client);
} }
} }
void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
{ {
m_listener->onResultAccepted(client, result, error); m_listener->onResultAccepted(this, client, result, error);
} }
void FailoverStrategy::add(const Url *url, const char *agent) void FailoverStrategy::add(const Url *url)
{ {
Client *client = new Client((int) m_pools.size(), agent, this); Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this);
client->setUrl(url); client->setUrl(url);
client->setRetryPause(Options::i()->retryPause() * 1000); client->setRetryPause(m_retryPause * 1000);
client->setQuiet(m_quiet);
m_pools.push_back(client); m_pools.push_back(client);
} }

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -40,13 +40,15 @@ class Url;
class FailoverStrategy : public IStrategy, public IClientListener class FailoverStrategy : public IStrategy, public IClientListener
{ {
public: public:
FailoverStrategy(const std::vector<Url*> &urls, const char *agent, IStrategyListener *listener); FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false);
~FailoverStrategy();
public: public:
inline bool isActive() const override { return m_active >= 0; } inline bool isActive() const override { return m_active >= 0; }
int64_t submit(const JobResult &result) override; int64_t submit(const JobResult &result) override;
void connect() override; void connect() override;
void release() override;
void resume() override; void resume() override;
void stop() override; void stop() override;
void tick(uint64_t now) override; void tick(uint64_t now) override;
@ -58,10 +60,15 @@ 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 Url *url, const char *agent); void add(const Url *url);
bool m_release;
const bool m_quiet;
const int m_retries;
const int m_retryPause;
int m_active; int m_active;
int m_index; int m_index;
int m_remaining;
IStrategyListener *m_listener; IStrategyListener *m_listener;
std::vector<Client*> m_pools; std::vector<Client*> m_pools;
}; };

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -25,16 +25,24 @@
#include "interfaces/IStrategyListener.h" #include "interfaces/IStrategyListener.h"
#include "net/Client.h" #include "net/Client.h"
#include "net/strategies/SinglePoolStrategy.h" #include "net/strategies/SinglePoolStrategy.h"
#include "Options.h" #include "Platform.h"
SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener) : SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet) :
m_active(false), m_active(false),
m_release(false),
m_listener(listener) m_listener(listener)
{ {
m_client = new Client(0, agent, this); m_client = new Client(0, Platform::userAgent(), this);
m_client->setUrl(url); m_client->setUrl(url);
m_client->setRetryPause(Options::i()->retryPause() * 1000); m_client->setRetryPause(retryPause * 1000);
m_client->setQuiet(quiet);
}
SinglePoolStrategy::~SinglePoolStrategy()
{
delete m_client;
} }
@ -50,13 +58,20 @@ void SinglePoolStrategy::connect()
} }
void SinglePoolStrategy::release()
{
m_release = true;
m_client->disconnect();
}
void SinglePoolStrategy::resume() void SinglePoolStrategy::resume()
{ {
if (!isActive()) { if (!isActive()) {
return; return;
} }
m_listener->onJob(m_client, m_client->job()); m_listener->onJob(this, m_client, m_client->job());
} }
@ -74,6 +89,11 @@ void SinglePoolStrategy::tick(uint64_t now)
void SinglePoolStrategy::onClose(Client *client, int failures) void SinglePoolStrategy::onClose(Client *client, int failures)
{ {
if (m_release) {
delete this;
return;
}
if (!isActive()) { if (!isActive()) {
return; return;
} }
@ -85,18 +105,18 @@ void SinglePoolStrategy::onClose(Client *client, int failures)
void SinglePoolStrategy::onJobReceived(Client *client, const Job &job) void SinglePoolStrategy::onJobReceived(Client *client, const Job &job)
{ {
m_listener->onJob(client, job); m_listener->onJob(this, client, job);
} }
void SinglePoolStrategy::onLoginSuccess(Client *client) void SinglePoolStrategy::onLoginSuccess(Client *client)
{ {
m_active = true; m_active = true;
m_listener->onActive(client); m_listener->onActive(this, client);
} }
void SinglePoolStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) void SinglePoolStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
{ {
m_listener->onResultAccepted(client, result, error); m_listener->onResultAccepted(this, client, result, error);
} }

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -37,13 +37,15 @@ class Url;
class SinglePoolStrategy : public IStrategy, public IClientListener class SinglePoolStrategy : public IStrategy, public IClientListener
{ {
public: public:
SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener); SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet = false);
~SinglePoolStrategy();
public: public:
inline bool isActive() const override { return m_active; } inline bool isActive() const override { return m_active; }
int64_t submit(const JobResult &result) override; int64_t submit(const JobResult &result) override;
void connect() override; void connect() override;
void release() override;
void resume() override; void resume() override;
void stop() override; void stop() override;
void tick(uint64_t now) override; void tick(uint64_t now) override;
@ -56,6 +58,7 @@ protected:
private: private:
bool m_active; bool m_active;
bool m_release;
Client *m_client; Client *m_client;
IStrategyListener *m_listener; IStrategyListener *m_listener;
}; };

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View file

@ -83,10 +83,14 @@ void Workers::setEnabled(bool enabled)
} }
void Workers::setJob(const Job &job) void Workers::setJob(const Job &job, bool donate)
{ {
uv_rwlock_wrlock(&m_rwlock); uv_rwlock_wrlock(&m_rwlock);
m_job = job; m_job = job;
if (donate) {
m_job.setPoolId(-1);
}
uv_rwlock_wrunlock(&m_rwlock); uv_rwlock_wrunlock(&m_rwlock);
m_active = true; m_active = true;

View file

@ -45,7 +45,7 @@ public:
static Job job(); static Job job();
static void printHashrate(bool detail); static void printHashrate(bool detail);
static void setEnabled(bool enabled); static void setEnabled(bool enabled);
static void setJob(const Job &job); static void setJob(const Job &job, bool donate);
static void start(int64_t affinity, int priority); static void start(int64_t affinity, int priority);
static void stop(); static void stop();
static void submit(const JobResult &result); static void submit(const JobResult &result);