-#include <sys/types.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
+#include "smux.h"
+#include "utils.h"
-#include <cstring>
-#include <cerrno>
-#include <ctime>
-#include <csignal>
-#include <cassert>
+#include "stg/common.h"
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <utility>
-#include "stg/common.h"
+#include <cstring>
+#include <cerrno>
+#include <ctime>
+#include <csignal>
+#include <cassert>
-#include "smux.h"
-#include "utils.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+using STG::SMUX;
+using STG::SMUX_SETTINGS;
namespace
{
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";
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;
return 0;
}
-int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
+int SMUX::Reload(const ModuleSettings & /*ms*/)
{
if (Stop() != 0)
return -1;
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()
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();
}