From b97a9e24620099727ba0b86de83fb83cf293c17e Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 7 Sep 2017 08:31:44 +0300 Subject: [PATCH] Removed interface IMinerListener and added Error class. --- CMakeLists.txt | 3 +- src/api/ApiState.cpp | 1 - src/proxy/Error.cpp | 64 +++++++++++++++++++ .../IMinerListener.h => proxy/Error.h} | 27 ++++---- src/proxy/Miner.cpp | 15 ++--- src/proxy/Miner.h | 2 - src/proxy/Proxy.cpp | 41 ------------ src/proxy/Proxy.h | 13 +--- 8 files changed, 87 insertions(+), 79 deletions(-) create mode 100644 src/proxy/Error.cpp rename src/{interfaces/IMinerListener.h => proxy/Error.h} (72%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 294a330..72c2de0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,6 @@ set(HEADERS src/interfaces/IEvent.h src/interfaces/IEventListener.h src/interfaces/ILogBackend.h - src/interfaces/IMinerListener.h src/interfaces/IStrategy.h src/interfaces/IStrategyListener.h src/log/ConsoleLog.h @@ -36,6 +35,7 @@ set(HEADERS src/Platform.h src/proxy/Addr.h src/proxy/Counters.h + src/proxy/Error.h src/proxy/Events.h src/proxy/events/CloseEvent.h src/proxy/events/ConnectionEvent.h @@ -79,6 +79,7 @@ set(SOURCES src/Options.cpp src/Platform.cpp src/proxy/Counters.cpp + src/proxy/Error.cpp src/proxy/Events.cpp src/proxy/events/ConnectionEvent.h src/proxy/events/Event.cpp diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp index 2ce4d40..14ab247 100644 --- a/src/api/ApiState.cpp +++ b/src/api/ApiState.cpp @@ -33,7 +33,6 @@ #include "api/ApiState.h" -#include "Mem.h" #include "net/Job.h" #include "Options.h" #include "Platform.h" diff --git a/src/proxy/Error.cpp b/src/proxy/Error.cpp new file mode 100644 index 0000000..a28c09b --- /dev/null +++ b/src/proxy/Error.cpp @@ -0,0 +1,64 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 + * 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 . + */ + + +#include "proxy/Error.h" + + +static const char *kBadGateway = "Bad gateway"; +static const char *kInvalidJobId = "Invalid job id"; +static const char *kInvalidMethod = "Invalid method"; +static const char *kInvalidNonce = "Invalid nonce; is miner not compatible with NiceHash?"; +static const char *kLowDifficulty = "Low difficulty share"; +static const char *kUnauthenticated = "Unauthenticated"; +static const char *kUnknownError = "Unknown error"; + + +const char *Error::toString(Type type) +{ + switch (type) + { + case BadGateway: + return kBadGateway; + + case InvalidJobId: + return kInvalidJobId; + + case InvalidMethod: + return kInvalidMethod; + + case InvalidNonce: + return kInvalidNonce; + + case LowDifficulty: + return kLowDifficulty; + + case Unauthenticated: + return kUnauthenticated; + + default: + break; + } + + return kUnknownError; +} diff --git a/src/interfaces/IMinerListener.h b/src/proxy/Error.h similarity index 72% rename from src/interfaces/IMinerListener.h rename to src/proxy/Error.h index 7092b2d..dfceda2 100644 --- a/src/interfaces/IMinerListener.h +++ b/src/proxy/Error.h @@ -21,24 +21,23 @@ * along with this program. If not, see . */ -#ifndef __IMINERLISTENER_H__ -#define __IMINERLISTENER_H__ +#ifndef __ERROR_H__ +#define __ERROR_H__ -class JobResult; -class LoginRequest; -class Miner; - - -class IMinerListener +class Error { public: - virtual ~IMinerListener() {} + enum Type { + BadGateway, + InvalidJobId, + InvalidMethod, + InvalidNonce, + LowDifficulty, + Unauthenticated + }; - virtual void onMinerClose(Miner *miner) = 0; - virtual void onMinerLogin(Miner *miner, const LoginRequest &request) = 0; - virtual void onMinerSubmit(Miner *miner, const JobResult &request) = 0; + static const char *toString(Type type); }; - -#endif // __IMINERLISTENER_H__ +#endif /* __ERROR_H__ */ diff --git a/src/proxy/Miner.cpp b/src/proxy/Miner.cpp index f753e58..255f276 100644 --- a/src/proxy/Miner.cpp +++ b/src/proxy/Miner.cpp @@ -26,9 +26,11 @@ #include "Counters.h" -#include "interfaces/IMinerListener.h" #include "log/Log.h" #include "net/Job.h" +#include "proxy/events/CloseEvent.h" +#include "proxy/events/LoginEvent.h" +#include "proxy/events/SubmitEvent.h" #include "proxy/JobResult.h" #include "proxy/LoginRequest.h" #include "proxy/Miner.h" @@ -39,7 +41,6 @@ static int64_t nextId = 0; Miner::Miner() : - m_listener(nullptr), m_id(++nextId), m_loginId(0), m_recvBufPos(0), @@ -166,7 +167,7 @@ void Miner::success(int64_t id, const char *status) bool Miner::parseRequest(int64_t id, const char *method, const json_t *params) { - if (!method || !json_is_object(params) || !m_listener) { + if (!method || !json_is_object(params)) { return false; } @@ -176,7 +177,7 @@ bool Miner::parseRequest(int64_t id, const char *method, const json_t *params) LoginRequest request(id, json_string_value(json_object_get(params, "login")), json_string_value(json_object_get(params, "pass")), json_string_value(json_object_get(params, "agent"))); m_loginId = id; - m_listener->onMinerLogin(this, request); + LoginEvent::start(this, request); return true; } @@ -207,7 +208,7 @@ bool Miner::parseRequest(int64_t id, const char *method, const json_t *params) return false; } - m_listener->onMinerSubmit(this, request); + SubmitEvent::start(this, request); return true; } @@ -297,9 +298,7 @@ void Miner::shutdown(bool had_error) delete req; }); - if (m_listener) { - m_listener->onMinerClose(this); - } + CloseEvent::start(this); } diff --git a/src/proxy/Miner.h b/src/proxy/Miner.h index b40f45d..c6912f5 100644 --- a/src/proxy/Miner.h +++ b/src/proxy/Miner.h @@ -63,7 +63,6 @@ public: inline uint8_t fixedByte() const { return m_fixedByte; } inline void close() { shutdown(true); } inline void setFixedByte(uint8_t fixedByte) { m_fixedByte = fixedByte; } - inline void setListener(IMinerListener *listener) { m_listener = listener; } inline void setMapperId(ssize_t mapperId) { m_mapperId = mapperId; } inline void setRealmId(uint32_t realmId) { m_realmId = realmId; } @@ -86,7 +85,6 @@ private: char m_ip[17]; char m_rpcId[37]; - IMinerListener *m_listener; int64_t m_id; int64_t m_loginId; size_t m_recvBufPos; diff --git a/src/proxy/Proxy.cpp b/src/proxy/Proxy.cpp index 6fae77d..3983b84 100644 --- a/src/proxy/Proxy.cpp +++ b/src/proxy/Proxy.cpp @@ -35,10 +35,7 @@ #include "Options.h" #include "Platform.h" #include "proxy/Events.h" -#include "proxy/events/CloseEvent.h" #include "proxy/events/ConnectionEvent.h" -#include "proxy/events/LoginEvent.h" -#include "proxy/events/SubmitEvent.h" #include "proxy/Miner.h" #include "proxy/Miners.h" #include "proxy/Proxy.h" @@ -57,7 +54,6 @@ Proxy::Proxy(const Options *options) m_timer.data = this; uv_timer_init(uv_default_loop(), &m_timer); - Events::subscribe(IEvent::ConnectionType, this); Events::subscribe(IEvent::ConnectionType, m_miners); Events::subscribe(IEvent::CloseType, m_miners); @@ -120,43 +116,6 @@ void Proxy::printState() #endif -void Proxy::onEvent(IEvent *event) -{ - switch (event->type()) - { - case IEvent::ConnectionType: - static_cast(event)->miner()->setListener(this); - break; - - default: - break; - } -} - - -void Proxy::onMinerClose(Miner *miner) -{ - CloseEvent::start(miner); -} - - -void Proxy::onMinerLogin(Miner *miner, const LoginRequest &request) -{ - LoginEvent::start(miner, request); -} - - -void Proxy::onMinerSubmit(Miner *miner, const JobResult &request) -{ - SubmitEvent::start(miner, request); -} - - -void Proxy::onRejectedEvent(IEvent *event) -{ -} - - void Proxy::bind(const char *ip, uint16_t port) { auto server = new Server(ip, port); diff --git a/src/proxy/Proxy.h b/src/proxy/Proxy.h index 4db694d..ae69834 100644 --- a/src/proxy/Proxy.h +++ b/src/proxy/Proxy.h @@ -29,10 +29,6 @@ #include -#include "interfaces/IEventListener.h" -#include "interfaces/IMinerListener.h" - - class Miners; class NonceSplitter; class Options; @@ -41,7 +37,7 @@ class Server; class Url; -class Proxy : public IMinerListener, public IEventListener +class Proxy { public: Proxy(const Options *options); @@ -56,13 +52,6 @@ public: void printState(); # endif -protected: - void onEvent(IEvent *event) override; - void onMinerClose(Miner *miner) override; - void onMinerLogin(Miner *miner, const LoginRequest &request) override; - void onMinerSubmit(Miner *miner, const JobResult &request) override; - void onRejectedEvent(IEvent *event) override; - private: constexpr static int kTickInterval = 60 * 1000;