]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
More subscriptions, less notifiers.
[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 SMUX : public STG::Plugin {
85 public:
86     SMUX();
87     virtual ~SMUX();
88
89     void SetUsers(STG::Users * u) { users = u; }
90     void SetTariffs(STG::Tariffs * t) { tariffs = t; }
91     void SetAdmins(STG::Admins * a) { admins = a; }
92     void SetServices(STG::Services * s) { services = s; }
93     void SetTraffcounter(STG::TraffCounter * tc) { traffcounter = tc; }
94     void SetCorporations(STG::Corporations * c) { corporations = c; }
95     void SetSettings(const STG::ModuleSettings & s) { settings = s; }
96     int ParseSettings();
97
98     int Start();
99     int Stop();
100     int Reload(const STG::ModuleSettings & ms);
101     bool IsRunning() { return m_thread.joinable() && !stopped; }
102
103     const std::string & GetStrError() const { return errorStr; }
104     std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
105     uint16_t GetStartPosition() const { return 10; }
106     uint16_t GetStopPosition() const { return 10; }
107
108     bool UpdateTables();
109
110     void SetNotifier(UserPtr userPtr);
111     void UnsetNotifier(UserPtr userPtr);
112
113 private:
114     SMUX(const SMUX & rvalue);
115     SMUX & operator=(const SMUX & rvalue);
116
117     void Run(std::stop_token token);
118     bool PrepareNet();
119     bool Reconnect();
120
121     bool DispatchPDUs(const SMUX_PDUs_t * pdus);
122
123     bool CloseHandler(const SMUX_PDUs_t * pdus);
124     bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
125     bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
126     bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
127
128     bool GetRequestHandler(const PDUs_t * pdus);
129     bool GetNextRequestHandler(const PDUs_t * pdus);
130     bool SetRequestHandler(const PDUs_t * pdus);
131
132     void SetNotifiers();
133     void ResetNotifiers();
134
135     STG::Users * users;
136     STG::Tariffs * tariffs;
137     STG::Admins * admins;
138     STG::Services * services;
139     STG::Corporations * corporations;
140     STG::TraffCounter * traffcounter;
141
142     mutable std::string errorStr;
143     SMUX_SETTINGS smuxSettings;
144     STG::ModuleSettings settings;
145
146     std::jthread m_thread;
147     std::mutex m_mutex;
148     bool stopped;
149     bool needReconnect;
150
151     time_t lastReconnectTry;
152     unsigned reconnectTimeout;
153
154     int sock;
155
156     SMUXHandlers smuxHandlers;
157     PDUsHandlers pdusHandlers;
158     Sensors sensors;
159     Tables tables;
160
161     STG::ScopedConnection m_onAddUserConn;
162     STG::ScopedConnection m_onDelUserConn;
163     STG::ScopedConnection m_onAddTariffConn;
164     STG::ScopedConnection m_onDelTariffConn;
165
166     std::list<CHG_AFTER_NOTIFIER> notifiers;
167
168     STG::PluginLogger logger;
169 };
170 //-----------------------------------------------------------------------------
171
172 inline
173 void CHG_AFTER_NOTIFIER::notify(const std::string &, const std::string &)
174 {
175 smux.UpdateTables();
176 }