Implemented DonateStrategy.

This commit is contained in:
XMRig 2017-06-30 03:20:50 +03:00
parent 25faeabd61
commit 7e17f77c11
8 changed files with 88 additions and 37 deletions

View file

@ -35,6 +35,7 @@ public:
virtual bool isActive() const = 0; virtual bool isActive() const = 0;
virtual void connect() = 0; virtual void connect() = 0;
virtual void resume() = 0;
virtual void submit(const JobResult &result) = 0; virtual void submit(const JobResult &result) = 0;
}; };

View file

@ -38,6 +38,7 @@
Client::Client(int id, const char *agent, IClientListener *listener) : Client::Client(int id, const char *agent, IClientListener *listener) :
m_quiet(false),
m_agent(agent), m_agent(agent),
m_listener(listener), m_listener(listener),
m_id(id), m_id(id),
@ -93,6 +94,7 @@ void Client::connect(const Url *url)
void Client::disconnect() void Client::disconnect()
{ {
uv_timer_stop(&m_retriesTimer);
m_failures = -1; m_failures = -1;
close(); close();
@ -207,10 +209,13 @@ int Client::resolve(const char *host)
setState(HostLookupState); setState(HostLookupState);
m_recvBufPos = 0; m_recvBufPos = 0;
m_failures = 0;
const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, NULL, &m_hints); const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, NULL, &m_hints);
if (r) { if (r) {
LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r)); if (!m_quiet) {
LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r));
}
return 1; return 1;
} }
@ -277,7 +282,9 @@ void Client::parse(char *line, size_t len)
json_t *val = json_loads(line, 0, &err); json_t *val = json_loads(line, 0, &err);
if (!val) { if (!val) {
LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), err.text); if (!m_quiet) {
LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), err.text);
}
return; return;
} }
@ -296,7 +303,9 @@ void Client::parse(char *line, size_t len)
void Client::parseNotification(const char *method, const json_t *params, const json_t *error) void Client::parseNotification(const char *method, const json_t *params, const json_t *error)
{ {
if (json_is_object(error)) { if (json_is_object(error)) {
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code"))); if (!m_quiet) {
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code")));
}
return; return;
} }
@ -321,7 +330,10 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
{ {
if (json_is_object(error)) { if (json_is_object(error)) {
const char *message = json_string_value(json_object_get(error, "message")); const char *message = json_string_value(json_object_get(error, "message"));
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), message, json_integer_value(json_object_get(error, "code")));
if (!m_quiet) {
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), message, json_integer_value(json_object_get(error, "code")));
}
if (id == 1 || (message && strncasecmp(message, "Unauthenticated", 15) == 0)) { if (id == 1 || (message && strncasecmp(message, "Unauthenticated", 15) == 0)) {
close(); close();
@ -337,7 +349,10 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
if (id == 1) { if (id == 1) {
int code = -1; int code = -1;
if (!parseLogin(result, &code)) { if (!parseLogin(result, &code)) {
LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); if (!m_quiet) {
LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code);
}
return close(); return close();
} }
@ -426,7 +441,10 @@ void Client::onConnect(uv_connect_t *req, int status)
{ {
auto client = getClient(req->data); auto client = getClient(req->data);
if (status < 0) { if (status < 0) {
LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); if (!client->m_quiet) {
LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status));
}
free(req); free(req);
client->close(); client->close();
return; return;
@ -447,7 +465,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
{ {
auto client = getClient(stream->data); auto client = getClient(stream->data);
if (nread < 0) { if (nread < 0) {
if (nread != UV_EOF) { if (nread != UV_EOF && !client->m_quiet) {
LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(nread)); LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(nread));
} }

View file

@ -67,6 +67,7 @@ public:
inline int id() const { return m_id; } inline int id() const { return m_id; }
inline SocketState state() const { return m_state; } inline SocketState state() const { return m_state; }
inline uint16_t port() const { return m_url.port(); } inline uint16_t port() const { return m_url.port(); }
inline void setQuiet(bool quiet) { m_quiet = quiet; }
inline void setRetryPause(int ms) { m_retryPause = ms; } inline void setRetryPause(int ms) { m_retryPause = ms; }
private: private:
@ -94,6 +95,7 @@ private:
static Client *getClient(void *data); static Client *getClient(void *data);
bool m_quiet;
char m_rpcId[64]; char m_rpcId[64];
const char *m_agent; const char *m_agent;
IClientListener *m_listener; IClientListener *m_listener;

View file

@ -147,9 +147,14 @@ void Network::onJobResult(const JobResult &result)
void Network::onPause(IStrategy *strategy) void Network::onPause(IStrategy *strategy)
{ {
if ((m_donate && !m_donate->isActive()) || !m_strategy->isActive()) { if (m_donate && m_donate == strategy) {
LOG_NOTICE("dev donate finished");
m_strategy->resume();
}
if (!m_strategy->isActive()) {
LOG_ERR("no active pools, pause mining"); LOG_ERR("no active pools, pause mining");
Workers::pause(); return Workers::pause();
} }
} }
@ -168,18 +173,6 @@ void Network::setJob(Client *client, const Job &job)
} }
void Network::startDonate()
{
if (m_donateActive) {
return;
}
LOG_NOTICE("dev donate started");
m_donateActive = true;
}
void Network::stopDonate() void Network::stopDonate()
{ {
// if (!m_donateActive) { // if (!m_donateActive) {

View file

@ -30,6 +30,8 @@
DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) :
m_active(false), m_active(false),
m_donateTime(Options::i()->donateLevel() * 60 * 1000),
m_idleTime((100 - Options::i()->donateLevel()) * 60 * 1000),
m_listener(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()); Url *url = new Url("donate.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443, Options::i()->pools().front()->user());
@ -37,24 +39,20 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) :
m_client = new Client(-1, agent, this); m_client = new Client(-1, agent, this);
m_client->setUrl(url); m_client->setUrl(url);
m_client->setRetryPause(Options::i()->retryPause() * 1000); m_client->setRetryPause(Options::i()->retryPause() * 1000);
m_client->setQuiet(true);
delete url; delete url;
m_timer.data = this; m_timer.data = this;
uv_timer_init(uv_default_loop(), &m_timer); uv_timer_init(uv_default_loop(), &m_timer);
uv_timer_start(&m_timer, DonateStrategy::onTimer, (100 - Options::i()->donateLevel()) * 60 * 1000, 0); idle();
}
bool DonateStrategy::isActive() const
{
return m_active;
} }
void DonateStrategy::connect() void DonateStrategy::connect()
{ {
m_client->connect();
} }
@ -66,7 +64,6 @@ void DonateStrategy::submit(const JobResult &result)
void DonateStrategy::onClose(Client *client, int failures) void DonateStrategy::onClose(Client *client, int failures)
{ {
} }
@ -78,12 +75,39 @@ void DonateStrategy::onJobReceived(Client *client, const Job &job)
void DonateStrategy::onLoginSuccess(Client *client) void DonateStrategy::onLoginSuccess(Client *client)
{ {
if (!isActive()) {
uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0);
}
m_active = true; m_active = true;
m_listener->onActive(client); m_listener->onActive(client);
} }
void DonateStrategy::idle()
{
uv_timer_start(&m_timer, DonateStrategy::onTimer, m_idleTime, 0);
}
void DonateStrategy::stop()
{
m_client->disconnect();
m_active = false;
m_listener->onPause(this);
idle();
}
void DonateStrategy::onTimer(uv_timer_t *handle) void DonateStrategy::onTimer(uv_timer_t *handle)
{ {
auto strategy = static_cast<DonateStrategy*>(handle->data);
if (!strategy->isActive()) {
return strategy->connect();
}
strategy->stop();
} }

View file

@ -43,7 +43,9 @@ public:
DonateStrategy(const char *agent, IStrategyListener *listener); DonateStrategy(const char *agent, IStrategyListener *listener);
public: public:
bool isActive() const override; inline bool isActive() const override { return m_active; }
inline void resume() override {}
void connect() override; void connect() override;
void submit(const JobResult &result) override; void submit(const JobResult &result) override;
@ -53,10 +55,15 @@ protected:
void onLoginSuccess(Client *client) override; void onLoginSuccess(Client *client) override;
private: private:
void idle();
void stop();
static void onTimer(uv_timer_t *handle); static void onTimer(uv_timer_t *handle);
bool m_active; bool m_active;
Client *m_client; Client *m_client;
const int m_donateTime;
const int m_idleTime;
IStrategyListener *m_listener; IStrategyListener *m_listener;
uv_timer_t m_timer; uv_timer_t m_timer;
}; };

View file

@ -38,18 +38,22 @@ SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrat
} }
bool SinglePoolStrategy::isActive() const
{
return m_active;
}
void SinglePoolStrategy::connect() void SinglePoolStrategy::connect()
{ {
m_client->connect(); m_client->connect();
} }
void SinglePoolStrategy::resume()
{
if (!isActive()) {
return;
}
m_listener->onJob(m_client, m_client->job());
}
void SinglePoolStrategy::submit(const JobResult &result) void SinglePoolStrategy::submit(const JobResult &result)
{ {
m_client->submit(result); m_client->submit(result);

View file

@ -40,8 +40,10 @@ public:
SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener); SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener);
public: public:
bool isActive() const override; inline bool isActive() const override { return m_active; }
void connect() override; void connect() override;
void resume() override;
void submit(const JobResult &result) override; void submit(const JobResult &result) override;
protected: protected: