7 #pragma GCC diagnostic push
8 #pragma GCC diagnostic ignored "-Wold-style-cast"
9 #include "stg/SMUX-PDUs.h"
10 #include "stg/ObjectSyntax.h"
11 #pragma GCC diagnostic pop
13 #include "stg/plugin.h"
14 #include "stg/module_settings.h"
15 #include "stg/subscriptions.h"
16 #include "stg/noncopyable.h"
17 #include "stg/logger.h"
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wshadow"
25 #include <jthread.hpp>
26 #pragma GCC diagnostic pop
41 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
42 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
43 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
44 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
46 using UserPtr = User*;
47 //-----------------------------------------------------------------------------
52 virtual ~SMUX_SETTINGS() {}
53 const std::string & GetStrError() const { return errorStr; }
54 int ParseSettings(const ModuleSettings & s);
56 uint32_t GetIP() const { return ip; }
57 uint16_t GetPort() const { return port; }
58 const std::string GetPassword() const { return password; }
61 mutable std::string errorStr;
67 //-----------------------------------------------------------------------------
68 class SMUX : public Plugin
74 void SetUsers(Users * u) { users = u; }
75 void SetTariffs(Tariffs * t) { tariffs = t; }
76 void SetAdmins(Admins * a) { admins = a; }
77 void SetServices(Services * s) { services = s; }
78 void SetTraffcounter(TraffCounter * tc) { traffcounter = tc; }
79 void SetCorporations(Corporations * c) { corporations = c; }
80 void SetSettings(const ModuleSettings & s) { settings = s; }
85 int Reload(const ModuleSettings & ms);
86 bool IsRunning() { return m_thread.joinable() && !stopped; }
88 const std::string & GetStrError() const { return errorStr; }
89 std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
90 uint16_t GetStartPosition() const { return 10; }
91 uint16_t GetStopPosition() const { return 10; }
95 void SetNotifier(UserPtr userPtr);
96 void UnsetNotifier(UserPtr userPtr);
99 SMUX(const SMUX & rvalue);
100 SMUX & operator=(const SMUX & rvalue);
102 void Run(std::stop_token token);
106 bool DispatchPDUs(const SMUX_PDUs_t * pdus);
108 bool CloseHandler(const SMUX_PDUs_t * pdus);
109 bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
110 bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
111 bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
113 bool GetRequestHandler(const PDUs_t * pdus);
114 bool GetNextRequestHandler(const PDUs_t * pdus);
115 bool SetRequestHandler(const PDUs_t * pdus);
118 void ResetNotifiers();
124 Corporations * corporations;
125 TraffCounter * traffcounter;
127 mutable std::string errorStr;
128 SMUX_SETTINGS smuxSettings;
129 ModuleSettings settings;
131 std::jthread m_thread;
136 time_t lastReconnectTry;
137 unsigned reconnectTimeout;
141 SMUXHandlers smuxHandlers;
142 PDUsHandlers pdusHandlers;
146 ScopedConnection m_onAddUserConn;
147 ScopedConnection m_onDelUserConn;
148 ScopedConnection m_onAddTariffConn;
149 ScopedConnection m_onDelTariffConn;
151 using ConnHolder = std::tuple<int, ScopedConnection>;
152 std::vector<ConnHolder> m_conns;