mirror of
https://github.com/xmrig/xmrig.git
synced 2024-12-22 19:49:36 +00:00
Restore network API.
This commit is contained in:
parent
9c66c9b30f
commit
c9f9e6787c
10 changed files with 145 additions and 67 deletions
|
@ -22,7 +22,7 @@ include (src/base/base.cmake)
|
|||
set(HEADERS
|
||||
"${HEADERS_BASE}"
|
||||
"${HEADERS_BASE_HTTP}"
|
||||
src/api/NetworkState.h
|
||||
src/api/interfaces/IApiListener.h
|
||||
src/App.h
|
||||
src/common/config/CommonConfig.h
|
||||
src/common/config/ConfigLoader.h
|
||||
|
@ -45,13 +45,14 @@ set(HEADERS
|
|||
src/Mem.h
|
||||
src/net/JobResult.h
|
||||
src/net/Network.h
|
||||
src/net/NetworkState.h
|
||||
src/net/strategies/DonateStrategy.h
|
||||
src/Summary.h
|
||||
src/version.h
|
||||
src/workers/CpuThread.h
|
||||
src/workers/ThreadHandle.h
|
||||
src/workers/Hashrate.h
|
||||
src/workers/MultiWorker.h
|
||||
src/workers/ThreadHandle.h
|
||||
src/workers/Worker.h
|
||||
src/workers/Workers.h
|
||||
)
|
||||
|
@ -81,7 +82,6 @@ endif()
|
|||
set(SOURCES
|
||||
"${SOURCES_BASE}"
|
||||
"${SOURCES_BASE_HTTP}"
|
||||
src/api/NetworkState.cpp
|
||||
src/App.cpp
|
||||
src/common/config/CommonConfig.cpp
|
||||
src/common/config/ConfigLoader.cpp
|
||||
|
@ -93,12 +93,13 @@ set(SOURCES
|
|||
src/core/Controller.cpp
|
||||
src/Mem.cpp
|
||||
src/net/Network.cpp
|
||||
src/net/NetworkState.cpp
|
||||
src/net/strategies/DonateStrategy.cpp
|
||||
src/Summary.cpp
|
||||
src/workers/CpuThread.cpp
|
||||
src/workers/ThreadHandle.cpp
|
||||
src/workers/Hashrate.cpp
|
||||
src/workers/MultiWorker.cpp
|
||||
src/workers/ThreadHandle.cpp
|
||||
src/workers/Worker.cpp
|
||||
src/workers/Workers.cpp
|
||||
src/xmrig.cpp
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
#include "3rdparty/http-parser/http_parser.h"
|
||||
#include "api/Api.h"
|
||||
#include "api/interfaces/IApiListener.h"
|
||||
#include "api/requests/HttpApiRequest.h"
|
||||
#include "base/tools/Buffer.h"
|
||||
#include "common/crypto/keccak.h"
|
||||
|
@ -102,9 +103,15 @@ void xmrig::Api::onConfigChanged(Config *config, Config *previousConfig)
|
|||
|
||||
void xmrig::Api::exec(IApiRequest &request)
|
||||
{
|
||||
if (request.isNew()) {
|
||||
request.done(HTTP_STATUS_NOT_FOUND);
|
||||
for (IApiListener *listener : m_listeners) {
|
||||
listener->onRequest(request);
|
||||
|
||||
if (request.isDone()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
request.done(request.isNew() ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,9 @@
|
|||
#define XMRIG_API_H
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
#include "base/kernel/interfaces/IControllerListener.h"
|
||||
|
||||
|
||||
|
@ -35,6 +38,7 @@ namespace xmrig {
|
|||
class Controller;
|
||||
class Httpd;
|
||||
class HttpRequest;
|
||||
class IApiListener;
|
||||
class IApiRequest;
|
||||
class String;
|
||||
|
||||
|
@ -45,8 +49,9 @@ public:
|
|||
Api(Controller *controller);
|
||||
~Api() override;
|
||||
|
||||
inline const char *id() const { return m_id; }
|
||||
inline const char *workerId() const { return m_workerId; }
|
||||
inline const char *id() const { return m_id; }
|
||||
inline const char *workerId() const { return m_workerId; }
|
||||
inline void addListener(IApiListener *listener) { m_listeners.push_back(listener); }
|
||||
|
||||
void request(const HttpRequest &req);
|
||||
void start();
|
||||
|
@ -64,6 +69,7 @@ private:
|
|||
char m_workerId[128];
|
||||
Controller *m_controller;
|
||||
Httpd *m_httpd;
|
||||
std::vector<IApiListener *> m_listeners;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -102,8 +102,6 @@ void ApiRouter::ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply &
|
|||
getIdentify(doc);
|
||||
getMiner(doc);
|
||||
getHashrate(doc);
|
||||
getResults(doc);
|
||||
getConnection(doc);
|
||||
|
||||
return finalize(reply, doc);
|
||||
}
|
||||
|
@ -120,12 +118,6 @@ void ApiRouter::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply)
|
|||
}
|
||||
|
||||
|
||||
void ApiRouter::tick(const xmrig::NetworkState &network)
|
||||
{
|
||||
m_network = network;
|
||||
}
|
||||
|
||||
|
||||
void ApiRouter::onConfigChanged(xmrig::Config *config, xmrig::Config *previousConfig)
|
||||
{
|
||||
updateWorkerId(config->apiWorkerId(), previousConfig->apiWorkerId());
|
||||
|
@ -185,21 +177,6 @@ void ApiRouter::genId(const char *id)
|
|||
}
|
||||
|
||||
|
||||
void ApiRouter::getConnection(rapidjson::Document &doc) const
|
||||
{
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
||||
rapidjson::Value connection(rapidjson::kObjectType);
|
||||
connection.AddMember("pool", rapidjson::StringRef(m_network.pool), allocator);
|
||||
connection.AddMember("uptime", m_network.connectionTime(), allocator);
|
||||
connection.AddMember("ping", m_network.latency(), allocator);
|
||||
connection.AddMember("failures", m_network.failures, allocator);
|
||||
connection.AddMember("error_log", rapidjson::Value(rapidjson::kArrayType), allocator);
|
||||
|
||||
doc.AddMember("connection", connection, allocator);
|
||||
}
|
||||
|
||||
|
||||
void ApiRouter::getHashrate(rapidjson::Document &doc) const
|
||||
{
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
@ -258,30 +235,6 @@ void ApiRouter::getMiner(rapidjson::Document &doc) const
|
|||
}
|
||||
|
||||
|
||||
void ApiRouter::getResults(rapidjson::Document &doc) const
|
||||
{
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
||||
rapidjson::Value results(rapidjson::kObjectType);
|
||||
|
||||
results.AddMember("diff_current", m_network.diff, allocator);
|
||||
results.AddMember("shares_good", m_network.accepted, allocator);
|
||||
results.AddMember("shares_total", m_network.accepted + m_network.rejected, allocator);
|
||||
results.AddMember("avg_time", m_network.avgTime(), allocator);
|
||||
results.AddMember("hashes_total", m_network.total, allocator);
|
||||
|
||||
rapidjson::Value best(rapidjson::kArrayType);
|
||||
for (size_t i = 0; i < m_network.topDiff.size(); ++i) {
|
||||
best.PushBack(m_network.topDiff[i], allocator);
|
||||
}
|
||||
|
||||
results.AddMember("best", best, allocator);
|
||||
results.AddMember("error_log", rapidjson::Value(rapidjson::kArrayType), allocator);
|
||||
|
||||
doc.AddMember("results", results, allocator);
|
||||
}
|
||||
|
||||
|
||||
void ApiRouter::getThreads(rapidjson::Document &doc) const
|
||||
{
|
||||
doc.SetObject();
|
||||
|
|
|
@ -50,26 +50,21 @@ public:
|
|||
void get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const;
|
||||
void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply);
|
||||
|
||||
void tick(const xmrig::NetworkState &results);
|
||||
|
||||
protected:
|
||||
void onConfigChanged(xmrig::Config *config, xmrig::Config *previousConfig) override;
|
||||
|
||||
private:
|
||||
void finalize(xmrig::HttpReply &reply, rapidjson::Document &doc) const;
|
||||
void genId(const char *id);
|
||||
void getConnection(rapidjson::Document &doc) const;
|
||||
void getHashrate(rapidjson::Document &doc) const;
|
||||
void getIdentify(rapidjson::Document &doc) const;
|
||||
void getMiner(rapidjson::Document &doc) const;
|
||||
void getResults(rapidjson::Document &doc) const;
|
||||
void getThreads(rapidjson::Document &doc) const;
|
||||
void setWorkerId(const char *id);
|
||||
void updateWorkerId(const char *id, const char *previousId);
|
||||
|
||||
char m_id[32];
|
||||
char m_workerId[128];
|
||||
xmrig::NetworkState m_network;
|
||||
xmrig::Controller *m_controller;
|
||||
};
|
||||
|
||||
|
|
45
src/api/interfaces/IApiListener.h
Normal file
45
src/api/interfaces/IApiListener.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/* 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-2018 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_IAPILISTENER_H
|
||||
#define XMRIG_IAPILISTENER_H
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class IApiRequest;
|
||||
|
||||
|
||||
class IApiListener
|
||||
{
|
||||
public:
|
||||
virtual ~IApiListener() = default;
|
||||
|
||||
virtual void onRequest(IApiRequest &request) = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif // XMRIG_IAPILISTENER_H
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -29,9 +29,9 @@
|
|||
#include <uv.h>
|
||||
|
||||
|
||||
#include "api/NetworkState.h"
|
||||
#include "base/net/stratum/SubmitResult.h"
|
||||
#include "base/tools/Chrono.h"
|
||||
#include "net/NetworkState.h"
|
||||
|
||||
|
||||
xmrig::NetworkState::NetworkState() :
|
Loading…
Reference in a new issue