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