Added xmrigCC-common src
This commit is contained in:
parent
b918e66dc5
commit
cf9f69294e
16 changed files with 2014 additions and 0 deletions
228
src/cc/CCClient.cpp
Normal file
228
src/cc/CCClient.cpp
Normal file
|
@ -0,0 +1,228 @@
|
|||
/* 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>
|
||||
* Copyright 2017- BenDr0id <ben@graef.in>
|
||||
*
|
||||
*
|
||||
* 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 <fstream>
|
||||
#include <3rdparty/rapidjson/stringbuffer.h>
|
||||
#include <3rdparty/rapidjson/prettywriter.h>
|
||||
#include <Platform.h>
|
||||
|
||||
#include "CCClient.h"
|
||||
#include "App.h"
|
||||
#include "ControlCommand.h"
|
||||
|
||||
#include "api/NetworkState.h"
|
||||
#include "log/Log.h"
|
||||
#include "workers/Workers.h"
|
||||
#include "workers/Hashrate.h"
|
||||
|
||||
#if _WIN32
|
||||
# include "winsock2.h"
|
||||
#else
|
||||
# include "unistd.h"
|
||||
#endif
|
||||
|
||||
|
||||
CCClient *CCClient::m_self = nullptr;
|
||||
uv_mutex_t CCClient::m_mutex;
|
||||
|
||||
CCClient::CCClient(const Options *options)
|
||||
: m_options(options)
|
||||
{
|
||||
uv_mutex_init(&m_mutex);
|
||||
|
||||
m_self = this;
|
||||
|
||||
std::string clientId;
|
||||
if (m_options->ccWorkerId()) {
|
||||
clientId = m_options->ccWorkerId();
|
||||
} else{
|
||||
char hostname[128];
|
||||
memset(hostname, 0, sizeof(hostname));
|
||||
gethostname(hostname, sizeof(hostname)-1);
|
||||
clientId = std::string(hostname);
|
||||
}
|
||||
|
||||
m_clientStatus.setClientId(clientId);
|
||||
|
||||
if (m_options->algoName() != nullptr) {
|
||||
m_clientStatus.setCurrentAlgoName(m_options->algoName());
|
||||
}
|
||||
|
||||
if (m_options->ccToken() != nullptr) {
|
||||
m_authorization = std::string("Bearer ") + m_self->m_options->ccToken();
|
||||
}
|
||||
|
||||
uv_timer_init(uv_default_loop(), &m_timer);
|
||||
uv_timer_start(&m_timer, CCClient::onReport,
|
||||
static_cast<uint64_t>(m_options->ccUpdateInterval() * 1000),
|
||||
static_cast<uint64_t>(m_options->ccUpdateInterval() * 1000));
|
||||
}
|
||||
|
||||
CCClient::~CCClient()
|
||||
{
|
||||
uv_timer_stop(&m_timer);
|
||||
m_self = nullptr;
|
||||
}
|
||||
|
||||
void CCClient::updateHashrate(const Hashrate *hashrate)
|
||||
{
|
||||
uv_mutex_lock(&m_mutex);
|
||||
|
||||
if (m_self) {
|
||||
m_self->m_clientStatus.setHashrateShort(hashrate->calc(Hashrate::ShortInterval));
|
||||
m_self->m_clientStatus.setHashrateMedium(hashrate->calc(Hashrate::MediumInterval));
|
||||
m_self->m_clientStatus.setHashrateLong(hashrate->calc(Hashrate::LargeInterval));
|
||||
m_self->m_clientStatus.setHashrateHighest(hashrate->highest());
|
||||
}
|
||||
|
||||
uv_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
|
||||
void CCClient::updateNetworkState(const NetworkState &network)
|
||||
{
|
||||
uv_mutex_lock(&m_mutex);
|
||||
|
||||
if (m_self) {
|
||||
m_self->m_clientStatus.setCurrentStatus(Workers::isEnabled() ? ClientStatus::RUNNING : ClientStatus::PAUSED);
|
||||
m_self->m_clientStatus.setCurrentPool(network.pool);
|
||||
m_self->m_clientStatus.setSharesGood(network.accepted);
|
||||
m_self->m_clientStatus.setSharesTotal(network.accepted + network.rejected);
|
||||
m_self->m_clientStatus.setHashesTotal(network.total);
|
||||
m_self->m_clientStatus.setAvgTime(network.avgTime());
|
||||
}
|
||||
uv_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
void CCClient::publishClientStatusReport()
|
||||
{
|
||||
std::string requestUrl = "/client/setClientStatus?clientId=" + m_self->m_clientStatus.getClientId();
|
||||
std::string requestBuffer = m_self->m_clientStatus.toJsonString();
|
||||
|
||||
auto res = performRequest(requestUrl, requestBuffer, "POST");
|
||||
if (!res) {
|
||||
LOG_ERR("[CC-Client] error: unable to performRequest POST -> http://%s:%d%s",
|
||||
m_self->m_options->ccHost(), m_self->m_options->ccPort(), requestUrl.c_str());
|
||||
} else if (res->status != 200) {
|
||||
LOG_ERR("[CC-Client] error: \"%d\" -> http://%s:%d%s", res->status, m_self->m_options->ccHost(),
|
||||
m_self->m_options->ccPort(), requestUrl.c_str());
|
||||
} else {
|
||||
ControlCommand controlCommand;
|
||||
if (controlCommand.parseFromJsonString(res->body)) {
|
||||
if (controlCommand.getCommand() == ControlCommand::START) {
|
||||
if (!Workers::isEnabled()) {
|
||||
LOG_WARN("[CC-Client] Command: START received -> resume");
|
||||
Workers::setEnabled(true);
|
||||
}
|
||||
} else if (controlCommand.getCommand() == ControlCommand::STOP) {
|
||||
if (Workers::isEnabled()) {
|
||||
LOG_WARN("[CC-Client] Command: STOP received -> pause");
|
||||
Workers::setEnabled(false);
|
||||
}
|
||||
} else if (controlCommand.getCommand() == ControlCommand::UPDATE_CONFIG) {
|
||||
LOG_WARN("[CC-Client] Command: UPDATE_CONFIG received -> update config");
|
||||
updateConfig();
|
||||
} else if (controlCommand.getCommand() == ControlCommand::RESTART) {
|
||||
LOG_WARN("[CC-Client] Command: RESTART received -> restart");
|
||||
App::restart();
|
||||
} else if (controlCommand.getCommand() == ControlCommand::SHUTDOWN) {
|
||||
LOG_WARN("[CC-Client] Command: SHUTDOWN received -> shutdown");
|
||||
App::shutdown();
|
||||
}
|
||||
} else {
|
||||
LOG_ERR("[CC-Client] Unknown command received from CC Server.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CCClient::updateConfig()
|
||||
{
|
||||
std::string requestUrl = "/client/getConfig?clientId=" + m_self->m_clientStatus.getClientId();
|
||||
std::string requestBuffer;
|
||||
|
||||
auto res = performRequest(requestUrl, requestBuffer, "GET");
|
||||
if (!res) {
|
||||
LOG_ERR("[CC-Client] error: unable to performRequest GET -> http://%s:%d%s",
|
||||
m_self->m_options->ccHost(), m_self->m_options->ccPort(), requestUrl.c_str());
|
||||
} else if (res->status != 200) {
|
||||
LOG_ERR("[CC-Client] error: \"%d\" -> http://%s:%d%s", res->status, m_self->m_options->ccHost(),
|
||||
m_self->m_options->ccPort(), requestUrl.c_str());
|
||||
} else {
|
||||
rapidjson::Document document;
|
||||
if (!document.Parse(res->body.c_str()).HasParseError()) {
|
||||
std::ofstream clientConfigFile(m_self->m_options->configFile());
|
||||
if (clientConfigFile) {
|
||||
rapidjson::StringBuffer buffer(0, 65536);
|
||||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
||||
writer.SetMaxDecimalPlaces(10);
|
||||
document.Accept(writer);
|
||||
|
||||
clientConfigFile << buffer.GetString();
|
||||
clientConfigFile.close();
|
||||
|
||||
LOG_WARN("[CC-Client] Config updated. -> restart");
|
||||
App::restart();
|
||||
} else {
|
||||
LOG_ERR("[CC-Client] Not able to store client config to file %s.", m_self->m_options->configFile());
|
||||
}
|
||||
} else{
|
||||
LOG_ERR("[CC-Client] Not able to store client config. received client config is broken!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<httplib::Response> CCClient::performRequest(const std::string& requestUrl,
|
||||
const std::string& requestBuffer,
|
||||
const std::string& operation)
|
||||
{
|
||||
httplib::Client cli(m_self->m_options->ccHost(), m_self->m_options->ccPort());
|
||||
|
||||
httplib::Request req;
|
||||
req.method = operation.c_str();
|
||||
req.path = requestUrl.c_str();
|
||||
req.set_header("Host", "");
|
||||
req.set_header("Accept", "*/*");
|
||||
req.set_header("User-Agent", Platform::userAgent());
|
||||
req.set_header("Accept", "application/json");
|
||||
req.set_header("Content-Type", "application/json");
|
||||
|
||||
if (!m_self->m_authorization.empty()) {
|
||||
req.set_header("Authorization", m_self->m_authorization.c_str());
|
||||
}
|
||||
|
||||
if (!requestBuffer.empty()) {
|
||||
req.body = requestBuffer.c_str();
|
||||
}
|
||||
|
||||
auto res = std::make_shared<httplib::Response>();
|
||||
|
||||
return cli.send(req, *res) ? res : nullptr;
|
||||
}
|
||||
|
||||
void CCClient::onReport(uv_timer_t *handle)
|
||||
{
|
||||
if (m_self) {
|
||||
m_self->publishClientStatusReport();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue