Add classes SinglePoolStrategy, DonateStrategy, IStrategy, IStrategyListener.

This commit is contained in:
XMRig 2017-06-28 06:17:02 +03:00
parent b2d26eb019
commit bd8776b7ee
11 changed files with 414 additions and 108 deletions

View file

@ -15,6 +15,8 @@ set(HEADERS
src/interfaces/IClientListener.h
src/interfaces/IJobResultListener.h
src/interfaces/ILogBackend.h
src/interfaces/IStrategy.h
src/interfaces/IStrategyListener.h
src/interfaces/IWorker.h
src/log/ConsoleLog.h
src/log/FileLog.h
@ -25,6 +27,8 @@ set(HEADERS
src/net/JobResult.h
src/net/Network.h
src/net/Url.h
src/net/strategies/DonateStrategy.h
src/net/strategies/SinglePoolStrategy.h
src/Options.h
src/Summary.h
src/version.h
@ -60,6 +64,8 @@ set(SOURCES
src/net/Job.cpp
src/net/Network.cpp
src/net/Url.cpp
src/net/strategies/DonateStrategy.cpp
src/net/strategies/SinglePoolStrategy.cpp
src/Options.cpp
src/Summary.cpp
src/workers/DoubleWorker.cpp
@ -114,7 +120,7 @@ else()
endif()
add_definitions(/DUNICODE)
#add_definitions(/DAPP_DEBUG)
add_definitions(/DAPP_DEBUG)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
@ -128,7 +134,7 @@ endif()
# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -Wno-strict-aliasing")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -std=c++14 -fno-exceptions -fno-rtti")

View file

@ -0,0 +1,37 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ISTRATEGY_H__
#define __ISTRATEGY_H__
class IStrategy
{
public:
virtual ~IStrategy() {}
virtual void connect() = 0;
};
#endif // __ISTRATEGY_H__

View file

@ -0,0 +1,35 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ISTRATEGYLISTENER_H__
#define __ISTRATEGYLISTENER_H__
class IStrategyListener
{
public:
virtual ~IStrategyListener() {}
};
#endif // __ISTRATEGYLISTENER_H__

View file

@ -60,6 +60,7 @@ public:
void send(char *data);
void setUrl(const Url *url);
void submit(const JobResult &result);
inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; }
inline const char *host() const { return m_url.host(); }
inline const Job &job() const { return m_job; }

View file

@ -28,36 +28,36 @@
#include "log/Log.h"
#include "net/Client.h"
#include "net/Network.h"
#include "net/strategies/DonateStrategy.h"
#include "net/strategies/SinglePoolStrategy.h"
#include "net/Url.h"
#include "Options.h"
#include "workers/Workers.h"
Network::Network(const Options *options) :
m_donate(false),
m_donateActive(false),
m_options(options),
m_pool(0),
m_diff(0)
m_donate(nullptr)
{
Workers::setListener(this);
m_pools.reserve(2);
m_agent = userAgent();
addPool(std::make_unique<Url>().get());
addPool(m_options->pools().front());
// addPool(m_options->backupUrl());
const std::vector<Url*> &pools = options->pools();
m_timer.data = this;
uv_timer_init(uv_default_loop(), &m_timer);
m_strategy = new SinglePoolStrategy(pools.front(), m_agent, this);
if (m_options->donateLevel() > 0) {
m_donate = new DonateStrategy(m_agent, this);
}
}
Network::~Network()
{
for (auto client : m_pools) {
delete client;
}
// for (auto client : m_pools) {
// delete client;
// }
free(m_agent);
}
@ -65,39 +65,35 @@ Network::~Network()
void Network::connect()
{
m_pools[1]->connect();
if (m_options->donateLevel()) {
uv_timer_start(&m_timer, Network::onTimer, (100 - m_options->donateLevel()) * 60 * 1000, 0);
}
m_strategy->connect();
}
void Network::onClose(Client *client, int failures)
{
const int id = client->id();
if (id == 0) {
if (failures == -1) {
stopDonate();
}
// const int id = client->id();
// if (id == 0) {
// if (failures == -1) {
// stopDonate();
// }
return;
}
// return;
// }
if (m_pool == id) {
m_pool = 0;
Workers::pause();
}
// if (m_pool == id) {
// m_pool = 0;
// Workers::pause();
// }
if (id == 1 && m_pools.size() > 2 && failures == m_options->retries()) {
m_pools[2]->connect();
}
// if (id == 1 && m_pools.size() > 2 && failures == m_options->retries()) {
// m_pools[2]->connect();
// }
}
void Network::onJobReceived(Client *client, const Job &job)
{
if (m_donate && client->id() != 0) {
if (m_donateActive && client->id() != 0) {
return;
}
@ -107,50 +103,35 @@ void Network::onJobReceived(Client *client, const Job &job)
void Network::onJobResult(const JobResult &result)
{
if (m_options->colors()) {
LOG_NOTICE("\x1B[01;32mSHARE FOUND");
}
else {
LOG_NOTICE("SHARE FOUND");
}
// if (m_options->colors()) {
// LOG_NOTICE("\x1B[01;32mSHARE FOUND");
// }
// else {
// LOG_NOTICE("SHARE FOUND");
// }
m_pools[result.poolId]->submit(result);
// m_pools[result.poolId]->submit(result);
}
void Network::onLoginSuccess(Client *client)
{
const int id = client->id();
if (id == 0) {
return startDonate();
}
// const int id = client->id();
// if (id == 0) {
// return startDonate();
// }
if (id == 2 && m_pool) { // primary pool is already active
m_pools[2]->disconnect();
return;
}
// if (id == 2 && m_pool) { // primary pool is already active
// m_pools[2]->disconnect();
// return;
// }
LOG_INFO(m_options->colors() ? "\x1B[01;37muse pool: \x1B[01;36m%s:%d" : "use pool: %s:%d", client->host(), client->port());
m_pool = id;
// LOG_INFO(m_options->colors() ? "\x1B[01;37muse pool: \x1B[01;36m%s:%d" : "use pool: %s:%d", client->host(), client->port());
// m_pool = id;
if (m_pool == 1 && m_pools.size() > 2) { // try disconnect from backup pool
m_pools[2]->disconnect();
}
}
void Network::addPool(const Url *url)
{
if (!url) {
return;
}
Client *client = new Client(m_pools.size(), m_agent, this);
client->setUrl(url);
client->setRetryPause(m_options->retryPause() * 1000);
// client->setKeepAlive(m_options->keepAlive());
m_pools.push_back(client);
// if (m_pool == 1 && m_pools.size() > 2) { // try disconnect from backup pool
// m_pools[2]->disconnect();
// }
}
@ -170,48 +151,48 @@ void Network::setJob(Client *client, const Job &job)
void Network::startDonate()
{
if (m_donate) {
if (m_donateActive) {
return;
}
LOG_NOTICE("dev donate started");
m_donate = true;
m_donateActive = true;
}
void Network::stopDonate()
{
if (!m_donate) {
return;
}
// if (!m_donateActive) {
// return;
// }
LOG_NOTICE("dev donate finished");
// LOG_NOTICE("dev donate finished");
m_donate = false;
if (!m_pool) {
return;
}
// m_donateActive = false;
// if (!m_pool) {
// return;
// }
Client *client = m_pools[m_pool];
if (client->isReady()) {
setJob(client, client->job());
}
// Client *client = m_pools[m_pool];
// if (client->isReady()) {
// setJob(client, client->job());
// }
}
void Network::onTimer(uv_timer_t *handle)
{
auto net = static_cast<Network*>(handle->data);
//void Network::onTimer(uv_timer_t *handle)
//{
// auto net = static_cast<Network*>(handle->data);
if (!net->m_donate) {
auto url = std::make_unique<Url>("donate.xmrig.com", net->m_options->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443);
net->m_pools[0]->connect(url.get());
// if (!net->m_donateActive) {
// auto url = std::make_unique<Url>("donate.xmrig.com", net->m_options->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443);
// net->m_pools[0]->connect(url.get());
uv_timer_start(&net->m_timer, Network::onTimer, net->m_options->donateLevel() * 60 * 1000, 0);
return;
}
// uv_timer_start(&net->m_timer, Network::onTimer, net->m_options->donateLevel() * 60 * 1000, 0);
// return;
// }
net->m_pools[0]->disconnect();
uv_timer_start(&net->m_timer, Network::onTimer, (100 - net->m_options->donateLevel()) * 60 * 1000, 0);
}
// net->m_pools[0]->disconnect();
// uv_timer_start(&net->m_timer, Network::onTimer, (100 - net->m_options->donateLevel()) * 60 * 1000, 0);
//}

View file

@ -31,13 +31,15 @@
#include "interfaces/IClientListener.h"
#include "interfaces/IJobResultListener.h"
#include "interfaces/IStrategyListener.h"
class IStrategy;
class Options;
class Url;
class Network : public IClientListener, public IJobResultListener
class Network : public IClientListener, public IJobResultListener, public IStrategyListener
{
public:
Network(const Options *options);
@ -59,15 +61,11 @@ private:
void startDonate();
void stopDonate();
static void onTimer(uv_timer_t *handle);
bool m_donate;
bool m_donateActive;
char *m_agent;
const Options *m_options;
int m_pool;
std::vector<Client*> m_pools;
uint64_t m_diff;
uv_timer_t m_timer;
IStrategy *m_donate;
IStrategy *m_strategy;
};

View file

@ -69,10 +69,10 @@ Url::Url(const char *url) :
Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash) :
m_keepAlive(false),
m_nicehash(false),
m_password(nullptr),
m_user(nullptr),
m_keepAlive(keepAlive),
m_nicehash(nicehash),
m_password(password ? strdup(password) : nullptr),
m_user(user ? strdup(user) : nullptr),
m_port(port)
{
m_host = strdup(host);

View file

@ -0,0 +1,73 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "net/Client.h"
#include "net/strategies/DonateStrategy.h"
#include "Options.h"
DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) :
m_listener(listener)
{
Url *url = new Url("donate.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443, Options::i()->pools().front()->user());
m_client = new Client(-1, agent, this);
m_client->setUrl(url);
m_client->setRetryPause(Options::i()->retryPause() * 1000);
delete url;
m_timer.data = this;
uv_timer_init(uv_default_loop(), &m_timer);
uv_timer_start(&m_timer, DonateStrategy::onTimer, (100 - Options::i()->donateLevel()) * 60 * 1000, 0);
}
void DonateStrategy::connect()
{
}
void DonateStrategy::onClose(Client *client, int failures)
{
}
void DonateStrategy::onJobReceived(Client *client, const Job &job)
{
}
void DonateStrategy::onLoginSuccess(Client *client)
{
}
void DonateStrategy::onTimer(uv_timer_t *handle)
{
}

View file

@ -0,0 +1,61 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __DONATESTRATEGY_H__
#define __DONATESTRATEGY_H__
#include <uv.h>
#include "interfaces/IClientListener.h"
#include "interfaces/IStrategy.h"
class Client;
class IStrategyListener;
class Url;
class DonateStrategy : public IStrategy, public IClientListener
{
public:
DonateStrategy(const char *agent, IStrategyListener *listener);
public:
void connect() override;
protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
private:
static void onTimer(uv_timer_t *handle);
Client *m_client;
IStrategyListener *m_listener;
uv_timer_t m_timer;
};
#endif /* __DONATESTRATEGY_H__ */

View file

@ -0,0 +1,59 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "net/Client.h"
#include "net/strategies/SinglePoolStrategy.h"
#include "Options.h"
SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener) :
m_listener(listener)
{
m_client = new Client(0, agent, this);
m_client->setUrl(url);
m_client->setRetryPause(Options::i()->retryPause() * 1000);
}
void SinglePoolStrategy::connect()
{
m_client->connect();
}
void SinglePoolStrategy::onClose(Client *client, int failures)
{
}
void SinglePoolStrategy::onJobReceived(Client *client, const Job &job)
{
}
void SinglePoolStrategy::onLoginSuccess(Client *client)
{
}

View file

@ -0,0 +1,55 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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>
*
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SINGLEPOOLSTRATEGY_H__
#define __SINGLEPOOLSTRATEGY_H__
#include "interfaces/IClientListener.h"
#include "interfaces/IStrategy.h"
class Client;
class IStrategyListener;
class Url;
class SinglePoolStrategy : public IStrategy, public IClientListener
{
public:
SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener);
public:
void connect() override;
protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
private:
Client *m_client;
IStrategyListener *m_listener;
};
#endif /* __SINGLEPOOLSTRATEGY_H__ */