Restore network API.
This commit is contained in:
parent
9c66c9b30f
commit
c9f9e6787c
10 changed files with 145 additions and 67 deletions
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,115 +0,0 @@
|
|||
/* 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 "api/NetworkState.h"
|
||||
#include "base/net/stratum/SubmitResult.h"
|
||||
#include "base/tools/Chrono.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();
|
||||
}
|
|
@ -4,9 +4,7 @@
|
|||
* 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>
|
||||
* 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
|
||||
|
@ -22,48 +20,26 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_NETWORKSTATE_H
|
||||
#define XMRIG_NETWORKSTATE_H
|
||||
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#ifndef XMRIG_IAPILISTENER_H
|
||||
#define XMRIG_IAPILISTENER_H
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class SubmitResult;
|
||||
class IApiRequest;
|
||||
|
||||
|
||||
class NetworkState
|
||||
class IApiListener
|
||||
{
|
||||
public:
|
||||
NetworkState();
|
||||
virtual ~IApiListener() = default;
|
||||
|
||||
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;
|
||||
virtual void onRequest(IApiRequest &request) = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_NETWORKSTATE_H */
|
||||
#endif // XMRIG_IAPILISTENER_H
|
Loading…
Add table
Add a link
Reference in a new issue