From 003e17d18f0ccbf2e990ad88f6fbae1265478da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ben=20Gr=C3=A4f?= Date: Sun, 8 Oct 2017 20:46:48 +0200 Subject: [PATCH] Cleanup CMakeLists, Added ControlCommand --- CMakeLists.txt | 55 +++++++++++++------- src/server/ClientStatus.cpp | 46 ++++++++--------- src/server/ClientStatus.h | 28 +++++----- src/server/ControlCommand.cpp | 97 +++++++++++++++++++++++++++++++++++ src/server/ControlCommand.h | 54 +++++++++++++++++++ src/server/Httpd.cpp | 11 ++-- src/server/Service.cpp | 83 +++++------------------------- src/server/Service.h | 20 +++----- 8 files changed, 254 insertions(+), 140 deletions(-) create mode 100644 src/server/ControlCommand.cpp create mode 100644 src/server/ControlCommand.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b4aabeaf..8e8e1cf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,9 @@ project(xmrig) set(CMAKE_BUILD_TYPE Debug) -option(WITH_LIBCPUID "Use Libcpuid" ON) -option(WITH_AEON "CryptoNight-Lite support" ON) -option(WITH_HTTPD "HTTP REST API" ON) +option(WITH_LIBCPUID "Use Libcpuid" ON) +option(WITH_AEON "CryptoNight-Lite support" ON) +option(WITH_HTTPD "HTTP REST API" ON) option(WITH_CC_SERVER "CC Server" ON) include (CheckIncludeFile) @@ -237,24 +237,41 @@ else() add_definitions(/DXMRIG_NO_API) endif() -if (WITH_CC_SERVER AND MHD_FOUND) - include_directories(${MHD_INCLUDE_DIRS}) +if (WITH_CC_SERVER) + find_package(MHD) + + if (MHD_FOUND) + include_directories(${MHD_INCLUDE_DIRS}) + else() + message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_CC_SERVER=OFF` to build without CC Server support") + endif() + + find_package(CURL) + + if (CURL_FOUND) + include_directories(${CURL_INCLUDE_DIRS}) + else() + message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_CC_SERVER=OFF` to build without CC Server support") + endif() set(CC_SERVER_SOURCES - src/server/ClientStatus.cpp - src/server/xmrigCC.cpp - src/server/CCServer.cpp - src/server/Service.cpp - src/server/Summary.cpp - src/server/Httpd.cpp) + src/server/ControlCommand.cpp + src/server/ClientStatus.cpp + src/server/xmrigCC.cpp + src/server/CCServer.cpp + src/server/Service.cpp + src/server/Summary.cpp + src/server/Httpd.cpp) set(CC_SERVER_HEADERS - src/server/ClientStatus.h - src/server/CCServer.h - src/server/Service.h - src/server/version.h - src/server/Httpd.h) - + src/server/ControlCommand.h + src/server/ClientStatus.h + src/server/CCServer.h + src/server/Service.h + src/server/version.h + src/server/Httpd.h) +else() + add_definitions(/DXMRIG_NO_CC_SERVER) endif() include_directories(src) @@ -262,9 +279,9 @@ include_directories(src/3rdparty) include_directories(${UV_INCLUDE_DIR}) add_executable(xmrig ${HEADERS} ${SOURCES} ${HEADERS_COMMON} ${SOURCES_COMMON} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) -target_link_libraries(xmrig ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) +target_link_libraries(xmrig ${UV_LIBRARIES} ${MHD_LIBRARY} ${CURL_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) -if (WITH_CC_SERVER AND MHD_FOUND) +if (WITH_CC_SERVER AND MHD_FOUND AND CURL_FOUND) add_executable(xmrigCC ${HEADERS_COMMON} ${SOURCES_COMMON} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CC_SERVER} ${CC_SERVER_SOURCES} ${SOURCES_SYSLOG}) target_link_libraries(xmrigCC ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) endif() \ No newline at end of file diff --git a/src/server/ClientStatus.cpp b/src/server/ClientStatus.cpp index 2b939037..2d277715 100644 --- a/src/server/ClientStatus.cpp +++ b/src/server/ClientStatus.cpp @@ -28,113 +28,113 @@ #include "server/ClientStatus.h" ClientStatus::ClientStatus() - : hashrateShort(0), hashrateMedium(0), hashrateLong(0), sharesGood(0), sharedTotal(0), hashesTotal(0), - lastStatusUpdate(0) + : m_hashrateShort(0), m_hashrateMedium(0), m_hashrateLong(0), m_sharesGood(0), m_sharedTotal(0), m_hashesTotal(0), + m_lastStatusUpdate(0) { } const std::string ClientStatus::getMiner() const { - return miner; + return m_miner; } void ClientStatus::setMiner(const std::string &miner) { - ClientStatus::miner = miner; + m_miner = miner; } const std::string ClientStatus::getCurrentPool() const { - return currentPool; + return m_currentPool; } void ClientStatus::setCurrentPool(const std::string ¤tPool) { - ClientStatus::currentPool = currentPool; + m_currentPool = currentPool; } const std::string ClientStatus::getCurrentStatus() const { - return currentStatus; + return m_currentStatus; } void ClientStatus::setCurrentStatus(const std::string ¤tStatus) { - ClientStatus::currentStatus = currentStatus; + m_currentStatus = currentStatus; } double ClientStatus::getHashrateShort() const { - return hashrateShort; + return m_hashrateShort; } void ClientStatus::setHashrateShort(double hashrateShort) { - ClientStatus::hashrateShort = hashrateShort; + m_hashrateShort = hashrateShort; } double ClientStatus::getHashrateMedium() const { - return hashrateMedium; + return m_hashrateMedium; } void ClientStatus::setHashrateMedium(double hashrateMedium) { - ClientStatus::hashrateMedium = hashrateMedium; + m_hashrateMedium = hashrateMedium; } double ClientStatus::getHashrateLong() const { - return hashrateLong; + return m_hashrateLong; } void ClientStatus::setHashrateLong(double hashrateLong) { - ClientStatus::hashrateLong = hashrateLong; + m_hashrateLong = hashrateLong; } uint64_t ClientStatus::getSharesGood() const { - return sharesGood; + return m_sharesGood; } void ClientStatus::setSharesGood(uint64_t sharesGood) { - ClientStatus::sharesGood = sharesGood; + m_sharesGood = sharesGood; } uint64_t ClientStatus::getSharedTotal() const { - return sharedTotal; + return m_sharedTotal; } void ClientStatus::setSharedTotal(uint64_t sharedTotal) { - ClientStatus::sharedTotal = sharedTotal; + m_sharedTotal = sharedTotal; } uint64_t ClientStatus::getHashesTotal() const { - return hashesTotal; + return m_hashesTotal; } void ClientStatus::setHashesTotal(uint64_t hashesTotal) { - ClientStatus::hashesTotal = hashesTotal; + m_hashesTotal = hashesTotal; } const uint32_t ClientStatus::getLastStatusUpdate() const { - return lastStatusUpdate; + return m_lastStatusUpdate; } void ClientStatus::setLastStatusUpdate(uint32_t lastStatusUpdate) { - ClientStatus::lastStatusUpdate = lastStatusUpdate; + m_lastStatusUpdate = lastStatusUpdate; } -void ClientStatus::parseFromJson(const rapidjson::Document &doc) +void ClientStatus::parseFromJson(const rapidjson::Document &document) { } diff --git a/src/server/ClientStatus.h b/src/server/ClientStatus.h index c200b1b3..80adfe45 100644 --- a/src/server/ClientStatus.h +++ b/src/server/ClientStatus.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __CLIENTSTATUS_H__ -#define __CLIENTSTATUS_H__ +#ifndef __CLIENT_STATUS_H__ +#define __CLIENT_STATUS_H__ #include #include "rapidjson/document.h" @@ -65,21 +65,21 @@ public: void setLastStatusUpdate(uint32_t lastStatusUpdate); std::string toJson(); - void parseFromJson(const rapidjson::Document &doc); + void parseFromJson(const rapidjson::Document &document); private: - std::string miner; - std::string currentPool; - std::string currentStatus; + std::string m_miner; + std::string m_currentPool; + std::string m_currentStatus; - double hashrateShort; - double hashrateMedium; - double hashrateLong; + double m_hashrateShort; + double m_hashrateMedium; + double m_hashrateLong; - uint64_t sharesGood; - uint64_t sharedTotal; - uint64_t hashesTotal; - uint32_t lastStatusUpdate; + uint64_t m_sharesGood; + uint64_t m_sharedTotal; + uint64_t m_hashesTotal; + uint32_t m_lastStatusUpdate; }; -#endif /* __CLIENTSTATUS_H__ */ +#endif /* __CLIENT_STATUS_H__ */ diff --git a/src/server/ControlCommand.cpp b/src/server/ControlCommand.cpp new file mode 100644 index 00000000..7aaf0ebb --- /dev/null +++ b/src/server/ControlCommand.cpp @@ -0,0 +1,97 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * Copyright 2017- BenDr0id + * + * + * 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 . + */ + +#include +#include <3rdparty/rapidjson/stringbuffer.h> +#include <3rdparty/rapidjson/prettywriter.h> + +#include "log/Log.h" +#include "server/ControlCommand.h" + +ControlCommand::ControlCommand() + : m_command(Command::START) +{ + +} + +ControlCommand::ControlCommand(ControlCommand::Command command) + : m_command(command) +{ + +} + +bool ControlCommand::parseFromJson(const std::string &json) +{ + bool result = false; + + rapidjson::Document document; + if (!document.Parse(json.c_str()).HasParseError()) { + if (document.HasMember("control_command")) + { + rapidjson::Value controlCommand = document["control_command"].GetObject(); + if (controlCommand.HasMember("command")) { + m_command = static_cast(controlCommand["command"].GetUint()); + result = true; + } + else { + LOG_ERR("Parse Error, JSON does not contain: command"); + } + } else { + LOG_ERR("Parse Error, JSON does not contain: control_command"); + } + } + else { + LOG_ERR("Parse Error Occured: %d", document.GetParseError()); + } + + return result; +} + +std::string ControlCommand::toJson() +{ + rapidjson::Document document; + document.SetObject(); + + rapidjson::Value controlCommand(rapidjson::kObjectType); + controlCommand.AddMember("command", m_command, document.GetAllocator()); + + document.AddMember("control_command", controlCommand, document.GetAllocator()); + + rapidjson::StringBuffer buffer(0, 1024); + rapidjson::PrettyWriter writer(buffer); + writer.SetMaxDecimalPlaces(10); + document.Accept(writer); + + return strdup(buffer.GetString());; +} + +void ControlCommand::setCommand(ControlCommand::Command command) +{ + m_command = command; +} + +ControlCommand::Command ControlCommand::getCommand() const +{ + return m_command; +} diff --git a/src/server/ControlCommand.h b/src/server/ControlCommand.h new file mode 100644 index 00000000..dc965a16 --- /dev/null +++ b/src/server/ControlCommand.h @@ -0,0 +1,54 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * Copyright 2017- BenDr0id + * + * + * 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 . + */ + +#ifndef __CONTROL_COMMAND_H__ +#define __CONTROL_COMMAND_H__ + +#include +#include "rapidjson/document.h" + +class ControlCommand +{ +public: + enum Command + { + START, + STOP, + UPDATE_CONFIG + }; + + ControlCommand(); + explicit ControlCommand(Command command); + + bool parseFromJson(const std::string &json); + std::string toJson(); + + void setCommand(Command command); + Command getCommand() const; + +private: + Command m_command; +}; + +#endif /* __CONTROL_COMMAND_H__ */ diff --git a/src/server/Httpd.cpp b/src/server/Httpd.cpp index 2abe75a5..ac60b468 100644 --- a/src/server/Httpd.cpp +++ b/src/server/Httpd.cpp @@ -181,10 +181,13 @@ int Httpd::handler(void *cls, MHD_Connection *connection, const char *url, const return MHD_NO; } -int Httpd::handleGET(struct MHD_Connection *connection, const char *url) -{LOG_INFO("HANDLE GET REQUEST"); +int Httpd::handleGET(struct MHD_Connection *connection, const char *urlPtr) +{ + LOG_INFO("HANDLE GET REQUEST"); std::string resp; + std::string url(urlPtr, strlen(urlPtr)); + unsigned status = Service::get(url, resp); MHD_Response *rsp = nullptr; @@ -195,7 +198,7 @@ int Httpd::handleGET(struct MHD_Connection *connection, const char *url) return sendJSONResponse(connection, status, rsp); } -int Httpd::handlePOST(struct MHD_Connection *connection, const char* url, const char *upload_data, +int Httpd::handlePOST(struct MHD_Connection *connection, const char* urlPtr, const char *upload_data, size_t *upload_data_size, void **con_cls) { LOG_INFO("HANDLE POST REQUEST"); @@ -212,6 +215,8 @@ int Httpd::handlePOST(struct MHD_Connection *connection, const char* url, const } else { std::string resp; + std::string url(urlPtr, strlen(urlPtr)); + unsigned status = Service::post(url, cc->data.str(), resp); MHD_Response *rsp = nullptr; diff --git a/src/server/Service.cpp b/src/server/Service.cpp index ab647fc4..ce550cf3 100644 --- a/src/server/Service.cpp +++ b/src/server/Service.cpp @@ -26,7 +26,6 @@ #include "server/Service.h" #include "log/Log.h" -char Service::m_buf[4096]; uv_mutex_t Service::m_mutex; @@ -40,98 +39,44 @@ bool Service::start() void Service::release() { - -} - - -unsigned Service::get(const char *url, std::string &resp) -{ - //if (!m_state) { - // *size = 0; - // return nullptr; - //} - uv_mutex_lock(&m_mutex); - LOG_INFO("GET(%s)", url); + m_clientStatus.clear(); + m_clientCommand.clear(); - /* + uv_mutex_unlock(&m_mutex); +} - Handle request here +unsigned Service::get(const std::string &url, std::string &resp) +{ + uv_mutex_lock(&m_mutex); - const char *buf = m_state->get(url, size); - if (*size) { - memcpy(m_buf, buf, *size); - } - else { - *status = 500; - } - */ + LOG_INFO("GET(%s)", url.c_str()); uv_mutex_unlock(&m_mutex); return 200; } -unsigned Service::post(const char *url, const std::string &data, std::string &resp) +unsigned Service::post(const std::string &url, const std::string &data, std::string &resp) { - //if (!m_state) { - // *size = 0; - // return nullptr; - //} - uv_mutex_lock(&m_mutex); - LOG_INFO("POST(%s, %s)", url, data.c_str()); + LOG_INFO("POST(url='%s', data='%s')", url.c_str(), data.c_str()); rapidjson::Document document; if (!document.Parse(data.c_str()).HasParseError()) { - LOG_ERR("Status from miner: %s", document['miner'].GetString()); + LOG_INFO("Status from miner: %s", document["miner"].GetString()); } else { LOG_ERR("Parse Error Occured: %d", document.GetParseError()); return MHD_HTTP_BAD_REQUEST; } + ControlCommand controlCommand; + resp = controlCommand.toJson(); - /* - - Handle request here - - const char *buf = m_state->get(url, size); - if (*size) { - memcpy(m_buf, buf, *size); - } - else { - *status = 500; - } - */ uv_mutex_unlock(&m_mutex); return 200; -} - -/* -void Service::tick(const Hashrate *hashrate) -{ - if (!m_state) { - return; - } - - uv_mutex_lock(&m_mutex); - m_state->tick(hashrate); - uv_mutex_unlock(&m_mutex); -} - - -void Service::tick(const NetworkState &network) -{ - if (!m_state) { - return; - } - - uv_mutex_lock(&m_mutex); - m_state->tick(network); - uv_mutex_unlock(&m_mutex); -} -*/ +} \ No newline at end of file diff --git a/src/server/Service.h b/src/server/Service.h index be81589c..5eb96c9d 100644 --- a/src/server/Service.h +++ b/src/server/Service.h @@ -28,12 +28,9 @@ #include #include #include - - -//class ServiceState; -//class Hashrate; -//class NetworkState; - +#include +#include "ClientStatus.h" +#include "ControlCommand.h" class Service { @@ -41,14 +38,13 @@ public: static bool start(); static void release(); - static unsigned get(const char *url, std::string &resp); - static unsigned post(const char *url, const std::string &data, std::string &resp); -// static void tick(const Hashrate *hashrate); -// static void tick(const NetworkState &results); + static unsigned get(const std::string &url, std::string &resp); + static unsigned post(const std::string &url, const std::string &data, std::string &resp); private: - //static ServiceState *m_state; - static char m_buf[4096]; + static std::map m_clientStatus; + static std::map m_clientCommand; + static uv_mutex_t m_mutex; };