New config subsystem
This commit is contained in:
parent
85ee55f309
commit
fa2a0b9b79
37 changed files with 1628 additions and 1254 deletions
|
@ -26,89 +26,28 @@
|
|||
#include <assert.h>
|
||||
|
||||
|
||||
#include "base/io/log/backends/ConsoleLog.h"
|
||||
#include "base/io/log/backends/FileLog.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/kernel/interfaces/IControllerListener.h"
|
||||
#include "common/config/ConfigLoader.h"
|
||||
#include "common/cpu/Cpu.h"
|
||||
#include "common/Platform.h"
|
||||
#include "core/config/Config.h"
|
||||
#include "core/Controller.h"
|
||||
#include "net/Network.h"
|
||||
|
||||
|
||||
#ifdef HAVE_SYSLOG_H
|
||||
# include "base/io/log/backends/SysLog.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_API
|
||||
# include "api/Api.h"
|
||||
#endif
|
||||
|
||||
|
||||
class xmrig::ControllerPrivate
|
||||
{
|
||||
public:
|
||||
inline ControllerPrivate(Process *process) :
|
||||
api(nullptr),
|
||||
config(nullptr),
|
||||
network(nullptr),
|
||||
process(process)
|
||||
{}
|
||||
|
||||
|
||||
inline ~ControllerPrivate()
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
delete api;
|
||||
# endif
|
||||
|
||||
delete network;
|
||||
delete config;
|
||||
}
|
||||
|
||||
|
||||
Api *api;
|
||||
Config *config;
|
||||
Network *network;
|
||||
Process *process;
|
||||
std::vector<IControllerListener *> listeners;
|
||||
};
|
||||
|
||||
|
||||
xmrig::Controller::Controller(Process *process)
|
||||
: d_ptr(new ControllerPrivate(process))
|
||||
xmrig::Controller::Controller(Process *process) :
|
||||
Base(process),
|
||||
m_network(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
xmrig::Controller::~Controller()
|
||||
{
|
||||
delete d_ptr;
|
||||
}
|
||||
|
||||
|
||||
xmrig::Api *xmrig::Controller::api() const
|
||||
{
|
||||
assert(d_ptr->api != nullptr);
|
||||
|
||||
return d_ptr->api;
|
||||
delete m_network;
|
||||
}
|
||||
|
||||
|
||||
bool xmrig::Controller::isReady() const
|
||||
{
|
||||
return d_ptr->config && d_ptr->network;
|
||||
}
|
||||
|
||||
|
||||
xmrig::Config *xmrig::Controller::config() const
|
||||
{
|
||||
assert(d_ptr->config != nullptr);
|
||||
|
||||
return d_ptr->config;
|
||||
return Base::isReady() && m_network;
|
||||
}
|
||||
|
||||
|
||||
|
@ -116,98 +55,36 @@ int xmrig::Controller::init()
|
|||
{
|
||||
Cpu::init();
|
||||
|
||||
d_ptr->config = Config::load(d_ptr->process, this);
|
||||
if (!d_ptr->config) {
|
||||
return 1;
|
||||
const int rc = Base::init();
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
d_ptr->api = new Api(this);
|
||||
# endif
|
||||
|
||||
Platform::init(config()->userAgent());
|
||||
Platform::setProcessPriority(d_ptr->config->priority());
|
||||
|
||||
if (!config()->isBackground()) {
|
||||
Log::add(new ConsoleLog());
|
||||
}
|
||||
|
||||
if (config()->logFile()) {
|
||||
Log::add(new FileLog(config()->logFile()));
|
||||
}
|
||||
|
||||
# ifdef HAVE_SYSLOG_H
|
||||
if (config()->isSyslog()) {
|
||||
Log::add(new SysLog());
|
||||
}
|
||||
# endif
|
||||
|
||||
d_ptr->network = new Network(this);
|
||||
m_network = new Network(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
xmrig::Network *xmrig::Controller::network() const
|
||||
{
|
||||
assert(d_ptr->network != nullptr);
|
||||
|
||||
return d_ptr->network;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Controller::addListener(IControllerListener *listener)
|
||||
{
|
||||
d_ptr->listeners.push_back(listener);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Controller::save()
|
||||
{
|
||||
if (!config()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (d_ptr->config->isShouldSave()) {
|
||||
d_ptr->config->save();
|
||||
}
|
||||
|
||||
ConfigLoader::watch(d_ptr->config);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Controller::onNewConfig(IConfig *config)
|
||||
{
|
||||
Config *previousConfig = d_ptr->config;
|
||||
d_ptr->config = static_cast<Config*>(config);
|
||||
|
||||
for (IControllerListener *listener : d_ptr->listeners) {
|
||||
listener->onConfigChanged(d_ptr->config, previousConfig);
|
||||
}
|
||||
|
||||
delete previousConfig;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Controller::start()
|
||||
{
|
||||
Base::start();
|
||||
|
||||
network()->connect();
|
||||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
api()->start();
|
||||
# endif
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Controller::stop()
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
api()->stop();
|
||||
# endif
|
||||
Base::stop();
|
||||
|
||||
ConfigLoader::release();
|
||||
|
||||
delete d_ptr->network;
|
||||
d_ptr->network = nullptr;
|
||||
delete m_network;
|
||||
m_network = nullptr;
|
||||
}
|
||||
|
||||
|
||||
xmrig::Network *xmrig::Controller::network() const
|
||||
{
|
||||
assert(m_network != nullptr);
|
||||
|
||||
return m_network;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue