Fixed bugs in CCClient, added missing delete/desttructors for restart

This commit is contained in:
BenDroid 2017-10-17 22:31:22 +02:00
parent 5c065a8e48
commit 2b97e2f4ff
17 changed files with 194 additions and 110 deletions

View file

@ -88,7 +88,7 @@ void CCClient::updateNetworkState(const NetworkState &network)
{
uv_mutex_lock(&m_mutex);
m_self->m_clientStatus.setCurrentStatus(Workers::isEnabled() ? "mining" : "paused");
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);
@ -100,50 +100,55 @@ void CCClient::updateNetworkState(const NetworkState &network)
void CCClient::publishClientStatusReport()
{
std::string requestUrl = m_self->m_serverURL + "/client/setClientStatus?clientId=" + m_self->m_clientStatus.getClientId();
std::string requestUrl = m_self->m_serverURL
+ "/client/setClientStatus?clientId="
+ m_self->m_clientStatus.getClientId();
std::string requestBuffer = m_self->m_clientStatus.toJsonString();
std::string responseBuffer;
CURLcode res = performCurl(requestUrl, requestBuffer, "POST", responseBuffer);
if (res != CURLE_OK) {
LOG_ERR("CCClient error: %s", curl_easy_strerror(res));
LOG_ERR("[CC-Client] error: \"%s\" -> %s",
curl_easy_strerror(res), requestUrl.c_str());
} else {
ControlCommand controlCommand;
if (controlCommand.parseFromJsonString(responseBuffer)) {
if (controlCommand.getCommand() == ControlCommand::START) {
if (!Workers::isEnabled()) {
LOG_INFO("Command: START received -> Resuming");
LOG_INFO("[CC-Client] Command: START received -> resume");
Workers::setEnabled(true);
}
} else if (controlCommand.getCommand() == ControlCommand::STOP) {
if (!Workers::isEnabled()) {
LOG_INFO("Command: STOP received -> Pausing");
Workers::setEnabled(true);
if (Workers::isEnabled()) {
LOG_INFO("[CC-Client] Command: STOP received -> pause");
Workers::setEnabled(false);
}
} else if (controlCommand.getCommand() == ControlCommand::UPDATE_CONFIG) {
LOG_INFO("Command: UPDATE_CONFIG received -> Updating config");
updateConfig();
} else if (controlCommand.getCommand() == ControlCommand::RELOAD) {
LOG_INFO("Command: RELOAD received -> Reload");
App::reloadConfig();
} else {
LOG_ERR("Command: GET_CONFIG received -> NOT IMPLEMENTED YET!");
} else if (controlCommand.getCommand() == ControlCommand::RESTART) {
App::restart();
} else if (controlCommand.getCommand() == ControlCommand::QUIT) {
// TODO
}
} else {
LOG_ERR("Unknown Command received from CC Server.");
LOG_ERR("[CC-Client] unknown command received from CC Server.");
}
}
}
void CCClient::updateConfig()
{
std::string requestUrl = m_self->m_serverURL + "/client/getConfig?clientId=" + m_self->m_clientStatus.getClientId();
std::string requestUrl = m_self->m_serverURL
+ "/client/getConfig?clientId="
+ m_self->m_clientStatus.getClientId();
std::string requestBuffer;
std::string responseBuffer;
CURLcode res = performCurl(requestUrl, requestBuffer, "GET", responseBuffer);
if (res != CURLE_OK) {
LOG_ERR("CCClient error: %s", curl_easy_strerror(res));
LOG_ERR("[CC-Client] error: \"%s\" -> %s", curl_easy_strerror(res), requestUrl.c_str());
} else {
rapidjson::Document document;
if (!document.Parse(responseBuffer.c_str()).HasParseError()) {
@ -157,13 +162,13 @@ void CCClient::updateConfig()
clientConfigFile << buffer.GetString();
clientConfigFile.close();
LOG_INFO("Config update done. Reload.");
App::reloadConfig();
LOG_INFO("[CC-Client] config updated. restart.");
App::restart();
} else {
LOG_ERR("Not able to store client config to file %s.", m_self->m_options->configFile());
LOG_ERR("[CC-Client] not able to store client config to file %s.", m_self->m_options->configFile());
}
} else{
LOG_ERR("Not able to store client config. The received client config is broken!");
LOG_ERR("[CC-Client] not able to store client config. received client config is broken!");
}
}
}

