Added IConsoleListener

This commit is contained in:
XMRig 2017-07-23 09:36:30 +03:00
parent 583f45e2da
commit 776f77a8d4
10 changed files with 104 additions and 5 deletions

View file

@ -14,6 +14,7 @@ set(HEADERS
src/Console.h
src/Cpu.h
src/interfaces/IClientListener.h
src/interfaces/IConsoleListener.h
src/interfaces/IJobResultListener.h
src/interfaces/ILogBackend.h
src/interfaces/IStrategy.h

View file

@ -64,7 +64,7 @@ App::App(int argc, char **argv) :
if (!m_options->background()) {
Log::add(new ConsoleLog(m_options->colors()));
m_console = new Console();
m_console = new Console(this);
}
if (m_options->logFile()) {
@ -125,6 +125,39 @@ int App::exec()
}
void App::onConsoleCommand(char command)
{
switch (command) {
case 'h':
case 'H':
Workers::printHashrate(true);
break;
case 'p':
case 'P':
LOG_INFO(m_options->colors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume");
Workers::setEnabled(false);
break;
case 'r':
case 'R':
if (!Workers::isEnabled()) {
LOG_INFO(m_options->colors() ? "\x1B[01;32mresumed" : "resumed");
Workers::setEnabled(true);
}
break;
case 3:
LOG_WARN("Ctrl+C received, exiting");
close();
break;
default:
break;
}
}
void App::close()
{
m_network->stop();

View file

@ -28,12 +28,15 @@
#include <uv.h>
#include "interfaces/IConsoleListener.h"
class Console;
class Network;
class Options;
class App
class App : public IConsoleListener
{
public:
App(int argc, char **argv);
@ -41,6 +44,9 @@ public:
int exec();
protected:
void onConsoleCommand(char command) override;
private:
void background();
void close();

View file

@ -23,9 +23,11 @@
#include "Console.h"
#include "interfaces/IConsoleListener.h"
Console::Console()
Console::Console(IConsoleListener *listener)
: m_listener(listener)
{
m_tty.data = this;
uv_tty_init(uv_default_loop(), &m_tty, 0, 1);
@ -50,6 +52,6 @@ void Console::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
}
if (nread == 1) {
printf("%c\n", buf->base[0]);
static_cast<Console*>(stream->data)->m_listener->onConsoleCommand(buf->base[0]);
}
}

View file

@ -28,16 +28,20 @@
#include <uv.h>
class IConsoleListener;
class Console
{
public:
Console();
Console(IConsoleListener *listener);
private:
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
char m_buf[1];
IConsoleListener *m_listener;
uv_tty_t m_tty;
};

View file

@ -0,0 +1,37 @@
/* 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 __ICONSOLELISTENER_H__
#define __ICONSOLELISTENER_H__
class IConsoleListener
{
public:
virtual ~IConsoleListener() {}
virtual void onConsoleCommand(char command) = 0;
};
#endif // __ICONSOLELISTENER_H__

View file

@ -69,6 +69,10 @@ void DoubleWorker::start()
}
while (Workers::isPaused());
if (Workers::sequence() == 0) {
break;
}
consumeJob();
}

View file

@ -45,6 +45,10 @@ void SingleWorker::start()
}
while (Workers::isPaused());
if (Workers::sequence() == 0) {
break;
}
consumeJob();
}

View file

@ -60,6 +60,12 @@ Job Workers::job()
}
void Workers::printHashrate(bool detail)
{
m_hashrate->print();
}
void Workers::setEnabled(bool enabled)
{
if (m_enabled == enabled) {
@ -121,6 +127,7 @@ void Workers::stop()
m_hashrate->stop();
uv_close(reinterpret_cast<uv_handle_t*>(&m_async), nullptr);
m_paused = 0;
m_sequence = 0;
for (size_t i = 0; i < m_workers.size(); ++i) {

View file

@ -43,6 +43,7 @@ class Workers
{
public:
static Job job();
static void printHashrate(bool detail);
static void setEnabled(bool enabled);
static void setJob(const Job &job);
static void start(int64_t affinity);