]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Use std::jthread and C++17.
[stg.git] / projects / stargazer / plugins / other / smux / smux.h
1 #ifndef __SMUX_H__
2 #define __SMUX_H__
3
4 #include <string>
5 #include <map>
6 #include <list>
7 #include <mutex>
8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wshadow"
10 #include <jthread.hpp>
11 #pragma GCC diagnostic pop
12 #include <cstdint>
13
14 #include "stg/SMUX-PDUs.h"
15 #include "stg/ObjectSyntax.h"
16
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"
22
23 #include "sensors.h"
24 #include "tables.h"
25 #include "types.h"
26
27 namespace STG
28 {
29 struct User;
30 struct Settings;
31 struct Users;
32 struct Tariffs;
33 struct Services;
34 struct Corporations;
35 struct TraffCounter;
36 }
37
38 class SMUX;
39
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;
44
45 using UserPtr = STG::User*;
46 //-----------------------------------------------------------------------------
47 class SMUX_SETTINGS {
48 public:
49     SMUX_SETTINGS();
50     virtual ~SMUX_SETTINGS() {}
51     const std::string & GetStrError() const { return errorStr; }
52     int ParseSettings(const STG::ModuleSettings & s);
53
54     uint32_t GetIP() const { return ip; }
55     uint16_t GetPort() const { return port; }
56     const std::string GetPassword() const { return password; }
57
58 private:
59     mutable std::string errorStr;
60
61     uint32_t ip;
62     uint16_t port;
63     std::string password;
64 };
65 //-----------------------------------------------------------------------------
66 class CHG_AFTER_NOTIFIER : public STG::PropertyNotifierBase<std::string> {
67 public:
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 &);
75
76     UserPtr GetUserPtr() const { return userPtr; }
77
78 private:
79     CHG_AFTER_NOTIFIER & operator=(const CHG_AFTER_NOTIFIER & rvalue);
80     SMUX & smux;
81     UserPtr userPtr;
82 };
83 //-----------------------------------------------------------------------------
84 class ADD_DEL_TARIFF_NOTIFIER : public STG::NotifierBase<STG::TariffData> {
85 public:
86     explicit ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
87              : STG::NotifierBase<STG::TariffData>(), smux(s) {}
88     void Notify(const STG::TariffData &);
89
90 private:
91     SMUX & smux;
92 };
93 //-----------------------------------------------------------------------------
94 class ADD_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
95 public:
96     explicit ADD_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<STG::User*>(), smux(s) {}
97     void Notify(const UserPtr &);
98
99 private:
100     SMUX & smux;
101 };
102 //-----------------------------------------------------------------------------
103 class DEL_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
104 public:
105     explicit DEL_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<UserPtr>(), smux(s) {}
106     void Notify(const UserPtr &);
107
108 private:
109     SMUX & smux;
110 };
111 //-----------------------------------------------------------------------------
112 class SMUX : public STG::Plugin {
113 public:
114     SMUX();
115     virtual ~SMUX();
116
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; }
124     int ParseSettings();
125
126     int Start();
127     int Stop();
128     int Reload(const STG::ModuleSettings & ms);
129     bool IsRunning() { return m_thread.joinable() && !stopped; }
130
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; }
135
136     bool UpdateTables();
137
138     void SetNotifier(UserPtr userPtr);
139     void UnsetNotifier(UserPtr userPtr);
140
141 private:
142     SMUX(const SMUX & rvalue);
143     SMUX & operator=(const SMUX & rvalue);
144
145     void Run(std::stop_token token);
146     bool PrepareNet();
147     bool Reconnect();
148
149     bool DispatchPDUs(const SMUX_PDUs_t * pdus);
150
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);
155
156     bool GetRequestHandler(const PDUs_t * pdus);
157     bool GetNextRequestHandler(const PDUs_t * pdus);
158     bool SetRequestHandler(const PDUs_t * pdus);
159
160     void SetNotifiers();
161     void ResetNotifiers();
162
163     STG::Users * users;
164     STG::Tariffs * tariffs;
165     STG::Admins * admins;
166     STG::Services * services;
167     STG::Corporations * corporations;
168     STG::TraffCounter * traffcounter;
169
170     mutable std::string errorStr;
171     SMUX_SETTINGS smuxSettings;
172     STG::ModuleSettings settings;
173
174     std::jthread m_thread;
175     std::mutex m_mutex;
176     bool stopped;
177     bool needReconnect;
178
179     time_t lastReconnectTry;
180     unsigned reconnectTimeout;
181
182     int sock;
183
184     SMUXHandlers smuxHandlers;
185     PDUsHandlers pdusHandlers;
186     Sensors sensors;
187     Tables tables;
188
189     std::list<CHG_AFTER_NOTIFIER> notifiers;
190     ADD_USER_NOTIFIER addUserNotifier;
191     DEL_USER_NOTIFIER delUserNotifier;
192     ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
193
194     STG::PluginLogger logger;
195 };
196 //-----------------------------------------------------------------------------
197
198 inline
199 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
200 {
201 smux.UpdateTables();
202 }
203
204 inline
205 void ADD_DEL_TARIFF_NOTIFIER::Notify(const STG::TariffData &)
206 {
207 smux.UpdateTables();
208 }
209
210 inline
211 void ADD_USER_NOTIFIER::Notify(const UserPtr & userPtr)
212 {
213 smux.SetNotifier(userPtr);
214 smux.UpdateTables();
215 }
216
217 inline
218 void DEL_USER_NOTIFIER::Notify(const UserPtr & userPtr)
219 {
220 smux.UnsetNotifier(userPtr);
221 smux.UpdateTables();
222 }
223
224 #endif