]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/smux/smux.cpp
More std::jthread
[stg.git] / projects / stargazer / plugins / other / smux / smux.cpp
index 09cb4636795b1f72d567dc6274e2d74f788a2fad..e0ddfffcb56e251fc84b0454fda065b6a9797930 100644 (file)
 #include <utility>
 
 #include "stg/common.h"
-#include "stg/plugin_creator.h"
 
 #include "smux.h"
 #include "utils.h"
 
 namespace
 {
-PLUGIN_CREATOR<SMUX> smc;
-
-bool SPrefixLess(const Sensors::value_type & a,
-                 const Sensors::value_type & b);
 
 bool SPrefixLess(const Sensors::value_type & a,
                  const Sensors::value_type & b)
@@ -35,11 +30,10 @@ return a.first.PrefixLess(b.first);
 
 }
 
-extern "C" PLUGIN * GetPlugin();
-
-PLUGIN * GetPlugin()
+extern "C" STG::Plugin* GetPlugin()
 {
-return smc.GetPlugin();
+    static SMUX plugin;
+    return &plugin;
 }
 
 SMUX_SETTINGS::SMUX_SETTINGS()
@@ -49,15 +43,15 @@ SMUX_SETTINGS::SMUX_SETTINGS()
       password()
 {}
 
-int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
+int SMUX_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
 {
-PARAM_VALUE pv;
-std::vector<PARAM_VALUE>::const_iterator pvi;
+STG::ParamValue pv;
+std::vector<STG::ParamValue>::const_iterator pvi;
 int p;
 
 pv.param = "Port";
 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
+if (pvi == s.moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'Port\' not found.";
     printfd(__FILE__, "Parameter 'Port' not found\n");
@@ -73,7 +67,7 @@ port = static_cast<uint16_t>(p);
 
 pv.param = "Password";
 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
+if (pvi == s.moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'Password\' not found.";
     printfd(__FILE__, "Parameter 'Password' not found\n");
@@ -86,7 +80,7 @@ else
 
 pv.param = "Server";
 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
+if (pvi == s.moduleParams.end() || pvi->value.empty())
     {
     errorStr = "Parameter \'Server\' not found.";
     printfd(__FILE__, "Parameter 'Server' not found\n");
@@ -98,36 +92,22 @@ return 0;
 }
 
 SMUX::SMUX()
-    : PLUGIN(),
-      users(NULL),
+    : users(NULL),
       tariffs(NULL),
       admins(NULL),
       services(NULL),
       corporations(NULL),
       traffcounter(NULL),
-      errorStr(),
-      smuxSettings(),
-      settings(),
-      thread(),
-      mutex(),
-      running(false),
       stopped(true),
       needReconnect(false),
       lastReconnectTry(0),
       reconnectTimeout(1),
       sock(-1),
-      smuxHandlers(),
-      pdusHandlers(),
-      sensors(),
-      tables(),
-      notifiers(),
       addUserNotifier(*this),
       delUserNotifier(*this),
       addDelTariffNotifier(*this),
-      logger(GetPluginLogger(GetStgLogger(), "smux"))
+      logger(STG::PluginLogger::get("smux"))
 {
-pthread_mutex_init(&mutex, NULL);
-
 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
@@ -151,7 +131,6 @@ SMUX::~SMUX()
         delete it->second;
     }
 printfd(__FILE__, "SMUX::~SMUX()\n");
-pthread_mutex_destroy(&mutex);
 }
 
 int SMUX::ParseSettings()
@@ -212,16 +191,8 @@ while (it != sensors.end())
     }
 #endif
 
-if (!running)
-    {
-    if (pthread_create(&thread, NULL, Runner, this))
-        {
-        errorStr = "Cannot create thread.";
-       logger("Cannot create thread.");
-        printfd(__FILE__, "Cannot create thread\n");
-        return -1;
-        }
-    }
+if (!m_thread.joinable())
+    m_thread = std::jthread([this](auto token){ Run(token); });
 
 return 0;
 }
@@ -229,7 +200,7 @@ return 0;
 int SMUX::Stop()
 {
 printfd(__FILE__, "SMUX::Stop() - Before\n");
-running = false;
+m_thread.request_stop();
 
 if (!stopped)
     {
@@ -241,8 +212,10 @@ if (!stopped)
         }
     }
 
-if (stopped)
-    pthread_join(thread, NULL);
+if (!stopped)
+    m_thread.detach();
+else
+    m_thread.join();
 
 ResetNotifiers();
 
@@ -264,7 +237,6 @@ close(sock);
 
 if (!stopped)
     {
-    running = true;
     return -1;
     }
 
@@ -272,7 +244,7 @@ printfd(__FILE__, "SMUX::Stop() - After\n");
 return 0;
 }
 
-int SMUX::Reload()
+int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
 {
 if (Stop())
     return -1;
@@ -286,30 +258,16 @@ if (!needReconnect)
 return 0;
 }
 
-void * SMUX::Runner(void * d)
-{
-sigset_t signalSet;
-sigfillset(&signalSet);
-pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
-
-SMUX * smux = static_cast<SMUX *>(d);
-
-smux->Run();
-
-return NULL;
-}
-
-void SMUX::Run()
+void SMUX::Run(std::stop_token token)
 {
 stopped = true;
 if (!SendOpenPDU(sock))
     needReconnect = true;
 if (!SendRReqPDU(sock))
     needReconnect = true;
-running = true;
 stopped = false;
 
-while(running)
+while (!token.stop_requested())
     {
     if (WaitPackets(sock) && !needReconnect)
         {
@@ -319,12 +277,12 @@ while(running)
             DispatchPDUs(pdus);
             ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
             }
-        else if (running)
+        else if (!token.stop_requested())
             Reconnect();
         }
-    else if (running && needReconnect)
+    else if (!token.stop_requested() && needReconnect)
         Reconnect();
-    if (!running)
+    if (token.stop_requested())
         break;
     }
 SendClosePDU(sock);
@@ -440,11 +398,11 @@ while (it != tables.end())
     }
 if (!done)
     {
-    Sensors::iterator it(newSensors.begin());
-    while (it != newSensors.end())
+    Sensors::iterator sit(newSensors.begin());
+    while (sit != newSensors.end())
         {
-        delete it->second;
-        ++it;
+        delete sit->second;
+        ++sit;
         }
     return false;
     }
@@ -472,20 +430,20 @@ sensors.insert(newSensors.begin(), newSensors.end());
 return true;
 }
 
-void SMUX::SetNotifier(USER_PTR userPtr)
+void SMUX::SetNotifier(UserPtr userPtr)
 {
 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
-userPtr->GetProperty().tariffName.AddAfterNotifier(&notifiers.back());
+userPtr->GetProperties().tariffName.AddAfterNotifier(&notifiers.back());
 }
 
-void SMUX::UnsetNotifier(USER_PTR userPtr)
+void SMUX::UnsetNotifier(UserPtr userPtr)
 {
 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
 while (it != notifiers.end())
     {
     if (it->GetUserPtr() == userPtr)
         {
-        userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
+        userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it));
         notifiers.erase(it);
         break;
         }
@@ -498,7 +456,7 @@ void SMUX::SetNotifiers()
 int h = users->OpenSearch();
 assert(h && "USERS::OpenSearch is always correct");
 
-USER_PTR u;
+UserPtr u;
 while (!users->SearchNext(h, &u))
     SetNotifier(u);
 
@@ -522,7 +480,7 @@ users->DelNotifierUserAdd(&addUserNotifier);
 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
 while (it != notifiers.end())
     {
-    it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
+    it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it));
     ++it;
     }
 notifiers.clear();