X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/f4333737c7e347efe9258ccf85c1237e03be619c..1cb912e4f90473f78d2f0f10ab5c9b2c5bd36440:/projects/stargazer/plugins/other/smux/smux.cpp diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index f79085df..a6ab8359 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -1,12 +1,7 @@ -#include -#include -#include +#include "smux.h" +#include "utils.h" -#include -#include -#include -#include -#include +#include "stg/common.h" #include #include @@ -14,10 +9,18 @@ #include #include -#include "stg/common.h" +#include +#include +#include +#include +#include -#include "smux.h" -#include "utils.h" +#include +#include +#include + +using STG::SMUX; +using STG::SMUX_SETTINGS; namespace { @@ -41,9 +44,9 @@ SMUX_SETTINGS::SMUX_SETTINGS() port(0) {} -int SMUX_SETTINGS::ParseSettings(const STG::ModuleSettings & s) +int SMUX_SETTINGS::ParseSettings(const ModuleSettings & s) { -STG::ParamValue pv; +ParamValue pv; int p; pv.param = "Port"; @@ -100,10 +103,7 @@ SMUX::SMUX() lastReconnectTry(0), reconnectTimeout(1), sock(-1), - addUserNotifier(*this), - delUserNotifier(*this), - addDelTariffNotifier(*this), - logger(STG::PluginLogger::get("smux")) + logger(PluginLogger::get("smux")) { smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler; smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler; @@ -229,7 +229,7 @@ printfd(__FILE__, "SMUX::Stop() - After\n"); return 0; } -int SMUX::Reload(const STG::ModuleSettings & /*ms*/) +int SMUX::Reload(const ModuleSettings & /*ms*/) { if (Stop() != 0) return -1; @@ -414,23 +414,17 @@ return true; void SMUX::SetNotifier(UserPtr userPtr) { -notifiers.emplace_back(*this, userPtr); -userPtr->GetProperties().tariffName.AddAfterNotifier(¬ifiers.back()); + m_conns.emplace_back( + userPtr->GetID(), + userPtr->GetProperties().tariffName.afterChange([this](const auto&, const auto&){ UpdateTables(); }) + ); } void SMUX::UnsetNotifier(UserPtr userPtr) { -auto it = notifiers.begin(); -while (it != notifiers.end()) - { - if (it->GetUserPtr() == userPtr) - { - userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it)); - notifiers.erase(it); - break; - } - ++it; - } + m_conns.erase(std::remove_if(m_conns.begin(), m_conns.end(), + [userPtr](const auto& c){ return std::get<0>(c) == userPtr->GetID(); }), + m_conns.end()); } void SMUX::SetNotifiers() @@ -444,26 +438,27 @@ while (users->SearchNext(h, &u) == 0) users->CloseSearch(h); -users->AddNotifierUserAdd(&addUserNotifier); -users->AddNotifierUserDel(&delUserNotifier); - -tariffs->AddNotifierAdd(&addDelTariffNotifier); -tariffs->AddNotifierDel(&addDelTariffNotifier); +m_onAddUserConn = users->onAdd([this](auto user){ + SetNotifier(user); + UpdateTables(); +}); +m_onDelUserConn = users->onDel([this](auto user){ + UnsetNotifier(user); + UpdateTables(); +}); + +auto updateTables = [this](const 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(); -users->DelNotifierUserDel(&delUserNotifier); -users->DelNotifierUserAdd(&addUserNotifier); +m_onAddUserConn.disconnect(); +m_onDelUserConn.disconnect(); -auto it = notifiers.begin(); -while (it != notifiers.end()) - { - it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it)); - ++it; - } -notifiers.clear(); +m_conns.clear(); }