Rewritten watch feature.
This commit is contained in:
parent
b368ffacdb
commit
0450c31449
19 changed files with 419 additions and 111 deletions
|
@ -65,7 +65,7 @@ public:
|
|||
|
||||
inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); }
|
||||
inline const Algorithm &algorithm() const override { return m_algorithm; }
|
||||
inline const char *fileName() const override { return m_fileName.data(); }
|
||||
inline const String &fileName() const override { return m_fileName; }
|
||||
|
||||
bool save() override;
|
||||
|
||||
|
|
|
@ -39,23 +39,22 @@
|
|||
|
||||
|
||||
#include "base/io/Json.h"
|
||||
#include "base/kernel/interfaces/IConfigListener.h"
|
||||
#include "common/config/ConfigLoader.h"
|
||||
#include "common/config/ConfigWatcher.h"
|
||||
#include "common/interfaces/IConfig.h"
|
||||
#include "common/interfaces/IWatcherListener.h"
|
||||
#include "common/net/Pool.h"
|
||||
#include "common/Platform.h"
|
||||
#include "core/ConfigCreator.h"
|
||||
#include "core/ConfigLoader_platform.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/error/en.h"
|
||||
#include "rapidjson/filereadstream.h"
|
||||
|
||||
|
||||
bool xmrig::ConfigLoader::m_done = false;
|
||||
xmrig::ConfigWatcher *xmrig::ConfigLoader::m_watcher = nullptr;
|
||||
xmrig::IConfigCreator *xmrig::ConfigLoader::m_creator = nullptr;
|
||||
xmrig::IWatcherListener *xmrig::ConfigLoader::m_listener = nullptr;
|
||||
xmrig::IConfigListener *xmrig::ConfigLoader::m_listener = nullptr;
|
||||
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
|
@ -78,8 +77,9 @@ bool xmrig::ConfigLoader::loadFromFile(xmrig::IConfig *config, const char *fileN
|
|||
|
||||
bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const char *json)
|
||||
{
|
||||
rapidjson::Document doc;
|
||||
doc.Parse(json);
|
||||
using namespace rapidjson;
|
||||
Document doc;
|
||||
doc.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(json);
|
||||
|
||||
if (doc.HasParseError() || !doc.IsObject()) {
|
||||
return false;
|
||||
|
@ -144,7 +144,7 @@ bool xmrig::ConfigLoader::reload(xmrig::IConfig *oldConfig, const char *json)
|
|||
}
|
||||
|
||||
|
||||
xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator *creator, IWatcherListener *listener)
|
||||
xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator *creator, IConfigListener *listener)
|
||||
{
|
||||
m_creator = creator;
|
||||
m_listener = listener;
|
||||
|
|
|
@ -40,7 +40,7 @@ namespace xmrig {
|
|||
|
||||
class ConfigWatcher;
|
||||
class IConfigCreator;
|
||||
class IWatcherListener;
|
||||
class IConfigListener;
|
||||
class IConfig;
|
||||
|
||||
|
||||
|
@ -51,7 +51,7 @@ public:
|
|||
static bool loadFromJSON(IConfig *config, const char *json);
|
||||
static bool loadFromJSON(IConfig *config, const rapidjson::Document &doc);
|
||||
static bool reload(IConfig *oldConfig, const char *json);
|
||||
static IConfig *load(int argc, char **argv, IConfigCreator *creator, IWatcherListener *listener);
|
||||
static IConfig *load(int argc, char **argv, IConfigCreator *creator, IConfigListener *listener);
|
||||
static void release();
|
||||
|
||||
static inline bool isDone() { return m_done; }
|
||||
|
@ -66,7 +66,7 @@ private:
|
|||
static bool m_done;
|
||||
static ConfigWatcher *m_watcher;
|
||||
static IConfigCreator *m_creator;
|
||||
static IWatcherListener *m_listener;
|
||||
static IConfigListener *m_listener;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/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
|
||||
|
@ -22,66 +23,35 @@
|
|||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#include "base/io/Watcher.h"
|
||||
#include "base/kernel/interfaces/IConfigListener.h"
|
||||
#include "common/config/ConfigLoader.h"
|
||||
#include "common/config/ConfigWatcher.h"
|
||||
#include "common/interfaces/IWatcherListener.h"
|
||||
#include "common/log/Log.h"
|
||||
#include "core/ConfigCreator.h"
|
||||
|
||||
|
||||
xmrig::ConfigWatcher::ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener) :
|
||||
xmrig::ConfigWatcher::ConfigWatcher(const String &path, IConfigCreator *creator, IConfigListener *listener) :
|
||||
m_creator(creator),
|
||||
m_listener(listener),
|
||||
m_path(path)
|
||||
m_listener(listener)
|
||||
{
|
||||
uv_fs_event_init(uv_default_loop(), &m_fsEvent);
|
||||
uv_timer_init(uv_default_loop(), &m_timer);
|
||||
|
||||
m_fsEvent.data = m_timer.data = this;
|
||||
|
||||
start();
|
||||
m_watcher = new Watcher(path, this);
|
||||
}
|
||||
|
||||
|
||||
xmrig::ConfigWatcher::~ConfigWatcher()
|
||||
{
|
||||
uv_timer_stop(&m_timer);
|
||||
uv_fs_event_stop(&m_fsEvent);
|
||||
delete m_watcher;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::ConfigWatcher::onTimer(uv_timer_t* handle)
|
||||
|
||||
void xmrig::ConfigWatcher::onFileChanged(const String &fileName)
|
||||
{
|
||||
static_cast<xmrig::ConfigWatcher *>(handle->data)->reload();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::ConfigWatcher::onFsEvent(uv_fs_event_t* handle, const char *filename, int events, int status)
|
||||
{
|
||||
if (!filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
static_cast<xmrig::ConfigWatcher *>(handle->data)->queueUpdate();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::ConfigWatcher::queueUpdate()
|
||||
{
|
||||
uv_timer_stop(&m_timer);
|
||||
uv_timer_start(&m_timer, xmrig::ConfigWatcher::onTimer, kDelay, 0);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::ConfigWatcher::reload()
|
||||
{
|
||||
LOG_WARN("\"%s\" was changed, reloading configuration", m_path.data());
|
||||
LOG_WARN("\"%s\" was changed, reloading configuration", fileName.data());
|
||||
|
||||
IConfig *config = m_creator->create();
|
||||
ConfigLoader::loadFromFile(config, m_path.data());
|
||||
ConfigLoader::loadFromFile(config, fileName);
|
||||
|
||||
if (!config->finalize()) {
|
||||
LOG_ERR("reloading failed");
|
||||
|
@ -91,15 +61,4 @@ void xmrig::ConfigWatcher::reload()
|
|||
}
|
||||
|
||||
m_listener->onNewConfig(config);
|
||||
|
||||
# ifndef _WIN32
|
||||
uv_fs_event_stop(&m_fsEvent);
|
||||
start();
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void xmrig::ConfigWatcher::start()
|
||||
{
|
||||
uv_fs_event_start(&m_fsEvent, xmrig::ConfigWatcher::onFsEvent, m_path.data(), 0);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/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
|
||||
|
@ -21,15 +22,12 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __CONFIGWATCHER_H__
|
||||
#define __CONFIGWATCHER_H__
|
||||
#ifndef XMRIG_CONFIGWATCHER_H
|
||||
#define XMRIG_CONFIGWATCHER_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <uv.h>
|
||||
|
||||
|
||||
#include "common/utils/c_str.h"
|
||||
#include "base/kernel/interfaces/IWatcherListener.h"
|
||||
#include "base/tools/String.h"
|
||||
#include "rapidjson/fwd.h"
|
||||
|
||||
|
||||
|
@ -40,29 +38,23 @@ namespace xmrig {
|
|||
|
||||
|
||||
class IConfigCreator;
|
||||
class IWatcherListener;
|
||||
class IConfigListener;
|
||||
class Watcher;
|
||||
|
||||
|
||||
class ConfigWatcher
|
||||
class ConfigWatcher : public IWatcherListener
|
||||
{
|
||||
public:
|
||||
ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener);
|
||||
~ConfigWatcher();
|
||||
ConfigWatcher(const String &path, IConfigCreator *creator, IConfigListener *listener);
|
||||
~ConfigWatcher() override;
|
||||
|
||||
protected:
|
||||
void onFileChanged(const String &fileName) override;
|
||||
|
||||
private:
|
||||
constexpr static int kDelay = 500;
|
||||
|
||||
static void onFsEvent(uv_fs_event_t* handle, const char *filename, int events, int status);
|
||||
static void onTimer(uv_timer_t* handle);
|
||||
void queueUpdate();
|
||||
void reload();
|
||||
void start();
|
||||
|
||||
IConfigCreator *m_creator;
|
||||
IWatcherListener *m_listener;
|
||||
uv_fs_event_t m_fsEvent;
|
||||
uv_timer_t m_timer;
|
||||
xmrig::c_str m_path;
|
||||
IConfigListener *m_listener;
|
||||
Watcher *m_watcher;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -33,6 +33,9 @@
|
|||
namespace xmrig {
|
||||
|
||||
|
||||
class String;
|
||||
|
||||
|
||||
class IConfig
|
||||
{
|
||||
public:
|
||||
|
@ -136,7 +139,7 @@ public:
|
|||
virtual bool parseUint64(int key, uint64_t arg) = 0;
|
||||
virtual bool save() = 0;
|
||||
virtual const Algorithm &algorithm() const = 0;
|
||||
virtual const char *fileName() const = 0;
|
||||
virtual const String &fileName() const = 0;
|
||||
virtual void getJSON(rapidjson::Document &doc) const = 0;
|
||||
virtual void parseJSON(const rapidjson::Document &doc) = 0;
|
||||
virtual void setFileName(const char *fileName) = 0;
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
* 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-2018 XMRig <support@xmrig.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/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
|
||||
|
@ -20,8 +22,8 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __ICONFIGCREATOR_H__
|
||||
#define __ICONFIGCREATOR_H__
|
||||
#ifndef XMRIG_ICONFIGCREATOR_H
|
||||
#define XMRIG_ICONFIGCREATOR_H
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
@ -33,7 +35,7 @@ class IConfig;
|
|||
class IConfigCreator
|
||||
{
|
||||
public:
|
||||
virtual ~IConfigCreator() {}
|
||||
virtual ~IConfigCreator() = default;
|
||||
|
||||
virtual IConfig *create() const = 0;
|
||||
};
|
||||
|
@ -42,4 +44,4 @@ public:
|
|||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif // __ICONFIGCREATOR_H__
|
||||
#endif // XMRIG_ICONFIGCREATOR_H
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
/* 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 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2016-2018 XMRig <https://github.com/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 __IWATCHERLISTENER_H__
|
||||
#define __IWATCHERLISTENER_H__
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class IConfig;
|
||||
|
||||
|
||||
class IWatcherListener
|
||||
{
|
||||
public:
|
||||
virtual ~IWatcherListener() {}
|
||||
|
||||
virtual void onNewConfig(IConfig *config) = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif // __IWATCHERLISTENER_H__
|
Loading…
Add table
Add a link
Reference in a new issue