]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Move projects back into subfolder.
[stg.git] / projects / stargazer / plugins / other / smux / smux.h
1 #ifndef __SMUX_H__
2 #define __SMUX_H__
3
4 #include <pthread.h>
5
6 #include <string>
7 #include <map>
8 #include <list>
9 #include <cstdint>
10
11 #include "stg/SMUX-PDUs.h"
12 #include "stg/ObjectSyntax.h"
13
14 #include "stg/plugin.h"
15 #include "stg/module_settings.h"
16 #include "stg/notifer.h"
17 #include "stg/noncopyable.h"
18 #include "stg/logger.h"
19
20 #include "sensors.h"
21 #include "tables.h"
22 #include "types.h"
23
24 namespace STG
25 {
26 struct User;
27 struct Settings;
28 struct Users;
29 struct Tariffs;
30 struct Services;
31 struct Corporations;
32 struct TraffCounter;
33 }
34
35 class SMUX;
36
37 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
38 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
39 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
40 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
41
42 using UserPtr = STG::User*;
43 //-----------------------------------------------------------------------------
44 class SMUX_SETTINGS {
45 public:
46     SMUX_SETTINGS();
47     virtual ~SMUX_SETTINGS() {}
48     const std::string & GetStrError() const { return errorStr; }
49     int ParseSettings(const STG::ModuleSettings & s);
50
51     uint32_t GetIP() const { return ip; }
52     uint16_t GetPort() const { return port; }
53     const std::string GetPassword() const { return password; }
54
55 private:
56     mutable std::string errorStr;
57
58     uint32_t ip;
59     uint16_t port;
60     std::string password;
61 };
62 //-----------------------------------------------------------------------------
63 class CHG_AFTER_NOTIFIER : public STG::PropertyNotifierBase<std::string> {
64 public:
65              CHG_AFTER_NOTIFIER(SMUX & s, const UserPtr & u)
66                  : STG::PropertyNotifierBase<std::string>(),
67                    smux(s), userPtr(u) {}
68              CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER & rvalue)
69                  : STG::PropertyNotifierBase<std::string>(),
70                    smux(rvalue.smux), userPtr(rvalue.userPtr) {}
71     void     Notify(const std::string &, const std::string &);
72
73     UserPtr GetUserPtr() const { return userPtr; }
74
75 private:
76     CHG_AFTER_NOTIFIER & operator=(const CHG_AFTER_NOTIFIER & rvalue);
77     SMUX & smux;
78     UserPtr userPtr;
79 };
80 //-----------------------------------------------------------------------------
81 class ADD_DEL_TARIFF_NOTIFIER : public STG::NotifierBase<STG::TariffData> {
82 public:
83     explicit ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
84              : STG::NotifierBase<STG::TariffData>(), smux(s) {}
85     void Notify(const STG::TariffData &);
86
87 private:
88     SMUX & smux;
89 };
90 //-----------------------------------------------------------------------------
91 class ADD_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
92 public:
93     explicit ADD_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<STG::User*>(), smux(s) {}
94     void Notify(const UserPtr &);
95
96 private:
97     SMUX & smux;
98 };
99 //-----------------------------------------------------------------------------
100 class DEL_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
101 public:
102     explicit DEL_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<UserPtr>(), smux(s) {}
103     void Notify(const UserPtr &);
104
105 private:
106     SMUX & smux;
107 };
108 //-----------------------------------------------------------------------------
109 class SMUX : public STG::Plugin {
110 public:
111     SMUX();
112     virtual ~SMUX();
113
114     void SetUsers(STG::Users * u) { users = u; }
115     void SetTariffs(STG::Tariffs * t) { tariffs = t; }
116     void SetAdmins(STG::Admins * a) { admins = a; }
117     void SetServices(STG::Services * s) { services = s; }
118     void SetTraffcounter(STG::TraffCounter * tc) { traffcounter = tc; }
119     void SetCorporations(STG::Corporations * c) { corporations = c; }
120     void SetSettings(const STG::ModuleSettings & s) { settings = s; }
121     int ParseSettings();
122
123     int Start();
124     int Stop();
125     int Reload(const STG::ModuleSettings & ms);
126     bool IsRunning() { return running && !stopped; }
127
128     const std::string & GetStrError() const { return errorStr; }
129     std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
130     uint16_t GetStartPosition() const { return 10; }
131     uint16_t GetStopPosition() const { return 10; }
132
133     bool UpdateTables();
134
135     void SetNotifier(UserPtr userPtr);
136     void UnsetNotifier(UserPtr userPtr);
137
138 private:
139     SMUX(const SMUX & rvalue);
140     SMUX & operator=(const SMUX & rvalue);
141
142     static void * Runner(void * d);
143     void Run();
144     bool PrepareNet();
145     bool Reconnect();
146
147     bool DispatchPDUs(const SMUX_PDUs_t * pdus);
148
149     bool CloseHandler(const SMUX_PDUs_t * pdus);
150     bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
151     bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
152     bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
153
154     bool GetRequestHandler(const PDUs_t * pdus);
155     bool GetNextRequestHandler(const PDUs_t * pdus);
156     bool SetRequestHandler(const PDUs_t * pdus);
157
158     void SetNotifiers();
159     void ResetNotifiers();
160
161     STG::Users * users;
162     STG::Tariffs * tariffs;
163     STG::Admins * admins;
164     STG::Services * services;
165     STG::Corporations * corporations;
166     STG::TraffCounter * traffcounter;
167
168     mutable std::string errorStr;
169     SMUX_SETTINGS smuxSettings;
170     STG::ModuleSettings settings;
171
172     pthread_t thread;
173     pthread_mutex_t mutex;
174     bool running;
175     bool stopped;
176     bool needReconnect;
177
178     time_t lastReconnectTry;
179     unsigned reconnectTimeout;
180
181     int sock;
182
183     SMUXHandlers smuxHandlers;
184     PDUsHandlers pdusHandlers;
185     Sensors sensors;
186     Tables tables;
187
188     std::list<CHG_AFTER_NOTIFIER> notifiers;
189     ADD_USER_NOTIFIER addUserNotifier;
190     DEL_USER_NOTIFIER delUserNotifier;
191     ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
192
193     STG::PluginLogger logger;
194 };
195 //-----------------------------------------------------------------------------
196
197 inline
198 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
199 {
200 smux.UpdateTables();
201 }
202
203 inline
204 void ADD_DEL_TARIFF_NOTIFIER::Notify(const STG::TariffData &)
205 {
206 smux.UpdateTables();
207 }
208
209 inline
210 void ADD_USER_NOTIFIER::Notify(const UserPtr & userPtr)
211 {
212 smux.SetNotifier(userPtr);
213 smux.UpdateTables();
214 }
215
216 inline
217 void DEL_USER_NOTIFIER::Notify(const UserPtr & userPtr)
218 {
219 smux.UnsetNotifier(userPtr);
220 smux.UpdateTables();
221 }
222
223 #endif