]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Fight CLang warnings.
[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 #pragma GCC diagnostic push
8 #pragma GCC diagnostic ignored "-Wold-style-cast"
9 #include "stg/SMUX-PDUs.h"
10 #include "stg/ObjectSyntax.h"
11 #pragma GCC diagnostic pop
12
13 #include "stg/plugin.h"
14 #include "stg/module_settings.h"
15 #include "stg/subscriptions.h"
16 #include "stg/noncopyable.h"
17 #include "stg/logger.h"
18
19 #include <string>
20 #include <map>
21 #include <list>
22 #include <mutex>
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wshadow"
25 #include <jthread.hpp>
26 #pragma GCC diagnostic pop
27 #include <cstdint>
28
29 namespace STG
30 {
31 class User;
32 struct Settings;
33 class Users;
34 class Tariffs;
35 struct Services;
36 struct Corporations;
37 struct TraffCounter;
38
39 class SMUX;
40
41 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
42 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
43 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
44 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
45
46 using UserPtr = User*;
47 //-----------------------------------------------------------------------------
48 class SMUX_SETTINGS
49 {
50     public:
51         SMUX_SETTINGS();
52         virtual ~SMUX_SETTINGS() {}
53         const std::string & GetStrError() const { return errorStr; }
54         int ParseSettings(const ModuleSettings & s);
55
56         uint32_t GetIP() const { return ip; }
57         uint16_t GetPort() const { return port; }
58         const std::string GetPassword() const { return password; }
59
60     private:
61         mutable std::string errorStr;
62
63         uint32_t ip;
64         uint16_t port;
65         std::string password;
66 };
67 //-----------------------------------------------------------------------------
68 class SMUX : public Plugin
69 {
70     public:
71         SMUX();
72         virtual ~SMUX();
73
74         void SetUsers(Users * u) { users = u; }
75         void SetTariffs(Tariffs * t) { tariffs = t; }
76         void SetAdmins(Admins * a) { admins = a; }
77         void SetServices(Services * s) { services = s; }
78         void SetTraffcounter(TraffCounter * tc) { traffcounter = tc; }
79         void SetCorporations(Corporations * c) { corporations = c; }
80         void SetSettings(const ModuleSettings & s) { settings = s; }
81         int ParseSettings();
82
83         int Start();
84         int Stop();
85         int Reload(const ModuleSettings & ms);
86         bool IsRunning() { return m_thread.joinable() && !stopped; }
87
88         const std::string & GetStrError() const { return errorStr; }
89         std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
90         uint16_t GetStartPosition() const { return 10; }
91         uint16_t GetStopPosition() const { return 10; }
92
93         bool UpdateTables();
94
95         void SetNotifier(UserPtr userPtr);
96         void UnsetNotifier(UserPtr userPtr);
97
98     private:
99         SMUX(const SMUX & rvalue);
100         SMUX & operator=(const SMUX & rvalue);
101
102         void Run(std::stop_token token);
103         bool PrepareNet();
104         bool Reconnect();
105
106         bool DispatchPDUs(const SMUX_PDUs_t * pdus);
107
108         bool CloseHandler(const SMUX_PDUs_t * pdus);
109         bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
110         bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
111         bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
112
113         bool GetRequestHandler(const PDUs_t * pdus);
114         bool GetNextRequestHandler(const PDUs_t * pdus);
115         bool SetRequestHandler(const PDUs_t * pdus);
116
117         void SetNotifiers();
118         void ResetNotifiers();
119
120         Users * users;
121         Tariffs * tariffs;
122         Admins * admins;
123         Services * services;
124         Corporations * corporations;
125         TraffCounter * traffcounter;
126
127         mutable std::string errorStr;
128         SMUX_SETTINGS smuxSettings;
129         ModuleSettings settings;
130
131         std::jthread m_thread;
132         std::mutex m_mutex;
133         bool stopped;
134         bool needReconnect;
135
136         time_t lastReconnectTry;
137         unsigned reconnectTimeout;
138
139         int sock;
140
141         SMUXHandlers smuxHandlers;
142         PDUsHandlers pdusHandlers;
143         Sensors sensors;
144         Tables tables;
145
146         ScopedConnection m_onAddUserConn;
147         ScopedConnection m_onDelUserConn;
148         ScopedConnection m_onAddTariffConn;
149         ScopedConnection m_onDelTariffConn;
150
151         using ConnHolder = std::tuple<int, ScopedConnection>;
152         std::vector<ConnHolder> m_conns;
153
154         PluginLogger logger;
155 };
156
157 }