7 #include "stg/SMUX-PDUs.h"
8 #include "stg/ObjectSyntax.h"
10 #include "stg/plugin.h"
11 #include "stg/module_settings.h"
12 #include "stg/subscriptions.h"
13 #include "stg/noncopyable.h"
14 #include "stg/logger.h"
20 #pragma GCC diagnostic push
21 #pragma GCC diagnostic ignored "-Wshadow"
22 #include <jthread.hpp>
23 #pragma GCC diagnostic pop
38 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
39 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
40 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
41 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
43 using UserPtr = User*;
44 //-----------------------------------------------------------------------------
49 virtual ~SMUX_SETTINGS() {}
50 const std::string & GetStrError() const { return errorStr; }
51 int ParseSettings(const ModuleSettings & s);
53 uint32_t GetIP() const { return ip; }
54 uint16_t GetPort() const { return port; }
55 const std::string GetPassword() const { return password; }
58 mutable std::string errorStr;
64 //-----------------------------------------------------------------------------
65 class SMUX : public Plugin
71 void SetUsers(Users * u) { users = u; }
72 void SetTariffs(Tariffs * t) { tariffs = t; }
73 void SetAdmins(Admins * a) { admins = a; }
74 void SetServices(Services * s) { services = s; }
75 void SetTraffcounter(TraffCounter * tc) { traffcounter = tc; }
76 void SetCorporations(Corporations * c) { corporations = c; }
77 void SetSettings(const ModuleSettings & s) { settings = s; }
82 int Reload(const ModuleSettings & ms);
83 bool IsRunning() { return m_thread.joinable() && !stopped; }
85 const std::string & GetStrError() const { return errorStr; }
86 std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
87 uint16_t GetStartPosition() const { return 10; }
88 uint16_t GetStopPosition() const { return 10; }
92 void SetNotifier(UserPtr userPtr);
93 void UnsetNotifier(UserPtr userPtr);
96 SMUX(const SMUX & rvalue);
97 SMUX & operator=(const SMUX & rvalue);
99 void Run(std::stop_token token);
103 bool DispatchPDUs(const SMUX_PDUs_t * pdus);
105 bool CloseHandler(const SMUX_PDUs_t * pdus);
106 bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
107 bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
108 bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
110 bool GetRequestHandler(const PDUs_t * pdus);
111 bool GetNextRequestHandler(const PDUs_t * pdus);
112 bool SetRequestHandler(const PDUs_t * pdus);
115 void ResetNotifiers();
121 Corporations * corporations;
122 TraffCounter * traffcounter;
124 mutable std::string errorStr;
125 SMUX_SETTINGS smuxSettings;
126 ModuleSettings settings;
128 std::jthread m_thread;
133 time_t lastReconnectTry;
134 unsigned reconnectTimeout;
138 SMUXHandlers smuxHandlers;
139 PDUsHandlers pdusHandlers;
143 ScopedConnection m_onAddUserConn;
144 ScopedConnection m_onDelUserConn;
145 ScopedConnection m_onAddTariffConn;
146 ScopedConnection m_onDelTariffConn;
148 using ConnHolder = std::tuple<int, ScopedConnection>;
149 std::vector<ConnHolder> m_conns;