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