Removed interface IMinerListener and added Error class.

This commit is contained in:
XMRig
2017-09-07 08:31:44 +03:00
parent 6c1258779e
commit b97a9e2462
8 changed files with 87 additions and 79 deletions

View File

@@ -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

View File

@@ -33,7 +33,6 @@
#include "api/ApiState.h"
#include "Mem.h"
#include "net/Job.h"
#include "Options.h"
#include "Platform.h"

64
src/proxy/Error.cpp Normal file
View File

@@ -0,0 +1,64 @@
/* 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 "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;
}

View File

@@ -21,24 +21,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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__ */

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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<ConnectionEvent*>(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);

View File

@@ -29,10 +29,6 @@
#include <uv.h>
#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;