]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/smux/smux.h
Start replacing notifiers with subscriptions.
[stg.git] / projects / stargazer / plugins / other / smux / smux.h
index 4954bd093663eeb11f9fe70bafeedeebc6740af8..1986d28be72ce746b02327e353c90ce88fdc8f1a 100644 (file)
@@ -1,25 +1,28 @@
-#ifndef __SMUX_H__
-#define __SMUX_H__
+#pragma once
 
-#include <pthread.h>
-
-#include <string>
-#include <map>
-#include <list>
-#include <cstdint>
+#include "sensors.h"
+#include "tables.h"
+#include "types.h"
 
 #include "stg/SMUX-PDUs.h"
 #include "stg/ObjectSyntax.h"
 
 #include "stg/plugin.h"
 #include "stg/module_settings.h"
+#include "stg/subscriptions.h"
 #include "stg/notifer.h"
 #include "stg/noncopyable.h"
 #include "stg/logger.h"
 
-#include "sensors.h"
-#include "tables.h"
-#include "types.h"
+#include <string>
+#include <map>
+#include <list>
+#include <mutex>
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <jthread.hpp>
+#pragma GCC diagnostic pop
+#include <cstdint>
 
 namespace STG
 {
@@ -68,7 +71,7 @@ public:
              CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER & rvalue)
                  : STG::PropertyNotifierBase<std::string>(),
                    smux(rvalue.smux), userPtr(rvalue.userPtr) {}
-    void     Notify(const std::string &, const std::string &);
+    void     notify(const std::string &, const std::string &) override;
 
     UserPtr GetUserPtr() const { return userPtr; }
 
@@ -82,25 +85,7 @@ class ADD_DEL_TARIFF_NOTIFIER : public STG::NotifierBase<STG::TariffData> {
 public:
     explicit ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
              : STG::NotifierBase<STG::TariffData>(), smux(s) {}
-    void Notify(const STG::TariffData &);
-
-private:
-    SMUX & smux;
-};
-//-----------------------------------------------------------------------------
-class ADD_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
-public:
-    explicit ADD_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<STG::User*>(), smux(s) {}
-    void Notify(const UserPtr &);
-
-private:
-    SMUX & smux;
-};
-//-----------------------------------------------------------------------------
-class DEL_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
-public:
-    explicit DEL_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<UserPtr>(), smux(s) {}
-    void Notify(const UserPtr &);
+    void notify(const STG::TariffData &) override;
 
 private:
     SMUX & smux;
@@ -123,7 +108,7 @@ public:
     int Start();
     int Stop();
     int Reload(const STG::ModuleSettings & ms);
-    bool IsRunning() { return running && !stopped; }
+    bool IsRunning() { return m_thread.joinable() && !stopped; }
 
     const std::string & GetStrError() const { return errorStr; }
     std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
@@ -139,8 +124,7 @@ private:
     SMUX(const SMUX & rvalue);
     SMUX & operator=(const SMUX & rvalue);
 
-    static void * Runner(void * d);
-    void Run();
+    void Run(std::stop_token token);
     bool PrepareNet();
     bool Reconnect();
 
@@ -169,9 +153,8 @@ private:
     SMUX_SETTINGS smuxSettings;
     STG::ModuleSettings settings;
 
-    pthread_t thread;
-    pthread_mutex_t mutex;
-    bool running;
+    std::jthread m_thread;
+    std::mutex m_mutex;
     bool stopped;
     bool needReconnect;
 
@@ -185,9 +168,10 @@ private:
     Sensors sensors;
     Tables tables;
 
+    STG::ScopedConnection m_onAddUserConn;
+    STG::ScopedConnection m_onDelUserConn;
+
     std::list<CHG_AFTER_NOTIFIER> notifiers;
-    ADD_USER_NOTIFIER addUserNotifier;
-    DEL_USER_NOTIFIER delUserNotifier;
     ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
 
     STG::PluginLogger logger;
@@ -195,29 +179,13 @@ private:
 //-----------------------------------------------------------------------------
 
 inline
-void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
+void CHG_AFTER_NOTIFIER::notify(const std::string &, const std::string &)
 {
 smux.UpdateTables();
 }
 
 inline
-void ADD_DEL_TARIFF_NOTIFIER::Notify(const STG::TariffData &)
+void ADD_DEL_TARIFF_NOTIFIER::notify(const STG::TariffData &)
 {
 smux.UpdateTables();
 }
-
-inline
-void ADD_USER_NOTIFIER::Notify(const UserPtr & userPtr)
-{
-smux.SetNotifier(userPtr);
-smux.UpdateTables();
-}
-
-inline
-void DEL_USER_NOTIFIER::Notify(const UserPtr & userPtr)
-{
-smux.UnsetNotifier(userPtr);
-smux.UpdateTables();
-}
-
-#endif