Added classes Api and ApiState.

This commit is contained in:
XMRig 2017-08-31 04:30:59 +03:00
parent 5601c7a672
commit 71f06530df
8 changed files with 270 additions and 4 deletions

View file

@ -10,6 +10,8 @@ include (CheckIncludeFile)
set(HEADERS
src/3rdparty/align.h
src/api/Api.h
src/api/ApiState.h
src/App.h
src/Console.h
src/Cpu.h
@ -60,6 +62,8 @@ set(HEADERS_CRYPTO
)
set(SOURCES
src/api/Api.cpp
src/api/ApiState.cpp
src/App.cpp
src/Console.cpp
src/log/ConsoleLog.cpp

View file

@ -26,6 +26,7 @@
#include <uv.h>
#include "api/Api.h"
#include "App.h"
#include "Console.h"
#include "Cpu.h"
@ -124,6 +125,8 @@ int App::exec()
Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash(), m_options->hugePages());
Summary::print();
Api::start();
# ifndef XMRIG_NO_HTTPD
m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken());
m_httpd->start();

69
src/api/Api.cpp 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 2016-2017 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 "api/Api.h"
#include "api/ApiState.h"
ApiState *Api::m_state = nullptr;
char Api::m_buf[4096];
uv_mutex_t Api::m_mutex;
bool Api::start()
{
uv_mutex_init(&m_mutex);
m_state = new ApiState();
return true;
}
void Api::release()
{
delete m_state;
}
const char *Api::get(const char *url, size_t *size, 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;
}
uv_mutex_unlock(&m_mutex);
return m_buf;
}

48
src/api/Api.h Normal file
View file

@ -0,0 +1,48 @@
/* 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-2017 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 __API_H__
#define __API_H__
#include <uv.h>
class ApiState;
class Api
{
public:
static bool start();
static void release();
static const char *get(const char *url, size_t *size, int *status);
private:
static ApiState *m_state;
static char m_buf[4096];
static uv_mutex_t m_mutex;
};
#endif /* __API_H__ */

79
src/api/ApiState.cpp Normal file
View file

@ -0,0 +1,79 @@
/* 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-2017 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 <string.h>
#include "api/ApiState.h"
#include "Cpu.h"
#include "Mem.h"
#include "Options.h"
#include "Platform.h"
#include "version.h"
ApiState::ApiState()
{
}
ApiState::~ApiState()
{
}
const char *ApiState::get(const char *url, size_t *size) const
{
json_t *reply = json_object();
getMiner(reply);
return finalize(reply, size);
}
const char *ApiState::finalize(json_t *reply, size_t *size) const
{
*size = json_dumpb(reply, m_buf, sizeof(m_buf) - 1, JSON_INDENT(4));
json_decref(reply);
return m_buf;
}
void ApiState::getMiner(json_t *reply) 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()));
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()));
}

46
src/api/ApiState.h Normal file
View file

@ -0,0 +1,46 @@
/* 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-2017 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 __APISTATE_H__
#define __APISTATE_H__
#include "jansson.h"
class ApiState
{
public:
ApiState();
~ApiState();
const char *get(const char *url, size_t *size) const;
private:
const char *finalize(json_t *reply, size_t *size) const;
void getMiner(json_t *reply) const;
mutable char m_buf[4096];
};
#endif /* __APISTATE_H__ */

View file

@ -25,12 +25,13 @@
#include <microhttpd.h>
#include "api/Api.h"
#include "api/Httpd.h"
#include "log/Log.h"
static const char kNotFound [] = "{\"error\":\"NOT_FOUND\"}";
static const size_t kNotFoundSize = sizeof(kNotFound) - 1;
static const char k500 [] = "{\"error\":\"INTERNAL_SERVER_ERROR\"}";
static const size_t k500Size = sizeof(k500) - 1;
Httpd::Httpd(int port, const char *accessToken) :
@ -59,11 +60,26 @@ bool Httpd::start()
int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
{
struct MHD_Response *rsp = MHD_create_response_from_buffer(kNotFoundSize, (void*)kNotFound, MHD_RESPMEM_PERSISTENT);
if (strcmp(method, "GET") != 0) {
return MHD_NO;
}
struct MHD_Response *rsp;
size_t size = 0;
int status = MHD_HTTP_OK;
const char *buf = Api::get(url, &size, &status);
if (size) {
rsp = MHD_create_response_from_buffer(size, (void*) buf, MHD_RESPMEM_PERSISTENT);
}
else {
rsp = MHD_create_response_from_buffer(k500Size, (void*) k500, MHD_RESPMEM_PERSISTENT);
}
MHD_add_response_header(rsp, "Content-Type", "application/json");
const int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, rsp);
const int ret = MHD_queue_response(connection, status, rsp);
MHD_destroy_response(rsp);
return ret;
}

View file

@ -31,6 +31,7 @@
#define APP_DOMAIN "xmrig.com"
#define APP_SITE "www.xmrig.com"
#define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com"
#define APP_KIND "cpu"
#define APP_VER_MAJOR 2
#define APP_VER_MINOR 3