Unify network strategies with upcoming proxy.

This commit is contained in:
XMRig 2018-03-17 05:03:14 +07:00
parent a2f747fb0c
commit c46c019c83
10 changed files with 110 additions and 45 deletions

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* 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
* it under the terms of the GNU General Public License as published by
@ -39,6 +39,7 @@ public:
virtual bool isActive() const = 0;
virtual int64_t submit(const JobResult &result) = 0;
virtual void connect() = 0;
virtual void release() = 0;
virtual void resume() = 0;
virtual void stop() = 0;
virtual void tick(uint64_t now) = 0;

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* 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
* it under the terms of the GNU General Public License as published by
@ -39,10 +39,10 @@ class IStrategyListener
public:
virtual ~IStrategyListener() {}
virtual void onActive(Client *client) = 0;
virtual void onJob(Client *client, const Job &job) = 0;
virtual void onPause(IStrategy *strategy) = 0;
virtual void onResultAccepted(Client *client, const SubmitResult &result, const char *error) = 0;
virtual void onActive(IStrategy *strategy, Client *client) = 0;
virtual void onJob(IStrategy *strategy, Client *client, const Job &job) = 0;
virtual void onPause(IStrategy *strategy) = 0;
virtual void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) = 0;
};

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* 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
* it under the terms of the GNU General Public License as published by
@ -40,7 +40,6 @@
#include "net/SubmitResult.h"
#include "net/Url.h"
#include "Options.h"
#include "Platform.h"
#include "workers/Workers.h"
@ -55,14 +54,14 @@ Network::Network(const Options *options) :
const std::vector<Url*> &pools = options->pools();
if (pools.size() > 1) {
m_strategy = new FailoverStrategy(pools, options->retryPause(), options->retries(), Platform::userAgent(), this);
m_strategy = new FailoverStrategy(pools, options->retryPause(), options->retries(), this);
}
else {
m_strategy = new SinglePoolStrategy(pools.front(), options->retryPause(), Platform::userAgent(), this);
m_strategy = new SinglePoolStrategy(pools.front(), options->retryPause(), this);
}
if (m_options->donateLevel() > 0) {
m_donate = new DonateStrategy(options->donateLevel(), options->pools().front()->user(), options->algo(), Platform::userAgent(), this);
m_donate = new DonateStrategy(options->donateLevel(), options->pools().front()->user(), options->algo(), this);
}
m_timer.data = this;
@ -93,7 +92,7 @@ void Network::stop()
}
void Network::onActive(Client *client)
void Network::onActive(IStrategy *strategy, Client *client)
{
if (client->id() == -1) {
LOG_NOTICE("dev donate started");
@ -106,7 +105,7 @@ 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) {
return;
@ -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);

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* 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
* it under the terms of the GNU General Public License as published by
@ -49,11 +49,11 @@ public:
void stop();
protected:
void onActive(Client *client) override;
void onJob(Client *client, const Job &job) override;
void onActive(IStrategy *strategy, Client *client) override;
void onJob(IStrategy *strategy, Client *client, const Job &job) override;
void onJobResult(const JobResult &result) 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:
constexpr static int kTickInterval = 1 * 1000;

View file

@ -26,6 +26,7 @@
#include "net/Client.h"
#include "net/Job.h"
#include "net/strategies/DonateStrategy.h"
#include "Platform.h"
#include "xmrig.h"
@ -35,7 +36,7 @@ extern "C"
}
DonateStrategy::DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener) :
DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) :
m_active(false),
m_donateTime(level * 60 * 1000),
m_idleTime((100 - level) * 60 * 1000),
@ -49,7 +50,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, const char
Url *url = new Url("thanks.xmrig.com", algo == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true);
m_client = new Client(-1, agent, this);
m_client = new Client(-1, Platform::userAgent(), this);
m_client->setUrl(url);
m_client->setRetryPause(1000);
m_client->setQuiet(true);
@ -75,6 +76,11 @@ void DonateStrategy::connect()
}
void DonateStrategy::release()
{
}
void DonateStrategy::stop()
{
uv_timer_stop(&m_timer);
@ -95,7 +101,7 @@ void DonateStrategy::onClose(Client *client, int failures)
void DonateStrategy::onJobReceived(Client *client, const Job &job)
{
m_listener->onJob(client, job);
m_listener->onJob(this, client, job);
}
@ -106,13 +112,13 @@ void DonateStrategy::onLoginSuccess(Client *client)
}
m_active = true;
m_listener->onActive(client);
m_listener->onActive(this, client);
}
void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
{
m_listener->onResultAccepted(client, result, error);
m_listener->onResultAccepted(this, client, result, error);
}

View file

