X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/bc6cac0e474dfe2feb4983aef98f99e23a98ffc4..refs/heads/master:/projects/stargazer/plugins/other/rscript/rscript.cpp diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 7f0f4bc4..24a8bd71 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -24,7 +24,6 @@ #include "ur_functor.h" #include "stg/common.h" -#include "stg/locker.h" #include "stg/users.h" #include "stg/user_property.h" #include "stg/logger.h" @@ -45,21 +44,9 @@ extern volatile time_t stgTime; +namespace RS = STG::RS; using RS::REMOTE_SCRIPT; -namespace { - -template -struct USER_IS -{ - explicit USER_IS(RS::UserPtr u) : user(u) {} - bool operator()(const T & notifier) { return notifier.GetUser() == user; } - - RS::UserPtr user; -}; - -} // namespace anonymous - extern "C" STG::Plugin* GetPlugin() { static REMOTE_SCRIPT plugin; @@ -74,10 +61,10 @@ RS::SETTINGS::SETTINGS() { } //----------------------------------------------------------------------------- -int RS::SETTINGS::ParseSettings(const STG::ModuleSettings & s) +int RS::SETTINGS::ParseSettings(const ModuleSettings & s) { int p; -STG::ParamValue pv; +ParamValue pv; netRouters.clear(); /////////////////////////// pv.param = "Port"; @@ -147,7 +134,7 @@ NRMapParser nrMapParser; if (!nrMapParser.ReadFile(subnetFile)) netRouters = nrMapParser.GetMap(); else - STG::PluginLogger::get("rscript")("mod_rscript: error opening subnets file '%s'", subnetFile.c_str()); + PluginLogger::get("rscript")("mod_rscript: error opening subnets file '%s'", subnetFile.c_str()); return 0; } @@ -160,9 +147,7 @@ REMOTE_SCRIPT::REMOTE_SCRIPT() isRunning(false), users(nullptr), sock(0), - onAddUserNotifier(*this), - onDelUserNotifier(*this), - logger(STG::PluginLogger::get("rscript")) + logger(PluginLogger::get("rscript")) { } //----------------------------------------------------------------------------- @@ -201,8 +186,8 @@ netRouters = rsSettings.GetSubnetsMap(); InitEncrypt(rsSettings.GetPassword()); -users->AddNotifierUserAdd(&onAddUserNotifier); -users->AddNotifierUserDel(&onDelUserNotifier); +m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); }); +m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); }); if (GetUsers()) return -1; @@ -225,10 +210,10 @@ if (!IsRunning()) m_thread.request_stop(); std::for_each( - authorizedUsers.begin(), - authorizedUsers.end(), - DisconnectUser(*this) - ); + authorizedUsers.begin(), + authorizedUsers.end(), + [this](auto& kv){ Send(kv.second, true); } +); FinalizeNet(); @@ -242,9 +227,6 @@ if (isRunning) } } -users->DelNotifierUserDel(&onDelUserNotifier); -users->DelNotifierUserAdd(&onAddUserNotifier); - if (isRunning) { logger("Cannot stop thread."); @@ -256,7 +238,7 @@ else return 0; } //----------------------------------------------------------------------------- -int REMOTE_SCRIPT::Reload(const STG::ModuleSettings & /*ms*/) +int REMOTE_SCRIPT::Reload(const ModuleSettings & /*ms*/) { NRMapParser nrMapParser; @@ -474,20 +456,18 @@ return {}; //----------------------------------------------------------------------------- void REMOTE_SCRIPT::SetUserNotifiers(UserPtr u) { -ipNotifierList.push_front(RS::IP_NOTIFIER(*this, u)); -connNotifierList.push_front(RS::CONNECTED_NOTIFIER(*this, u)); + m_conns.emplace_back( + u->GetID(), + u->afterCurrIPChange([this, u](auto, auto newVal){ addDelUser(u, newVal != 0); }), + u->afterConnectedChange([this, u](auto, auto newVal){ addDelUser(u, newVal); }) + ); } //----------------------------------------------------------------------------- void REMOTE_SCRIPT::UnSetUserNotifiers(UserPtr u) { -ipNotifierList.erase(std::remove_if(ipNotifierList.begin(), - ipNotifierList.end(), - USER_IS(u)), - ipNotifierList.end()); -connNotifierList.erase(std::remove_if(connNotifierList.begin(), - connNotifierList.end(), - USER_IS(u)), - connNotifierList.end()); + m_conns.erase(std::remove_if(m_conns.begin(), m_conns.end(), + [u](const auto& c){ return std::get<0>(c) == u->GetID(); }), + m_conns.end()); } //----------------------------------------------------------------------------- @@ -522,20 +502,12 @@ if (it != authorizedUsers.end()) }*/ } //----------------------------------------------------------------------------- -void RS::IP_NOTIFIER::notify(const uint32_t & /*oldValue*/, const uint32_t & newValue) +void REMOTE_SCRIPT::addDelUser(UserPtr user, bool toAdd) { -if (newValue != 0) - rs.AddRSU(user); -else - rs.DelRSU(user); -} -//----------------------------------------------------------------------------- -void RS::CONNECTED_NOTIFIER::notify(const bool & /*oldValue*/, const bool & newValue) -{ -if (newValue) - rs.AddRSU(user); -else - rs.DelRSU(user); + if (toAdd) + AddRSU(user); + else + DelRSU(user); } //----------------------------------------------------------------------------- void REMOTE_SCRIPT::InitEncrypt(const std::string & password) const