]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/smux/smux.cpp
Fight CLang warnings.
[stg.git] / projects / stargazer / plugins / other / smux / smux.cpp
index f79085dff1250bc3978420ac35975a06cf05e0dd..a6ab83597037d86374b8150da22d1ceb7fc7c953 100644 (file)
@@ -1,12 +1,7 @@
-#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
 {
@@ -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(&notifiers.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();
 }