@ -40,7 +40,7 @@ class Url;
class DonateStrategy : public IStrategy, public IClientListener
{
public:
DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener);
DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener);
public:
inline bool isActive() const override { return m_active; }
@ -48,6 +48,7 @@ public:
int64_t submit(const JobResult &result) override;
void connect() override;
void release() override;
void stop() override;
void tick(uint64_t now) override;

View file

@ -25,17 +25,28 @@
#include "interfaces/IStrategyListener.h"
#include "net/Client.h"
#include "net/strategies/FailoverStrategy.h"
#include "Platform.h"
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener) :
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener) :
m_release(false),
m_retries(retries),
m_retryPause(retryPause),
m_active(-1),
m_index(0),
m_remaining(0),
m_listener(listener)
{
for (const Url *url : urls) {
add(url, agent);
add(url);
}
}
FailoverStrategy::~FailoverStrategy()
{
for (Client *client : m_pools) {
delete client;
}
}
@ -52,13 +63,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()
{
if (!isActive()) {
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());
}
@ -86,6 +109,14 @@ void FailoverStrategy::tick(uint64_t now)
void FailoverStrategy::onClose(Client *client, int failures)
{
if (failures == -1) {
if (m_release) {
m_remaining--;
if (m_remaining == 0) {
delete this;
}
}
return;
}
@ -107,7 +138,7 @@ void FailoverStrategy::onClose(Client *client, int failures)
void FailoverStrategy::onJobReceived(Client *client, const Job &job)
{
if (m_active == client->id()) {
m_listener->onJob(client, job);
m_listener->onJob(this, client, job);
}
}
@ -128,20 +159,20 @@ void FailoverStrategy::onLoginSuccess(Client *client)
if (active >= 0 && active != m_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)
{
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->setRetryPause(m_retryPause * 1000);

View file

@ -40,13 +40,15 @@ class Url;
class FailoverStrategy : public IStrategy, public IClientListener
{
public:
FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener);
FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener);
~FailoverStrategy();
public:
inline bool isActive() const override { return m_active >= 0; }
int64_t submit(const JobResult &result) override;
void connect() override;
void release() override;
void resume() override;
void stop() override;
void tick(uint64_t now) override;
@ -58,12 +60,14 @@ protected:
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override;
private:
void add(const Url *url, const char *agent);
void add(const Url *url);
bool m_release;
const int m_retries;
const int m_retryPause;
int m_active;
int m_index;
int m_remaining;
IStrategyListener *m_listener;
std::vector<Client*> m_pools;
};

View file

@ -25,18 +25,26 @@
#include "interfaces/IStrategyListener.h"
#include "net/Client.h"
#include "net/strategies/SinglePoolStrategy.h"
#include "Platform.h"
SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener) :
SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener) :
m_active(false),
m_release(false),
m_listener(listener)
{
m_client = new Client(0, agent, this);
m_client = new Client(0, Platform::userAgent(), this);
m_client->setUrl(url);
m_client->setRetryPause(retryPause * 1000);
}
SinglePoolStrategy::~SinglePoolStrategy()
{
delete m_client;
}
int64_t SinglePoolStrategy::submit(const JobResult &result)
{
return m_client->submit(result);
@ -49,13 +57,20 @@ void SinglePoolStrategy::connect()
}
void SinglePoolStrategy::release()
{
m_release = true;
m_client->disconnect();
}
void SinglePoolStrategy::resume()
{
if (!isActive()) {
return;
}
m_listener->onJob(m_client, m_client->job());
m_listener->onJob(this, m_client, m_client->job());
}
@ -73,6 +88,11 @@ void SinglePoolStrategy::tick(uint64_t now)
void SinglePoolStrategy::onClose(Client *client, int failures)
{
if (m_release) {
delete this;
return;
}
if (!isActive()) {
return;
}
@ -84,18 +104,18 @@ void SinglePoolStrategy::onClose(Client *client, int failures)
void SinglePoolStrategy::onJobReceived(Client *client, const Job &job)
{
m_listener->onJob(client, job);
m_listener->onJob(this, client, job);
}
void SinglePoolStrategy::onLoginSuccess(Client *client)
{
m_active = true;
m_listener->onActive(client);
m_listener->onActive(this, client);
}
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

@ -37,13 +37,15 @@ class Url;
class SinglePoolStrategy : public IStrategy, public IClientListener
{
public:
SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener);
SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener);
~SinglePoolStrategy();
public:
inline bool isActive() const override { return m_active; }
int64_t submit(const JobResult &result) override;
void connect() override;
void release() override;
void resume() override;
void stop() override;
void tick(uint64_t now) override;
@ -56,6 +58,7 @@ protected:
private:
bool m_active;
bool m_release;
Client *m_client;
IStrategyListener *m_listener;
};