From 1cd2b12bd4e4d86f6cd099240795f3ebeb3852b3 Mon Sep 17 00:00:00 2001 From: Maksym Mamontov Date: Thu, 25 Aug 2022 16:16:56 +0300 Subject: [PATCH] More subscriptions, less notifiers. --- include/stg/subscriptions.h | 6 +-- include/stg/tariffs.h | 39 +++++++++++-------- include/stg/users.h | 6 ++- .../stargazer/plugins/authorization/ao/ao.cpp | 4 +- .../authorization/inetaccess/inetaccess.cpp | 2 +- .../stargazer/plugins/other/ping/ping.cpp | 4 +- .../plugins/other/rscript/rscript.cpp | 4 +- .../stargazer/plugins/other/smux/smux.cpp | 14 +++---- projects/stargazer/plugins/other/smux/smux.h | 19 +-------- projects/stargazer/tariffs_impl.cpp | 39 +------------------ projects/stargazer/tariffs_impl.h | 9 ----- projects/stargazer/traffcounter_impl.cpp | 4 +- projects/stargazer/users_impl.h | 4 +- tests/testtariffs.h | 6 --- 14 files changed, 51 insertions(+), 109 deletions(-) diff --git a/include/stg/subscriptions.h b/include/stg/subscriptions.h index 853cebe1..a50cbba3 100644 --- a/include/stg/subscriptions.h +++ b/include/stg/subscriptions.h @@ -53,7 +53,7 @@ template class Subscriptions { public: - using Callback = std::function; + using Callback = std::function; using Callbacks = std::list; Connection makeConn(typename Callbacks::iterator i) noexcept @@ -71,7 +71,7 @@ class Subscriptions template Connection add(C& c, void (C::*m)(T2s...)) { - return add([&c, m](Ts&&... values){ (c.*m)(std::forward(values)...); }); + return add([&c, m](const Ts&... values){ (c.*m)(values...); }); } void remove(typename Callbacks::iterator i) @@ -80,7 +80,7 @@ class Subscriptions m_callbacks.erase(i); } - void notify(Ts&&... values) + void notify(const Ts&... values) { std::lock_guard lock(m_mutex); for (auto& cb : m_callbacks) diff --git a/include/stg/tariffs.h b/include/stg/tariffs.h index 66db5f31..c47b857f 100644 --- a/include/stg/tariffs.h +++ b/include/stg/tariffs.h @@ -20,11 +20,11 @@ #pragma once +#include "subscriptions.h" + #include #include -#include "notifer.h" - namespace STG { @@ -32,27 +32,32 @@ struct Admin; struct Tariff; struct TariffData; -struct Tariffs { - virtual ~Tariffs() = default; +class Tariffs +{ + public: + virtual ~Tariffs() = default; - virtual int ReadTariffs() = 0; - virtual const Tariff* FindByName(const std::string& name) const = 0; - virtual const Tariff* GetNoTariff() const = 0; - virtual int Del(const std::string& name, const Admin* admin) = 0; - virtual int Add(const std::string& name, const Admin* admin) = 0; - virtual int Chg(const TariffData& td, const Admin* admin) = 0; + virtual int ReadTariffs() = 0; + virtual const Tariff* FindByName(const std::string& name) const = 0; + virtual const Tariff* GetNoTariff() const = 0; + virtual int Del(const std::string& name, const Admin* admin) = 0; + virtual int Add(const std::string& name, const Admin* admin) = 0; + virtual int Chg(const TariffData& td, const Admin* admin) = 0; - virtual void AddNotifierAdd(NotifierBase* notifier) = 0; - virtual void DelNotifierAdd(NotifierBase* notifier) = 0; + template + auto onAdd(F&& f) { return m_onAddCallbacks.add(std::forward(f)); } + template + auto onDel(F&& f) { return m_onDelCallbacks.add(std::forward(f)); } - virtual void AddNotifierDel(NotifierBase* notifier) = 0; - virtual void DelNotifierDel(NotifierBase* notifier) = 0; + virtual void GetTariffsData(std::vector* tdl) const = 0; - virtual void GetTariffsData(std::vector* tdl) const = 0; + virtual size_t Count() const = 0; - virtual size_t Count() const = 0; + virtual const std::string& GetStrError() const = 0; - virtual const std::string& GetStrError() const = 0; + protected: + Subscriptions m_onAddCallbacks; + Subscriptions m_onDelCallbacks; }; } diff --git a/include/stg/users.h b/include/stg/users.h index a8d4eb60..1587f350 100644 --- a/include/stg/users.h +++ b/include/stg/users.h @@ -24,6 +24,8 @@ #include +#include + namespace STG { @@ -46,9 +48,9 @@ class Users virtual bool TariffInUse(const std::string& tariffName) const = 0; template - auto onUserAdd(F&& f) { return m_onAddCallbacks.add(std::forward(f)); } + auto onAdd(F&& f) { return m_onAddCallbacks.add(std::forward(f)); } template - auto onUserDel(F&& f) { return m_onDelCallbacks.add(std::forward(f)); } + auto onDel(F&& f) { return m_onDelCallbacks.add(std::forward(f)); } virtual int Add(const std::string& login, const Admin* admin) = 0; virtual void Del(const std::string& login, const Admin* admin) = 0; diff --git a/projects/stargazer/plugins/authorization/ao/ao.cpp b/projects/stargazer/plugins/authorization/ao/ao.cpp index d0be8dd5..355c7061 100644 --- a/projects/stargazer/plugins/authorization/ao/ao.cpp +++ b/projects/stargazer/plugins/authorization/ao/ao.cpp @@ -57,8 +57,8 @@ int AUTH_AO::Start() printfd(__FILE__, "AUTH_AO::Start()\n"); GetUsers(); -m_onAddUserConn = users->onUserAdd([this](auto user){ AddUser(user); }); -m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); }); +m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); }); +m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); }); std::for_each(userList.begin(), userList.end(), [this](auto user){ UpdateUserAuthorization(user); }); diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 3c6a687c..c607b2ac 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -340,7 +340,7 @@ AUTH_IA::~AUTH_IA() //----------------------------------------------------------------------------- int AUTH_IA::Start() { -m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); }); +m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); }); if (PrepareNet()) return -1; diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index 65dd3454..71752de6 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -79,8 +79,8 @@ int PING::Start() { GetUsers(); -m_onAddUserConn = users->onUserAdd([this](auto user){ AddUser(user); }); -m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); }); +m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); }); +m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); }); pinger.SetDelayTime(pingSettings.GetPingDelay()); pinger.Start(); diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index d54c800e..f8dc9d99 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -199,8 +199,8 @@ netRouters = rsSettings.GetSubnetsMap(); InitEncrypt(rsSettings.GetPassword()); -m_onAddUserConn = users->onUserAdd([this](auto user){ AddUser(user); }); -m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); }); +m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); }); +m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); }); if (GetUsers()) return -1; diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 8d5079bd..07f9d200 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -100,7 +100,6 @@ SMUX::SMUX() lastReconnectTry(0), reconnectTimeout(1), sock(-1), - addDelTariffNotifier(*this), logger(STG::PluginLogger::get("smux")) { smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler; @@ -442,23 +441,24 @@ while (users->SearchNext(h, &u) == 0) users->CloseSearch(h); -m_onAddUserConn = users->onUserAdd([this](auto user){ +m_onAddUserConn = users->onAdd([this](auto user){ SetNotifier(user); UpdateTables(); }); -m_onDelUserConn = users->onUserDel([this](auto user){ +m_onDelUserConn = users->onDel([this](auto user){ UnsetNotifier(user); UpdateTables(); }); -tariffs->AddNotifierAdd(&addDelTariffNotifier); -tariffs->AddNotifierDel(&addDelTariffNotifier); +auto updateTables = [this](const STG::TariffData&){ UpdateTables(); }; +m_onAddTariffConn = tariffs->onAdd(updateTables); +m_onDelTariffConn = tariffs->onDel(updateTables); } void SMUX::ResetNotifiers() { -tariffs->DelNotifierDel(&addDelTariffNotifier); -tariffs->DelNotifierAdd(&addDelTariffNotifier); +m_onAddTariffConn.disconnect(); +m_onDelTariffConn.disconnect(); m_onAddUserConn.disconnect(); m_onDelUserConn.disconnect(); diff --git a/projects/stargazer/plugins/other/smux/smux.h b/projects/stargazer/plugins/other/smux/smux.h index 1986d28b..3e708e2b 100644 --- a/projects/stargazer/plugins/other/smux/smux.h +++ b/projects/stargazer/plugins/other/smux/smux.h @@ -81,16 +81,6 @@ private: UserPtr userPtr; }; //----------------------------------------------------------------------------- -class ADD_DEL_TARIFF_NOTIFIER : public STG::NotifierBase { -public: - explicit ADD_DEL_TARIFF_NOTIFIER(SMUX & s) - : STG::NotifierBase(), smux(s) {} - void notify(const STG::TariffData &) override; - -private: - SMUX & smux; -}; -//----------------------------------------------------------------------------- class SMUX : public STG::Plugin { public: SMUX(); @@ -170,9 +160,10 @@ private: STG::ScopedConnection m_onAddUserConn; STG::ScopedConnection m_onDelUserConn; + STG::ScopedConnection m_onAddTariffConn; + STG::ScopedConnection m_onDelTariffConn; std::list notifiers; - ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier; STG::PluginLogger logger; }; @@ -183,9 +174,3 @@ void CHG_AFTER_NOTIFIER::notify(const std::string &, const std::string &) { smux.UpdateTables(); } - -inline -void ADD_DEL_TARIFF_NOTIFIER::notify(const STG::TariffData &) -{ -smux.UpdateTables(); -} diff --git a/projects/stargazer/tariffs_impl.cpp b/projects/stargazer/tariffs_impl.cpp index 492f403f..116570d3 100644 --- a/projects/stargazer/tariffs_impl.cpp +++ b/projects/stargazer/tariffs_impl.cpp @@ -175,12 +175,7 @@ TariffData td; tariffs.erase(ti); } -auto ni = onDelNotifiers.begin(); -while (ni != onDelNotifiers.end()) - { - (*ni)->notify(td); - ++ni; - } +m_onDelCallbacks.notify(td); WriteServLog("%s Tariff \'%s\' deleted.", admin->logStr().c_str(), @@ -223,13 +218,7 @@ if (store->AddTariff(name) < 0) return -1; } -// Fire all "on add" notifiers -auto ni = onAddNotifiers.begin(); -while (ni != onAddNotifiers.end()) - { - (*ni)->notify(tariffs.back().GetTariffData()); - ++ni; - } +m_onAddCallbacks.notify(tariffs.back().GetTariffData()); WriteServLog("%s Tariff \'%s\' added.", admin->logStr().c_str(), name.c_str()); @@ -249,27 +238,3 @@ for (; it != tariffs.end(); ++it) } } //----------------------------------------------------------------------------- -void TariffsImpl::AddNotifierAdd(NotifierBase * n) -{ -std::lock_guard lock(m_mutex); -onAddNotifiers.insert(n); -} -//----------------------------------------------------------------------------- -void TariffsImpl::DelNotifierAdd(NotifierBase * n) -{ -std::lock_guard lock(m_mutex); -onAddNotifiers.erase(n); -} -//----------------------------------------------------------------------------- -void TariffsImpl::AddNotifierDel(NotifierBase * n) -{ -std::lock_guard lock(m_mutex); -onDelNotifiers.insert(n); -} -//----------------------------------------------------------------------------- -void TariffsImpl::DelNotifierDel(NotifierBase * n) -{ -std::lock_guard lock(m_mutex); -onDelNotifiers.erase(n); -} -//----------------------------------------------------------------------------- diff --git a/projects/stargazer/tariffs_impl.h b/projects/stargazer/tariffs_impl.h index 4b554b4d..31d22b59 100644 --- a/projects/stargazer/tariffs_impl.h +++ b/projects/stargazer/tariffs_impl.h @@ -55,12 +55,6 @@ class TariffsImpl : public Tariffs { int Add(const std::string & name, const Admin * admin) override; int Chg(const TariffData & td, const Admin * admin) override; - void AddNotifierAdd(NotifierBase * notifier) override; - void DelNotifierAdd(NotifierBase * notifier) override; - - void AddNotifierDel(NotifierBase * notifier) override; - void DelNotifierDel(NotifierBase * notifier) override; - void GetTariffsData(std::vector * tdl) const override; const std::string & GetStrError() const override { return strError; } @@ -72,9 +66,6 @@ class TariffsImpl : public Tariffs { mutable std::mutex m_mutex; std::string strError; TariffImpl noTariff; - - std::set*> onAddNotifiers; - std::set*> onDelNotifiers; }; } diff --git a/projects/stargazer/traffcounter_impl.cpp b/projects/stargazer/traffcounter_impl.cpp index aa156c9d..44f2e558 100644 --- a/projects/stargazer/traffcounter_impl.cpp +++ b/projects/stargazer/traffcounter_impl.cpp @@ -78,10 +78,10 @@ for (int i = 0; i < DIR_NUM; i++) dirName[DIR_NUM] = "NULL"; -m_onAddUserConn = users->onUserImplAdd([this](auto user){ +m_onAddUserConn = users->onImplAdd([this](auto user){ AsyncPoolST::enqueue([this, user](){ SetUserNotifiers(user); }); }); -m_onDelUserConn = users->onUserImplDel([this](auto user){ +m_onDelUserConn = users->onImplDel([this](auto user){ AsyncPoolST::enqueue([this, user](){ UnSetUserNotifiers(user); }); AsyncPoolST::enqueue([this, user](){ DelUser(user->GetCurrIP()); }); }); diff --git a/projects/stargazer/users_impl.h b/projects/stargazer/users_impl.h index b5a37063..7b6f900a 100644 --- a/projects/stargazer/users_impl.h +++ b/projects/stargazer/users_impl.h @@ -83,9 +83,9 @@ class UsersImpl : public Users bool TariffInUse(const std::string & tariffName) const override; template - auto onUserImplAdd(F&& f) { return m_onAddImplCallbacks.add(std::forward(f)); } + auto onImplAdd(F&& f) { return m_onAddImplCallbacks.add(std::forward(f)); } template - auto onUserImplDel(F&& f) { return m_onDelImplCallbacks.add(std::forward(f)); } + auto onImplDel(F&& f) { return m_onDelImplCallbacks.add(std::forward(f)); } int Add(const std::string & login, const Admin * admin) override; void Del(const std::string & login, const Admin * admin) override; diff --git a/tests/testtariffs.h b/tests/testtariffs.h index 30f26a5d..ca8ab0ce 100644 --- a/tests/testtariffs.h +++ b/tests/testtariffs.h @@ -16,12 +16,6 @@ class TestTariffs : public STG::Tariffs int Add(const std::string& /*name*/, const STG::Admin* /*admin*/) override { return 0; } int Chg(const STG::TariffData& /*td*/, const STG::Admin* /*admin*/) override { return 0; } - void AddNotifierAdd(STG::NotifierBase*) override {} - void DelNotifierAdd(STG::NotifierBase*) override {} - - void AddNotifierDel(STG::NotifierBase*) override {} - void DelNotifierDel(STG::NotifierBase*) override {} - void GetTariffsData(std::vector* /*tdl*/) const override {} size_t Count() const override { return 0; } -- 2.44.2