From ee4f6e28f030b4f98db3cefdf0b71f6e24554a71 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 17 Feb 2019 10:51:32 +0700 Subject: [PATCH] * [WIP] More unification in Pools class. --- src/base/net/Pools.cpp | 78 ++++++++++++++++++++++++- src/base/net/Pools.h | 8 +++ src/common/config/CommonConfig.cpp | 50 ++++------------ src/common/config/CommonConfig.h | 10 +--- src/common/interfaces/IClientListener.h | 6 +- src/common/log/ConsoleLog.cpp | 2 +- src/common/log/Log.cpp | 8 ++- src/common/log/Log.h | 5 +- src/common/net/Tls.h | 8 ++- src/core/Config.cpp | 9 +-- 10 files changed, 118 insertions(+), 66 deletions(-) diff --git a/src/base/net/Pools.cpp b/src/base/net/Pools.cpp index 6ebafdc3..247e13d9 100644 --- a/src/base/net/Pools.cpp +++ b/src/base/net/Pools.cpp @@ -24,10 +24,18 @@ #include "base/net/Pools.h" +#include "common/log/Log.h" +#include "rapidjson/document.h" -xmrig::Pools::Pools() +xmrig::Pools::Pools() : + m_retries(5), + m_retryPause(5) { +# ifdef XMRIG_PROXY_PROJECT + m_retries = 2; + m_retryPause = 1; +# endif } @@ -60,6 +68,21 @@ bool xmrig::Pools::setUrl(const char *url) } +rapidjson::Value xmrig::Pools::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value pools(kArrayType); + + for (const Pool &pool : m_data) { + pools.PushBack(pool.toJSON(doc), allocator); + } + + return pools; +} + + size_t xmrig::Pools::active() const { size_t count = 0; @@ -79,3 +102,56 @@ void xmrig::Pools::adjust(const Algorithm &algorithm) pool.adjust(algorithm); } } + + +void xmrig::Pools::print() +{ + size_t i = 1; + for (const Pool &pool : m_data) { + if (Log::colors) { + const int color = pool.isEnabled() ? (pool.isTLS() ? 32 : 36) : 31; + + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m algo " WHITE_BOLD("%s"), + i, + color, + pool.url(), + pool.algorithm().shortName() + ); + } + else { + Log::i()->text(" * POOL #%-7zu%s%s algo=%s %s", + i, + pool.isEnabled() ? "" : "-", + pool.url(), + pool.algorithm().shortName(), + pool.isTLS() ? "TLS" : "" + ); + } + + i++; + } + +# ifdef APP_DEBUG + LOG_NOTICE("POOLS --------------------------------------------------------------------"); + for (const Pool &pool : m_data) { + pool.print(); + } + LOG_NOTICE("--------------------------------------------------------------------------"); +# endif +} + + +void xmrig::Pools::setRetries(int retries) +{ + if (retries > 0 && retries <= 1000) { + m_retries = retries; + } +} + + +void xmrig::Pools::setRetryPause(int retryPause) +{ + if (retryPause > 0 && retryPause <= 3600) { + m_retryPause = retryPause; + } +} diff --git a/src/base/net/Pools.h b/src/base/net/Pools.h index d882a6b3..0822af07 100644 --- a/src/base/net/Pools.h +++ b/src/base/net/Pools.h @@ -42,6 +42,8 @@ public: inline bool setUserpass(const char *userpass) { return current().setUserpass(userpass); } inline const std::vector &data() const { return m_data; } + inline int retries() const { return m_retries; } + inline int retryPause() const { return m_retryPause; } inline void setFingerprint(const char *fingerprint) { current().setFingerprint(fingerprint); } inline void setKeepAlive(bool enable) { setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); } inline void setKeepAlive(int keepAlive) { current().setKeepAlive(keepAlive); } @@ -54,12 +56,18 @@ public: inline void setVariant(int variant) { current().algorithm().parseVariant(variant); } bool setUrl(const char *url); + rapidjson::Value toJSON(rapidjson::Document &doc) const; size_t active() const; void adjust(const Algorithm &algorithm); + void print(); + void setRetries(int retries); + void setRetryPause(int retryPause); private: Pool ¤t(); + int m_retries; + int m_retryPause; std::vector m_data; }; diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 10a7b03a..7acc58a0 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -71,21 +71,20 @@ xmrig::CommonConfig::CommonConfig() : m_apiRestricted(true), m_autoSave(true), m_background(false), - m_colors(true), m_dryRun(false), m_syslog(false), m_watch(true), m_apiPort(0), m_donateLevel(kDefaultDonateLevel), m_printTime(60), - m_retries(5), - m_retryPause(5), m_state(NoneState) { -# ifdef XMRIG_PROXY_PROJECT - m_retries = 2; - m_retryPause = 1; -# endif +} + + +bool xmrig::CommonConfig::isColors() const +{ + return Log::colors; } @@ -105,32 +104,7 @@ void xmrig::CommonConfig::printAPI() void xmrig::CommonConfig::printPools() { - for (size_t i = 0; i < m_pools.data().size(); ++i) { - if (!isColors()) { - Log::i()->text(" * POOL #%-7zu%s algo=%s, TLS=%d", - i + 1, - m_pools.data()[i].url(), - m_pools.data()[i].algorithm().shortName(), - static_cast(m_pools.data()[i].isTLS()) - ); - } - else { - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m algo " WHITE_BOLD("%s"), - i + 1, - m_pools.data()[i].isTLS() ? 32 : 36, - m_pools.data()[i].url(), - m_pools.data()[i].algorithm().shortName() - ); - } - } - -# ifdef APP_DEBUG - LOG_NOTICE("POOLS --------------------------------------------------------------------"); - for (const Pool &pool : m_activePools) { - pool.print(); - } - LOG_NOTICE("--------------------------------------------------------------------------"); -# endif + m_pools.print(); } @@ -261,7 +235,7 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) # endif case ColorKey: /* --no-color */ - m_colors = enable; + Log::colors = enable; break; case WatchKey: /* watch */ @@ -404,15 +378,11 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) { switch (key) { case RetriesKey: /* --retries */ - if (arg > 0 && arg <= 1000) { - m_retries = arg; - } + m_pools.setRetries(arg); break; case RetryPauseKey: /* --retry-pause */ - if (arg > 0 && arg <= 3600) { - m_retryPause = arg; - } + m_pools.setRetryPause(arg); break; case KeepAliveKey: /* --keepalive */ diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 9c6c32bb..77d4b202 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -44,7 +44,6 @@ public: inline bool isApiRestricted() const { return m_apiRestricted; } inline bool isAutoSave() const { return m_autoSave; } inline bool isBackground() const { return m_background; } - inline bool isColors() const { return m_colors; } inline bool isDryRun() const { return m_dryRun; } inline bool isSyslog() const { return m_syslog; } inline const char *apiId() const { return m_apiId.data(); } @@ -56,9 +55,8 @@ public: 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_retries; } - inline int retryPause() const { return m_retryPause; } - inline void setColors(bool colors) { m_colors = colors; } + 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; } @@ -66,6 +64,7 @@ public: bool save() override; + bool isColors() const; void printAPI(); void printPools(); void printVersions(); @@ -90,15 +89,12 @@ protected: bool m_apiRestricted; bool m_autoSave; bool m_background; - bool m_colors; bool m_dryRun; bool m_syslog; bool m_watch; int m_apiPort; int m_donateLevel; int m_printTime; - int m_retries; - int m_retryPause; Pools m_pools; State m_state; String m_apiId; diff --git a/src/common/interfaces/IClientListener.h b/src/common/interfaces/IClientListener.h index 029d140d..70a3d1d0 100644 --- a/src/common/interfaces/IClientListener.h +++ b/src/common/interfaces/IClientListener.h @@ -29,14 +29,12 @@ #include -class Job; -class SubmitResult; - - namespace xmrig { class Client; +class Job; +class SubmitResult; class IClientListener diff --git a/src/common/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp index 6cf61980..b10812a6 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -45,7 +45,7 @@ ConsoleLog::ConsoleLog(xmrig::Controller *controller) : m_controller(controller) { if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) { - controller->config()->setColors(false); + Log::colors = false; return; } diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 2af90209..07e4f299 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -35,9 +36,10 @@ Log *Log::m_self = nullptr; +bool Log::colors = true; -static const char *colors[5] = { +static const char *color[5] = { "\x1B[0;31m", /* ERR */ "\x1B[0;33m", /* WARNING */ "\x1B[1;37m", /* NOTICE */ @@ -96,7 +98,7 @@ const char *Log::colorByLevel(ILogBackend::Level level, bool isColors) return ""; } - return colors[level]; + return color[level]; } diff --git a/src/common/log/Log.h b/src/common/log/Log.h index aa03a743..c32edddd 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -48,6 +49,8 @@ public: static const char *endl(bool isColors = true); static void defaultInit(); + static bool colors; + private: inline Log() { assert(m_self == nullptr); diff --git a/src/common/net/Tls.h b/src/common/net/Tls.h index 937d255c..cce78eeb 100644 --- a/src/common/net/Tls.h +++ b/src/common/net/Tls.h @@ -32,7 +32,10 @@ #include "common/net/Client.h" -class xmrig::Client::Tls +namespace xmrig { + + +class Client::Tls { public: Tls(Client *client); @@ -60,4 +63,7 @@ private: }; +} /* namespace xmrig */ + + #endif /* XMRIG_CLIENT_TLS_H */ diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 6ff19588..b477c1e5 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -103,14 +103,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("hw-aes", m_aesMode == AES_AUTO ? Value(kNullType) : Value(m_aesMode == AES_HW), allocator); doc.AddMember("log-file", logFile() ? Value(StringRef(logFile())).Move() : Value(kNullType).Move(), allocator); doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator); - - Value pools(kArrayType); - - for (const Pool &pool : m_pools.data()) { - pools.PushBack(pool.toJSON(doc), allocator); - } - - doc.AddMember("pools", pools, 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);