View file

@ -63,12 +63,12 @@ void ClientStatus::setCurrentPool(const std::string& currentPool)
m_currentPool = currentPool;
}
const std::string ClientStatus::getCurrentStatus() const
ClientStatus::Status ClientStatus::getCurrentStatus() const
{
return m_currentStatus;
}
void ClientStatus::setCurrentStatus(const std::string& currentStatus)
void ClientStatus::setCurrentStatus(Status currentStatus)
{
m_currentStatus = currentStatus;
}
@ -175,7 +175,7 @@ bool ClientStatus::parseFromJson(const rapidjson::Document& document)
}
if (clientStatus.HasMember("current_status")) {
m_currentStatus = clientStatus["current_status"].GetString();
m_currentStatus = toStatus(clientStatus["current_status"].GetString());
}
if (clientStatus.HasMember("hashrate_short")) {
@ -227,7 +227,7 @@ rapidjson::Value ClientStatus::toJson(rapidjson::MemoryPoolAllocator<rapidjson::
clientStatus.AddMember("client_id", rapidjson::StringRef(m_clientId.c_str()), allocator);
clientStatus.AddMember("current_pool", rapidjson::StringRef(m_currentPool.c_str()), allocator);
clientStatus.AddMember("current_status", rapidjson::StringRef(m_currentStatus.c_str()), allocator);
clientStatus.AddMember("current_status", rapidjson::StringRef(toString(m_currentStatus)), allocator);
clientStatus.AddMember("hashrate_short", m_hashrateShort, allocator);
clientStatus.AddMember("hashrate_medium", m_hashrateMedium, allocator);

View file

@ -31,18 +31,41 @@
class ClientStatus
{
public:
public:
enum Status {
RUNNING,
PAUSED,
CONFIG_UPDATED
};
public:
ClientStatus();
inline const char *toString (Status status)
{
return status_str[static_cast<int>(status)];
}
inline Status toStatus (const char *status)
{
const int n = sizeof(status_str) / sizeof(status_str[0]);
for (int i = 0; i < n; ++i)
{
if (strcmp(status_str[i], status) == 0)
return (Status) i;
}
return Status::RUNNING;
}
const std::string getClientId() const;
void setClientId(const std::string& clientId);
const std::string getCurrentPool() const;
void setCurrentPool(const std::string& currentPool);
const std::string getCurrentStatus() const;
void setCurrentStatus(const std::string& currentStatus);
Status getCurrentStatus() const;
void setCurrentStatus(Status currentStatus);
double getHashrateShort() const;
void setHashrateShort(double hashrateShort);
@ -76,9 +99,16 @@ public:
private:
const char* status_str[3] = {
"RUNNING",
"PAUSED",
"CONFIG_UPDATED"
};
Status m_currentStatus;
std::string m_clientId;
std::string m_currentPool;
std::string m_currentStatus;
double m_hashrateShort;
double m_hashrateMedium;

View file

@ -34,7 +34,7 @@ ControlCommand::ControlCommand()
}
ControlCommand::ControlCommand(ControlCommand::Command command)
ControlCommand::ControlCommand(Command command)
: m_command(command)
{
@ -59,7 +59,7 @@ bool ControlCommand::parseFromJson(const rapidjson::Document& document)
if (document.HasMember("control_command")) {
rapidjson::Value::ConstObject controlCommand = document["control_command"].GetObject();
if (controlCommand.HasMember("command")) {
m_command = static_cast<Command>(controlCommand["command"].GetUint());
m_command = toCommand(controlCommand["command"].GetString());
result = true;
}
else {
@ -76,12 +76,12 @@ rapidjson::Value ControlCommand::toJson(rapidjson::MemoryPoolAllocator<rapidjson
{
rapidjson::Value controlCommand(rapidjson::kObjectType);
controlCommand.AddMember("command", m_command, allocator);
controlCommand.AddMember("command", rapidjson::StringRef(toString(m_command)), allocator);
return controlCommand;
}
void ControlCommand::setCommand(ControlCommand::Command command)
void ControlCommand::setCommand(Command command)
{
m_command = command;
}
@ -89,4 +89,11 @@ void ControlCommand::setCommand(ControlCommand::Command command)
ControlCommand::Command ControlCommand::getCommand() const
{
return m_command;
}
}
bool ControlCommand::isOneTimeCommand() const {
return m_command == ControlCommand::UPDATE_CONFIG ||
m_command == ControlCommand::RESTART ||
m_command == ControlCommand::QUIT;
}

View file

@ -32,18 +32,34 @@
class ControlCommand
{
public:
enum Command
{
enum Command {
START,
STOP,
RELOAD,
UPDATE_CONFIG,
HALT
RESTART,
QUIT
};
public:
ControlCommand();
explicit ControlCommand(Command command);
inline const char *toString (Command command)
{
return command_str[static_cast<int>(command)];
}
inline Command toCommand (const char *command)
{
const int n = sizeof(command_str) / sizeof(command_str[0]);
for (int i = 0; i < n; ++i)
{
if (strcmp(command_str[i], command) == 0)
return (Command) i;
}
return Command::START;
}
rapidjson::Value toJson(rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>& allocator);
bool parseFromJsonString(const std::string& json);
bool parseFromJson(const rapidjson::Document& document);
@ -51,7 +67,17 @@ public:
Command getCommand() const;
void setCommand(Command command);
bool isOneTimeCommand() const;
private:
const char* command_str[5] = {
"START",
"STOP",
"UPDATE_CONFIG",
"RESTART",
"QUIT"
};
Command m_command;
};

View file

@ -60,7 +60,7 @@ unsigned Service::handleGET(const Options* options, const std::string& url, cons
{
uv_mutex_lock(&m_mutex);
unsigned resultCode = MHD_HTTP_BAD_REQUEST;
unsigned resultCode = MHD_HTTP_NOT_FOUND;
LOG_INFO("GET(url='%s', clientId='%s')", url.c_str(), clientId.c_str());
@ -72,12 +72,6 @@ unsigned Service::handleGET(const Options* options, const std::string& url, cons
if (!clientId.empty()) {
if (url.rfind("/client/getConfig", 0) == 0 || url.rfind("/admin/getClientConfig", 0) == 0) {
resultCode = getClientConfig(options, clientId, resp);
if (url.rfind("/client/getConfig", 0) == 0) {
std::map<std::string,ControlCommand>::iterator iter = m_clientCommand.find(clientId);
if (iter != m_clientCommand.end()) {
m_clientCommand.erase(iter);
}
}
} else if (url.rfind("/admin/getClientCommand", 0) == 0) {
resultCode = getClientCommand(clientId, resp);
}
@ -96,7 +90,7 @@ unsigned Service::handlePOST(const Options* options, const std::string& url, con
{
uv_mutex_lock(&m_mutex);
unsigned resultCode = MHD_HTTP_BAD_REQUEST;
unsigned resultCode = MHD_HTTP_NOT_FOUND;
LOG_INFO("POST(url='%s', clientId='%s', data='%s')", url.c_str(), clientId.c_str(), data.c_str());
@ -212,6 +206,8 @@ unsigned Service::getClientStatusList(std::string& resp)
unsigned Service::setClientStatus(const std::string& clientId, const std::string& data, std::string& resp)
{
int resultCode = MHD_HTTP_BAD_REQUEST;
rapidjson::Document document;
if (!document.Parse(data.c_str()).HasParseError()) {
LOG_INFO("Status from client: %s", clientId.c_str());
@ -220,12 +216,17 @@ unsigned Service::setClientStatus(const std::string& clientId, const std::string
clientStatus.parseFromJson(document);
m_clientStatus[clientId] = clientStatus;
resultCode = getClientCommand(clientId, resp);
if (m_clientCommand[clientId].isOneTimeCommand()) {
m_clientCommand.erase(clientId);
}
} else {
LOG_ERR("Parse Error Occured: %d", document.GetParseError());
return MHD_HTTP_BAD_REQUEST;
}
return getClientCommand(clientId, resp);
return resultCode;
}
unsigned Service::getClientCommand(const std::string& clientId, std::string& resp)