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 ADD_DEL_TARIFF_NOTIFIER : public STG::NotifierBase<STG::TariffData> {
86 explicit ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
87 : STG::NotifierBase<STG::TariffData>(), smux(s) {}
88 void notify(const STG::TariffData &) override;
93 //-----------------------------------------------------------------------------
94 class SMUX : public STG::Plugin {
99 void SetUsers(STG::Users * u) { users = u; }
100 void SetTariffs(STG::Tariffs * t) { tariffs = t; }
101 void SetAdmins(STG::Admins * a) { admins = a; }
102 void SetServices(STG::Services * s) { services = s; }
103 void SetTraffcounter(STG::TraffCounter * tc) { traffcounter = tc; }
104 void SetCorporations(STG::Corporations * c) { corporations = c; }
105 void SetSettings(const STG::ModuleSettings & s) { settings = s; }
110 int Reload(const STG::ModuleSettings & ms);
111 bool IsRunning() { return m_thread.joinable() && !stopped; }
113 const std::string & GetStrError() const { return errorStr; }
114 std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
115 uint16_t GetStartPosition() const { return 10; }
116 uint16_t GetStopPosition() const { return 10; }
120 void SetNotifier(UserPtr userPtr);
121 void UnsetNotifier(UserPtr userPtr);
124 SMUX(const SMUX & rvalue);
125 SMUX & operator=(const SMUX & rvalue);
127 void Run(std::stop_token token);
131 bool DispatchPDUs(const SMUX_PDUs_t * pdus);
133 bool CloseHandler(const SMUX_PDUs_t * pdus);
134 bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
135 bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
136 bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
138 bool GetRequestHandler(const PDUs_t * pdus);
139 bool GetNextRequestHandler(const PDUs_t * pdus);
140 bool SetRequestHandler(const PDUs_t * pdus);
143 void ResetNotifiers();
146 STG::Tariffs * tariffs;
147 STG::Admins * admins;
148 STG::Services * services;
149 STG::Corporations * corporations;
150 STG::TraffCounter * traffcounter;
152 mutable std::string errorStr;
153 SMUX_SETTINGS smuxSettings;
154 STG::ModuleSettings settings;
156 std::jthread m_thread;
161 time_t lastReconnectTry;
162 unsigned reconnectTimeout;
166 SMUXHandlers smuxHandlers;
167 PDUsHandlers pdusHandlers;
171 STG::ScopedConnection m_onAddUserConn;
172 STG::ScopedConnection m_onDelUserConn;
174 std::list<CHG_AFTER_NOTIFIER> notifiers;
175 ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
177 STG::PluginLogger logger;
179 //-----------------------------------------------------------------------------
182 void CHG_AFTER_NOTIFIER::notify(const std::string &, const std::string &)
188 void ADD_DEL_TARIFF_NOTIFIER::notify(const STG::TariffData &)