diff --git a/src/base/net/Pools.cpp b/src/base/net/Pools.cpp index c21cfe214..1513541bc 100644 --- a/src/base/net/Pools.cpp +++ b/src/base/net/Pools.cpp @@ -51,6 +51,16 @@ xmrig::Pool &xmrig::Pools::current() } +bool xmrig::Pools::isEqual(const Pools &other) const +{ + if (m_data.size() != other.m_data.size() || m_retries != other.m_retries || m_retryPause != other.m_retryPause) { + return false; + } + + return std::equal(m_data.begin(), m_data.end(), other.m_data.begin()); +} + + bool xmrig::Pools::setUrl(const char *url) { if (m_data.empty() || m_data.back().isValid()) { @@ -144,7 +154,7 @@ void xmrig::Pools::load(const rapidjson::Value &pools) } -void xmrig::Pools::print() +void xmrig::Pools::print() const { size_t i = 1; for (const Pool &pool : m_data) { diff --git a/src/base/net/Pools.h b/src/base/net/Pools.h index 8657b7750..a6038a044 100644 --- a/src/base/net/Pools.h +++ b/src/base/net/Pools.h @@ -59,13 +59,17 @@ public: inline void setVariant(const char *variant) { current().algorithm().parseVariant(variant); } inline void setVariant(int variant) { current().algorithm().parseVariant(variant); } + inline bool operator!=(const Pools &other) const { return !isEqual(other); } + inline bool operator==(const Pools &other) const { return isEqual(other); } + + bool isEqual(const Pools &other) const; bool setUrl(const char *url); IStrategy *createStrategy(IStrategyListener *listener) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; size_t active() const; void adjust(const Algorithm &algorithm); void load(const rapidjson::Value &pools); - void print(); + void print() const; void setRetries(int retries); void setRetryPause(int retryPause); diff --git a/src/common/interfaces/IControllerListener.h b/src/common/interfaces/IControllerListener.h index 35249bcde..c61574083 100644 --- a/src/common/interfaces/IControllerListener.h +++ b/src/common/interfaces/IControllerListener.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -34,7 +35,7 @@ class Config; class IControllerListener { public: - virtual ~IControllerListener() {} + virtual ~IControllerListener() = default; virtual void onConfigChanged(Config *config, Config *previousConfig) = 0; }; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index e2df21f79..a7177571d 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -47,6 +47,7 @@ xmrig::Network::Network(Controller *controller) : m_donate(nullptr) { Workers::setListener(this); + controller->addListener(this); const Pools &pools = controller->config()->pools(); m_strategy = pools.createStrategy(this); @@ -64,6 +65,7 @@ xmrig::Network::Network(Controller *controller) : xmrig::Network::~Network() { + delete m_strategy; } @@ -104,6 +106,22 @@ void xmrig::Network::onActive(IStrategy *strategy, Client *client) } +void xmrig::Network::onConfigChanged(Config *config, Config *previousConfig) +{ + if (config->pools() == previousConfig->pools() || !config->pools().active()) { + return; + } + + m_strategy->stop(); + + config->pools().print(); + + delete m_strategy; + m_strategy = config->pools().createStrategy(this); + connect(); +} + + void xmrig::Network::onJob(IStrategy *strategy, Client *client, const Job &job) { if (m_donate && m_donate->isActive() && m_donate != strategy) { diff --git a/src/net/Network.h b/src/net/Network.h index e9b073ae9..b11d6998e 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -31,6 +31,7 @@ #include "api/NetworkState.h" +#include "common/interfaces/IControllerListener.h" #include "common/interfaces/IStrategyListener.h" #include "interfaces/IJobResultListener.h" @@ -42,36 +43,37 @@ class Controller; class IStrategy; -class Network : public IJobResultListener, public IStrategyListener +class Network : public IJobResultListener, public IStrategyListener, public IControllerListener { public: - Network(Controller *controller); - ~Network() override; + Network(Controller *controller); + ~Network() override; - void connect(); - void stop(); + void connect(); + void stop(); protected: - void onActive(IStrategy *strategy, Client *client) override; - void onJob(IStrategy *strategy, Client *client, const Job &job) override; - void onJobResult(const JobResult &result) override; - void onPause(IStrategy *strategy) override; - void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; + void onActive(IStrategy *strategy, Client *client) override; + void onConfigChanged(Config *config, Config *previousConfig) override; + void onJob(IStrategy *strategy, Client *client, const Job &job) override; + void onJobResult(const JobResult &result) override; + void onPause(IStrategy *strategy) override; + void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; private: - constexpr static int kTickInterval = 1 * 1000; + constexpr static int kTickInterval = 1 * 1000; - bool isColors() const; - void setJob(Client *client, const Job &job, bool donate); - void tick(); + bool isColors() const; + void setJob(Client *client, const Job &job, bool donate); + void tick(); - static void onTick(uv_timer_t *handle); + static void onTick(uv_timer_t *handle); - Controller *m_controller; - IStrategy *m_donate; - IStrategy *m_strategy; - NetworkState m_state; - uv_timer_t m_timer; + Controller *m_controller; + IStrategy *m_donate; + IStrategy *m_strategy; + NetworkState m_state; + uv_timer_t m_timer; };