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/notifer.h"
14 #include "stg/noncopyable.h"
15 #include "stg/logger.h"
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wshadow"
23 #include <jthread.hpp>
24 #pragma GCC diagnostic pop
40 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
41 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
42 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
43 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
45 using UserPtr = STG::User*;
46 //-----------------------------------------------------------------------------
50 virtual ~SMUX_SETTINGS() {}
51 const std::string & GetStrError() const { return errorStr; }
52 int ParseSettings(const STG::ModuleSettings & s);
54 uint32_t GetIP() const { return ip; }
55 uint16_t GetPort() const { return port; }
56 const std::string GetPassword() const { return password; }
59 mutable std::string errorStr;
65 //-----------------------------------------------------------------------------
66 class CHG_AFTER_NOTIFIER : public STG::PropertyNotifierBase<std::string> {
68 CHG_AFTER_NOTIFIER(SMUX & s, const UserPtr & u)
69 : STG::PropertyNotifierBase<std::string>(),
70 smux(s), userPtr(u) {}
71 CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER & rvalue)
72 : STG::PropertyNotifierBase<std::string>(),
73 smux(rvalue.smux), userPtr(rvalue.userPtr) {}
74 void notify(const std::string &, const std::string &) override;
76 UserPtr GetUserPtr() const { return userPtr; }
79 CHG_AFTER_NOTIFIER & operator=(const CHG_AFTER_NOTIFIER & rvalue);
83 //-----------------------------------------------------------------------------
84 class SMUX : public STG::Plugin {
89 void SetUsers(STG::Users * u) { users = u; }
90 void SetTariffs(STG::Tariffs * t) { tariffs = t; }
91 void SetAdmins(STG::Admins * a) { admins = a; }
92 void SetServices(STG::Services * s) { services = s; }
93 void SetTraffcounter(STG::TraffCounter * tc) { traffcounter = tc; }
94 void SetCorporations(STG::Corporations * c) { corporations = c; }
95 void SetSettings(const STG::ModuleSettings & s) { settings = s; }
100 int Reload(const STG::ModuleSettings & ms);
101 bool IsRunning() { return m_thread.joinable() && !stopped; }
103 const std::string & GetStrError() const { return errorStr; }
104 std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
105 uint16_t GetStartPosition() const { return 10; }
106 uint16_t GetStopPosition() const { return 10; }
110 void SetNotifier(UserPtr userPtr);
111 void UnsetNotifier(UserPtr userPtr);
114 SMUX(const SMUX & rvalue);
115 SMUX & operator=(const SMUX & rvalue);
117 void Run(std::stop_token token);
121 bool DispatchPDUs(const SMUX_PDUs_t * pdus);
123 bool CloseHandler(const SMUX_PDUs_t * pdus);
124 bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
125 bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
126 bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
128 bool GetRequestHandler(const PDUs_t * pdus);
129 bool GetNextRequestHandler(const PDUs_t * pdus);
130 bool SetRequestHandler(const PDUs_t * pdus);
133 void ResetNotifiers();
136 STG::Tariffs * tariffs;
137 STG::Admins * admins;
138 STG::Services * services;
139 STG::Corporations * corporations;
140 STG::TraffCounter * traffcounter;
142 mutable std::string errorStr;
143 SMUX_SETTINGS smuxSettings;
144 STG::ModuleSettings settings;
146 std::jthread m_thread;
151 time_t lastReconnectTry;
152 unsigned reconnectTimeout;
156 SMUXHandlers smuxHandlers;
157 PDUsHandlers pdusHandlers;
161 STG::ScopedConnection m_onAddUserConn;
162 STG::ScopedConnection m_onDelUserConn;
163 STG::ScopedConnection m_onAddTariffConn;
164 STG::ScopedConnection m_onDelTariffConn;
166 std::list<CHG_AFTER_NOTIFIER> notifiers;
168 STG::PluginLogger logger;
170 //-----------------------------------------------------------------------------
173 void CHG_AFTER_NOTIFIER::notify(const std::string &, const std::string &)