8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wshadow"
10 #include <jthread.hpp>
11 #pragma GCC diagnostic pop
14 #include "stg/SMUX-PDUs.h"
15 #include "stg/ObjectSyntax.h"
17 #include "stg/plugin.h"
18 #include "stg/module_settings.h"
19 #include "stg/notifer.h"
20 #include "stg/noncopyable.h"
21 #include "stg/logger.h"
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 ADD_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
96 explicit ADD_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<STG::User*>(), smux(s) {}
97 void notify(const UserPtr &) override;
102 //-----------------------------------------------------------------------------
103 class DEL_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
105 explicit DEL_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<UserPtr>(), smux(s) {}
106 void notify(const UserPtr &) override;
111 //-----------------------------------------------------------------------------
112 class SMUX : public STG::Plugin {
117 void SetUsers(STG::Users * u) { users = u; }
118 void SetTariffs(STG::Tariffs * t) { tariffs = t; }
119 void SetAdmins(STG::Admins * a) { admins = a; }
120 void SetServices(STG::Services * s) { services = s; }
121 void SetTraffcounter(STG::TraffCounter * tc) { traffcounter = tc; }
122 void SetCorporations(STG::Corporations * c) { corporations = c; }
123 void SetSettings(const STG::ModuleSettings & s) { settings = s; }
128 int Reload(const STG::ModuleSettings & ms);
129 bool IsRunning() { return m_thread.joinable() && !stopped; }
131 const std::string & GetStrError() const { return errorStr; }
132 std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
133 uint16_t GetStartPosition() const { return 10; }
134 uint16_t GetStopPosition() const { return 10; }
138 void SetNotifier(UserPtr userPtr);
139 void UnsetNotifier(UserPtr userPtr);
142 SMUX(const SMUX & rvalue);
143 SMUX & operator=(const SMUX & rvalue);
145 void Run(std::stop_token token);
149 bool DispatchPDUs(const SMUX_PDUs_t * pdus);
151 bool CloseHandler(const SMUX_PDUs_t * pdus);
152 bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
153 bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
154 bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
156 bool GetRequestHandler(const PDUs_t * pdus);
157 bool GetNextRequestHandler(const PDUs_t * pdus);
158 bool SetRequestHandler(const PDUs_t * pdus);
161 void ResetNotifiers();
164 STG::Tariffs * tariffs;
165 STG::Admins * admins;
166 STG::Services * services;
167 STG::Corporations * corporations;
168 STG::TraffCounter * traffcounter;
170 mutable std::string errorStr;
171 SMUX_SETTINGS smuxSettings;
172 STG::ModuleSettings settings;
174 std::jthread m_thread;
179 time_t lastReconnectTry;
180 unsigned reconnectTimeout;
184 SMUXHandlers smuxHandlers;
185 PDUsHandlers pdusHandlers;
189 std::list<CHG_AFTER_NOTIFIER> notifiers;
190 ADD_USER_NOTIFIER addUserNotifier;
191 DEL_USER_NOTIFIER delUserNotifier;
192 ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
194 STG::PluginLogger logger;
196 //-----------------------------------------------------------------------------
199 void CHG_AFTER_NOTIFIER::notify(const std::string &, const std::string &)
205 void ADD_DEL_TARIFF_NOTIFIER::notify(const STG::TariffData &)
211 void ADD_USER_NOTIFIER::notify(const UserPtr & userPtr)
213 smux.SetNotifier(userPtr);
218 void DEL_USER_NOTIFIER::notify(const UserPtr & userPtr)
220 smux.UnsetNotifier(userPtr);