Restore network API.

This commit is contained in:
XMRig 2019-03-30 03:10:27 +07:00
parent 9c66c9b30f
commit c9f9e6787c
10 changed files with 145 additions and 67 deletions

View file

@ -41,9 +41,16 @@
#include "core/Controller.h"
#include "net/Network.h"
#include "net/strategies/DonateStrategy.h"
#include "rapidjson/document.h"
#include "workers/Workers.h"
#ifdef XMRIG_FEATURE_API
# include "api/Api.h"
# include "api/interfaces/IApiRequest.h"
#endif
xmrig::Network::Network(Controller *controller) :
m_donate(nullptr),
m_timer(nullptr)
@ -51,6 +58,10 @@ xmrig::Network::Network(Controller *controller) :
Workers::setListener(this);
controller->addListener(this);
# ifdef XMRIG_FEATURE_API
controller->api()->addListener(this);
# endif
const Pools &pools = controller->config()->pools();
m_strategy = pools.createStrategy(this);
@ -152,6 +163,19 @@ void xmrig::Network::onPause(IStrategy *strategy)
}
void xmrig::Network::onRequest(IApiRequest &request)
{
# ifdef XMRIG_FEATURE_API
if (request.method() == IApiRequest::METHOD_GET && request.url() == "/1/summary") {
request.accept();
getResults(request.reply(), request.doc());
getConnection(request.reply(), request.doc());
}
# endif
}
void xmrig::Network::onResultAccepted(IStrategy *, Client *, const SubmitResult &result, const char *error)
{
m_state.add(result, error);
@ -196,8 +220,47 @@ void xmrig::Network::tick()
if (m_donate) {
m_donate->tick(now);
}
# ifdef XMRIG_FEATURE_API
//Api::tick(m_state);
# endif
}
#ifdef XMRIG_FEATURE_API
void xmrig::Network::getConnection(rapidjson::Value &reply, rapidjson::Document &doc) const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value connection(kObjectType);
connection.AddMember("pool", StringRef(m_state.pool), allocator);
connection.AddMember("uptime", m_state.connectionTime(), allocator);
connection.AddMember("ping", m_state.latency(), allocator);
connection.AddMember("failures", m_state.failures, allocator);
connection.AddMember("error_log", Value(kArrayType), allocator);
reply.AddMember("connection", connection, allocator);
}
void xmrig::Network::getResults(rapidjson::Value &reply, rapidjson::Document &doc) const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value results(kObjectType);
results.AddMember("diff_current", m_state.diff, allocator);
results.AddMember("shares_good", m_state.accepted, allocator);
results.AddMember("shares_total", m_state.accepted + m_state.rejected, allocator);
results.AddMember("avg_time", m_state.avgTime(), allocator);
results.AddMember("hashes_total", m_state.total, allocator);
Value best(kArrayType);
for (size_t i = 0; i < m_state.topDiff.size(); ++i) {
best.PushBack(m_state.topDiff[i], allocator);
}
results.AddMember("best", best, allocator);
results.AddMember("error_log", Value(kArrayType), allocator);
reply.AddMember("results", results, allocator);
}
#endif

View file

@ -29,11 +29,13 @@
#include <vector>
#include "api/NetworkState.h"
#include "api/interfaces/IApiListener.h"
#include "base/kernel/interfaces/IControllerListener.h"
#include "base/kernel/interfaces/IStrategyListener.h"
#include "base/kernel/interfaces/ITimerListener.h"
#include "interfaces/IJobResultListener.h"
#include "net/NetworkState.h"
#include "rapidjson/fwd.h"
namespace xmrig {
@ -43,7 +45,7 @@ class Controller;
class IStrategy;
class Network : public IJobResultListener, public IStrategyListener, public IControllerListener, public ITimerListener
class Network : public IJobResultListener, public IStrategyListener, public IControllerListener, public ITimerListener, public IApiListener
{
public:
Network(Controller *controller);
@ -61,6 +63,7 @@ protected:
void onJob(IStrategy *strategy, Client *client, const Job &job) override;
void onJobResult(const JobResult &result) override;
void onPause(IStrategy *strategy) override;
void onRequest(IApiRequest &request) override;
void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override;
private:
@ -69,6 +72,11 @@ private:
void setJob(Client *client, const Job &job, bool donate);
void tick();
# ifdef XMRIG_FEATURE_API
void getConnection(rapidjson::Value &reply, rapidjson::Document &doc) const;
void getResults(rapidjson::Value &reply, rapidjson::Document &doc) const;
# endif
IStrategy *m_donate;
IStrategy *m_strategy;
NetworkState m_state;

115
src/net/NetworkState.cpp Normal file
View file

@ -0,0 +1,115 @@
/* 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 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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 <algorithm>
#include <stdio.h>
#include <string.h>
#include <uv.h>
#include "base/net/stratum/SubmitResult.h"
#include "base/tools/Chrono.h"
#include "net/NetworkState.h"
xmrig::NetworkState::NetworkState() :
diff(0),
accepted(0),
failures(0),
rejected(0),
total(0),
m_active(false)
{
memset(pool, 0, sizeof(pool));
}
uint32_t xmrig::NetworkState::avgTime() const
{
if (m_latency.empty()) {
return 0;
}
return connectionTime() / (uint32_t)m_latency.size();
}
uint32_t xmrig::NetworkState::latency() const
{
const size_t calls = m_latency.size();
if (calls == 0) {
return 0;
}
auto v = m_latency;
std::nth_element(v.begin(), v.begin() + calls / 2, v.end());
return v[calls / 2];
}
uint64_t xmrig::NetworkState::connectionTime() const
{
return m_active ? ((Chrono::steadyMSecs() - m_connectionTime) / 1000) : 0;
}
void xmrig::NetworkState::add(const SubmitResult &result, const char *error)
{
if (error) {
rejected++;
return;
}
accepted++;
total += result.diff;
const size_t ln = topDiff.size() - 1;
if (result.actualDiff > topDiff[ln]) {
topDiff[ln] = result.actualDiff;
std::sort(topDiff.rbegin(), topDiff.rend());
}
m_latency.push_back(result.elapsed > 0xFFFF ? 0xFFFF : (uint16_t) result.elapsed);
}
void xmrig::NetworkState::setPool(const char *host, int port, const char *ip)
{
snprintf(pool, sizeof(pool) - 1, "%s:%d", host, port);
m_active = true;
m_connectionTime = Chrono::steadyMSecs();
}
void xmrig::NetworkState::stop()
{
m_active = false;
diff = 0;
failures++;
m_latency.clear();
}

69
src/net/NetworkState.h Normal file
View file

@ -0,0 +1,69 @@
/* 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 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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 XMRIG_NETWORKSTATE_H
#define XMRIG_NETWORKSTATE_H
#include <array>
#include <vector>
namespace xmrig {
class SubmitResult;
class NetworkState
{
public:
NetworkState();
uint32_t avgTime() const;
uint32_t latency() const;
uint64_t connectionTime() const;
void add(const SubmitResult &result, const char *error);
void setPool(const char *host, int port, const char *ip);
void stop();
char pool[256];
std::array<uint64_t, 10> topDiff { { } };
uint32_t diff;
uint64_t accepted;
uint64_t failures;
uint64_t rejected;
uint64_t total;
private:
bool m_active;
std::vector<uint16_t> m_latency;
uint64_t m_connectionTime;
};
} /* namespace xmrig */
#endif /* XMRIG_NETWORKSTATE_H */