]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Start replacing notifiers with subscriptions.
[stg.git] / projects / stargazer / plugins / other / smux / smux.h
1 #pragma once
2
3 #include "sensors.h"
4 #include "tables.h"
5 #include "types.h"
6
7 #include "stg/SMUX-PDUs.h"
8 #include "stg/ObjectSyntax.h"
9
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"
16
17 #include <string>
18 #include <map>
19 #include <list>
20 #include <mutex>
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wshadow"
23 #include <jthread.hpp>
24 #pragma GCC diagnostic pop
25 #include <cstdint>
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 &) override;
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 &) override;
89
90 private:
91     SMUX & smux;
92 };
93 //-----------------------------------------------------------------------------
94 class SMUX : public STG::Plugin {
95 public:
96     SMUX();
97     virtual ~SMUX();
98
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; }
106     int ParseSettings();
107
108     int Start();
109     int Stop();
110     int Reload(const STG::ModuleSettings & ms);
111     bool IsRunning() { return m_thread.joinable() && !stopped; }
112
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; }
117
118     bool UpdateTables();
119
120     void SetNotifier(UserPtr userPtr);
121     void UnsetNotifier(UserPtr userPtr);
122
123 private:
124     SMUX(const SMUX & rvalue);
125     SMUX & operator=(const SMUX & rvalue);
126
127     void Run(std::stop_token token);
128     bool PrepareNet();
129     bool Reconnect();
130
131     bool DispatchPDUs(const SMUX_PDUs_t * pdus);
132
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);
137
138     bool GetRequestHandler(const PDUs_t * pdus);
139     bool GetNextRequestHandler(const PDUs_t * pdus);
140     bool SetRequestHandler(const PDUs_t * pdus);
141
142     void SetNotifiers();
143     void ResetNotifiers();
144
145     STG::Users * users;
146     STG::Tariffs * tariffs;
147     STG::Admins * admins;
148     STG::Services * services;
149     STG::Corporations * corporations;
150     STG::TraffCounter * traffcounter;
151
152     mutable std::string errorStr;
153     SMUX_SETTINGS smuxSettings;
154     STG::ModuleSettings settings;
155
156     std::jthread m_thread;
157     std::mutex m_mutex;
158     bool stopped;
159     bool needReconnect;
160
161     time_t lastReconnectTry;
162     unsigned reconnectTimeout;
163
164     int sock;
165
166     SMUXHandlers smuxHandlers;
167     PDUsHandlers pdusHandlers;
168     Sensors sensors;
169     Tables tables;
170
171     STG::ScopedConnection m_onAddUserConn;
172     STG::ScopedConnection m_onDelUserConn;
173
174     std::list<CHG_AFTER_NOTIFIER> notifiers;
175     ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
176
177     STG::PluginLogger logger;
178 };
179 //-----------------------------------------------------------------------------
180
181 inline
182 void CHG_AFTER_NOTIFIER::notify(const std::string &, const std::string &)
183 {
184 smux.UpdateTables();
185 }
186
187 inline
188 void ADD_DEL_TARIFF_NOTIFIER::notify(const STG::TariffData &)
189 {
190 smux.UpdateTables();
191 }