libjansson replaced to rapidjson.
Sync changes with proxy.
This commit is contained in:
parent
4cf3bb9930
commit
af51513614
78 changed files with 15550 additions and 6420 deletions
|
@ -29,7 +29,6 @@
|
|||
|
||||
|
||||
ApiState *Api::m_state = nullptr;
|
||||
char Api::m_buf[4096];
|
||||
uv_mutex_t Api::m_mutex;
|
||||
|
||||
|
||||
|
@ -48,26 +47,17 @@ void Api::release()
|
|||
}
|
||||
|
||||
|
||||
const char *Api::get(const char *url, size_t *size, int *status)
|
||||
char *Api::get(const char *url, int *status)
|
||||
{
|
||||
if (!m_state) {
|
||||
*size = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uv_mutex_lock(&m_mutex);
|
||||
|
||||
const char *buf = m_state->get(url, size);
|
||||
if (*size) {
|
||||
memcpy(m_buf, buf, *size);
|
||||
}
|
||||
else {
|
||||
*status = 500;
|
||||
}
|
||||
|
||||
char *buf = m_state->get(url, status);
|
||||
uv_mutex_unlock(&m_mutex);
|
||||
|
||||
return m_buf;
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -39,13 +39,12 @@ public:
|
|||
static bool start();
|
||||
static void release();
|
||||
|
||||
static const char *get(const char *url, size_t *size, int *status);
|
||||
static char *get(const char *url, int *status);
|
||||
static void tick(const Hashrate *hashrate);
|
||||
static void tick(const NetworkState &results);
|
||||
|
||||
private:
|
||||
static ApiState *m_state;
|
||||
static char m_buf[4096];
|
||||
static uv_mutex_t m_mutex;
|
||||
};
|
||||
|
||||
|
|
|
@ -38,6 +38,9 @@
|
|||
#include "net/Job.h"
|
||||
#include "Options.h"
|
||||
#include "Platform.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/prettywriter.h"
|
||||
#include "version.h"
|
||||
#include "workers/Hashrate.h"
|
||||
|
||||
|
@ -83,17 +86,18 @@ ApiState::~ApiState()
|
|||
}
|
||||
|
||||
|
||||
const char *ApiState::get(const char *url, size_t *size) const
|
||||
char *ApiState::get(const char *url, int *status) const
|
||||
{
|
||||
json_t *reply = json_object();
|
||||
rapidjson::Document doc;
|
||||
doc.SetObject();
|
||||
|
||||
getIdentify(reply);
|
||||
getMiner(reply);
|
||||
getHashrate(reply);
|
||||
getResults(reply);
|
||||
getConnection(reply);
|
||||
getIdentify(doc);
|
||||
getMiner(doc);
|
||||
getHashrate(doc);
|
||||
getResults(doc);
|
||||
getConnection(doc);
|
||||
|
||||
return finalize(reply, size);
|
||||
return finalize(doc);
|
||||
}
|
||||
|
||||
|
||||
|
@ -118,12 +122,14 @@ void ApiState::tick(const NetworkState &network)
|
|||
}
|
||||
|
||||
|
||||
const char *ApiState::finalize(json_t *reply, size_t *size) const
|
||||
char *ApiState::finalize(rapidjson::Document &doc) const
|
||||
{
|
||||
*size = json_dumpb(reply, m_buf, sizeof(m_buf) - 1, JSON_INDENT(4) | JSON_REAL_PRECISION(15));
|
||||
rapidjson::StringBuffer buffer(0, 4096);
|
||||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
||||
writer.SetMaxDecimalPlaces(10);
|
||||
doc.Accept(writer);
|
||||
|
||||
json_decref(reply);
|
||||
return m_buf;
|
||||
return strdup(buffer.GetString());
|
||||
}
|
||||
|
||||
|
||||
|
@ -150,6 +156,8 @@ void ApiState::genId()
|
|||
|
||||
keccak(input, static_cast<int>(inSize), hash, sizeof(hash));
|
||||
Job::toHex(hash, 8, m_id);
|
||||
|
||||
delete [] input;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -158,85 +166,95 @@ void ApiState::genId()
|
|||
}
|
||||
|
||||
|
||||
void ApiState::getConnection(json_t *reply) const
|
||||
void ApiState::getConnection(rapidjson::Document &doc) const
|
||||
{
|
||||
json_t *connection = json_object();
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
||||
json_object_set(reply, "connection", connection);
|
||||
json_object_set(connection, "pool", json_string(m_network.pool));
|
||||
json_object_set(connection, "uptime", json_integer(m_network.connectionTime()));
|
||||
json_object_set(connection, "ping", json_integer(m_network.latency()));
|
||||
json_object_set(connection, "failures", json_integer(m_network.failures));
|
||||
json_object_set(connection, "error_log", json_array());
|
||||
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 ApiState::getHashrate(json_t *reply) const
|
||||
void ApiState::getHashrate(rapidjson::Document &doc) const
|
||||
{
|
||||
json_t *hashrate = json_object();
|
||||
json_t *threads = json_array();
|
||||
json_t *total = json_array();
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
||||
json_object_set(reply, "hashrate", hashrate);
|
||||
json_object_set(hashrate, "total", total);
|
||||
json_object_set(hashrate, "highest", json_real(normalize(m_highestHashrate)));
|
||||
json_object_set(hashrate, "threads", threads);
|
||||
|
||||
for (int i = 0; i < m_threads * 3; i += 3) {
|
||||
json_t *thread = json_array();
|
||||
json_array_append(thread, json_real(normalize(m_hashrate[i])));
|
||||
json_array_append(thread, json_real(normalize(m_hashrate[i + 1])));
|
||||
json_array_append(thread, json_real(normalize(m_hashrate[i + 2])));
|
||||
|
||||
json_array_append(threads, thread);
|
||||
}
|
||||
rapidjson::Value hashrate(rapidjson::kObjectType);
|
||||
rapidjson::Value total(rapidjson::kArrayType);
|
||||
rapidjson::Value threads(rapidjson::kArrayType);
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
json_array_append(total, json_real(normalize(m_totalHashrate[i])));
|
||||
total.PushBack(normalize(m_totalHashrate[i]), allocator);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_threads * 3; i += 3) {
|
||||
rapidjson::Value thread(rapidjson::kArrayType);
|
||||
thread.PushBack(normalize(m_hashrate[i]), allocator);
|
||||
thread.PushBack(normalize(m_hashrate[i + 1]), allocator);
|
||||
thread.PushBack(normalize(m_hashrate[i + 2]), allocator);
|
||||
|
||||
threads.PushBack(thread, allocator);
|
||||
}
|
||||
|
||||
hashrate.AddMember("total", total, allocator);
|
||||
hashrate.AddMember("highest", normalize(m_highestHashrate), allocator);
|
||||
hashrate.AddMember("threads", threads, allocator);
|
||||
doc.AddMember("hashrate", hashrate, allocator);
|
||||
}
|
||||
|
||||
|
||||
void ApiState::getIdentify(json_t *reply) const
|
||||
void ApiState::getIdentify(rapidjson::Document &doc) const
|
||||
{
|
||||
json_object_set(reply, "id", json_string(m_id));
|
||||
json_object_set(reply, "worker_id", json_string(m_workerId));
|
||||
doc.AddMember("id", rapidjson::StringRef(m_id), doc.GetAllocator());
|
||||
doc.AddMember("worker_id", rapidjson::StringRef(m_workerId), doc.GetAllocator());
|
||||
}
|
||||
|
||||
|
||||
void ApiState::getMiner(json_t *reply) const
|
||||
void ApiState::getMiner(rapidjson::Document &doc) const
|
||||
{
|
||||
json_t *cpu = json_object();
|
||||
json_object_set(reply, "version", json_string(APP_VERSION));
|
||||
json_object_set(reply, "kind", json_string(APP_KIND));
|
||||
json_object_set(reply, "ua", json_string(Platform::userAgent()));
|
||||
json_object_set(reply, "cpu", cpu);
|
||||
json_object_set(reply, "algo", json_string(Options::i()->algoName()));
|
||||
json_object_set(reply, "hugepages", json_boolean(Mem::isHugepagesEnabled()));
|
||||
json_object_set(reply, "donate", json_integer(Options::i()->donateLevel()));
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
||||
json_object_set(cpu, "brand", json_string(Cpu::brand()));
|
||||
json_object_set(cpu, "aes", json_boolean(Cpu::hasAES()));
|
||||
json_object_set(cpu, "x64", json_boolean(Cpu::isX64()));
|
||||
json_object_set(cpu, "sockets", json_integer(Cpu::sockets()));
|
||||
rapidjson::Value cpu(rapidjson::kObjectType);
|
||||
cpu.AddMember("brand", rapidjson::StringRef(Cpu::brand()), allocator);
|
||||
cpu.AddMember("aes", Cpu::hasAES(), allocator);
|
||||
cpu.AddMember("x64", Cpu::isX64(), allocator);
|
||||
cpu.AddMember("sockets", Cpu::sockets(), allocator);
|
||||
|
||||
doc.AddMember("version", APP_VERSION, allocator);
|
||||
doc.AddMember("kind", APP_KIND, allocator);
|
||||
doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator);
|
||||
doc.AddMember("cpu", cpu, allocator);
|
||||
doc.AddMember("algo", rapidjson::StringRef(Options::i()->algoName()), allocator);
|
||||
doc.AddMember("hugepages", Mem::isHugepagesEnabled(), allocator);
|
||||
doc.AddMember("donate_level", Options::i()->donateLevel(), allocator);
|
||||
}
|
||||
|
||||
|
||||
void ApiState::getResults(json_t *reply) const
|
||||
void ApiState::getResults(rapidjson::Document &doc) const
|
||||
{
|
||||
json_t *results = json_object();
|
||||
json_t *best = json_array();
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
||||
json_object_set(reply, "results", results);
|
||||
json_object_set(results, "diff_current", json_integer(m_network.diff));
|
||||
json_object_set(results, "shares_good", json_integer(m_network.accepted));
|
||||
json_object_set(results, "shares_total", json_integer(m_network.accepted + m_network.rejected));
|
||||
json_object_set(results, "avg_time", json_integer(m_network.avgTime()));
|
||||
json_object_set(results, "hashes_total", json_integer(m_network.total));
|
||||
json_object_set(results, "best", best);
|
||||
json_object_set(results, "error_log", json_array());
|
||||
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) {
|
||||
json_array_append(best, json_integer(m_network.topDiff[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);
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
|
||||
|
||||
#include "api/NetworkState.h"
|
||||
#include "jansson.h"
|
||||
#include "rapidjson/fwd.h"
|
||||
|
||||
|
||||
class Hashrate;
|
||||
|
@ -38,18 +38,18 @@ public:
|
|||
ApiState();
|
||||
~ApiState();
|
||||
|
||||
const char *get(const char *url, size_t *size) const;
|
||||
char *get(const char *url, int *status) const;
|
||||
void tick(const Hashrate *hashrate);
|
||||
void tick(const NetworkState &results);
|
||||
|
||||
private:
|
||||
const char *finalize(json_t *reply, size_t *size) const;
|
||||
char *finalize(rapidjson::Document &doc) const;
|
||||
void genId();
|
||||
void getConnection(json_t *reply) const;
|
||||
void getHashrate(json_t *reply) const;
|
||||
void getIdentify(json_t *reply) const;
|
||||
void getMiner(json_t *reply) const;
|
||||
void getResults(json_t *reply) const;
|
||||
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;
|
||||
|
||||
char m_id[17];
|
||||
char m_workerId[128];
|
||||
|
|
|
@ -110,13 +110,11 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url
|
|||
return done(connection, status, nullptr);
|
||||
}
|
||||
|
||||
MHD_Response *rsp = nullptr;
|
||||
size_t size = 0;
|
||||
const char *buf = Api::get(url, &size, &status);
|
||||
|
||||
if (size) {
|
||||
rsp = MHD_create_response_from_buffer(size, (void*) buf, MHD_RESPMEM_PERSISTENT);
|
||||
char *buf = Api::get(url, &status);
|
||||
if (buf == nullptr) {
|
||||
return MHD_NO;
|
||||
}
|
||||
|
||||
MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE);
|
||||
return done(connection, status, rsp);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue