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