]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Explicitly initialize base class in copy ctors of notifiers
[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
19 #include "sensors.h"
20 #include "tables.h"
21 #include "types.h"
22
23 extern "C" PLUGIN * GetPlugin();
24
25 class USER;
26 class SETTINGS;
27 class SMUX;
28 class USERS;
29 class TARIFFS;
30 class SERVICES;
31 class CORPORATIONS;
32 class TRAFFCOUNTER;
33
34 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
35 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
36 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
37 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
38 //-----------------------------------------------------------------------------
39 class SMUX_SETTINGS {
40 public:
41     SMUX_SETTINGS();
42     virtual ~SMUX_SETTINGS() {}
43     const std::string & GetStrError() const { return errorStr; }
44     int ParseSettings(const MODULE_SETTINGS & s);
45
46     uint32_t GetIP() const { return ip; }
47     uint16_t GetPort() const { return port; }
48     const std::string GetPassword() const { return password; }
49
50 private:
51     mutable std::string errorStr;
52
53     uint32_t ip;
54     uint16_t port;
55     std::string password;
56 };
57 //-----------------------------------------------------------------------------
58 class CHG_AFTER_NOTIFIER : public PROPERTY_NOTIFIER_BASE<std::string> {
59 public:
60              CHG_AFTER_NOTIFIER(SMUX & s, const USER_PTR & u)
61                  : PROPERTY_NOTIFIER_BASE<std::string>(),
62                    smux(s), userPtr(u) {}
63              CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER & rvalue)
64                  : PROPERTY_NOTIFIER_BASE<std::string>(),
65                    smux(rvalue.smux), userPtr(rvalue.userPtr) {}
66     void     Notify(const std::string &, const std::string &);
67
68     USER_PTR GetUserPtr() { return userPtr; }
69
70 private:
71     CHG_AFTER_NOTIFIER & operator=(const CHG_AFTER_NOTIFIER & rvalue);
72     SMUX & smux;
73     USER_PTR userPtr;
74 };
75 //-----------------------------------------------------------------------------
76 class ADD_DEL_TARIFF_NOTIFIER : public NOTIFIER_BASE<TARIFF_DATA>, private NONCOPYABLE {
77 public:
78          ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
79              : NOTIFIER_BASE<TARIFF_DATA>(), smux(s) {}
80     void Notify(const TARIFF_DATA &);
81
82 private:
83     SMUX & smux;
84 };
85 //-----------------------------------------------------------------------------
86 class ADD_USER_NOTIFIER : public NOTIFIER_BASE<USER_PTR>, private NONCOPYABLE {
87 public:
88          ADD_USER_NOTIFIER(SMUX & s) : NOTIFIER_BASE<USER_PTR>(), smux(s) {}
89     void Notify(const USER_PTR &);
90
91 private:
92     SMUX & smux;
93 };
94 //-----------------------------------------------------------------------------
95 class DEL_USER_NOTIFIER : public NOTIFIER_BASE<USER_PTR>, private NONCOPYABLE {
96 public:
97          DEL_USER_NOTIFIER(SMUX & s) : NOTIFIER_BASE<USER_PTR>(), smux(s) {}
98     void Notify(const USER_PTR &);
99
100 private:
101     SMUX & smux;
102 };
103 //-----------------------------------------------------------------------------
104 class SMUX : public PLUGIN {
105 public:
106     SMUX();
107     virtual ~SMUX();
108
109     void SetUsers(USERS * u) { users = u; }
110     void SetTariffs(TARIFFS * t) { tariffs = t; }
111     void SetAdmins(ADMINS * a) { admins = a; }
112     void SetServices(SERVICES * s) { services = s; }
113     void SetTraffcounter(TRAFFCOUNTER * tc) { traffcounter = tc; }
114     void SetCorporations(CORPORATIONS * c) { corporations = c; }
115     void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
116     int ParseSettings();
117
118     int Start();
119     int Stop();
120     int Reload();
121     bool IsRunning() { return running && !stopped; }
122
123     const std::string & GetStrError() const { return errorStr; }
124     const std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
125     uint16_t GetStartPosition() const { return 100; }
126     uint16_t GetStopPosition() const { return 100; }
127
128     bool UpdateTables();
129
130     void SetNotifier(USER_PTR userPtr);
131     void UnsetNotifier(USER_PTR userPtr);
132
133 private:
134     SMUX(const SMUX & rvalue);
135     SMUX & operator=(const SMUX & rvalue);
136
137     static void * Runner(void * d);
138     void Run();
139     bool PrepareNet();
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
171     int sock;
172
173     SMUXHandlers smuxHandlers;
174     PDUsHandlers pdusHandlers;
175     Sensors sensors;
176     Tables tables;
177
178     std::list<CHG_AFTER_NOTIFIER> notifiers;
179     ADD_USER_NOTIFIER addUserNotifier;
180     DEL_USER_NOTIFIER delUserNotifier;
181     ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
182 };
183 //-----------------------------------------------------------------------------
184
185 inline
186 void ADD_DEL_TARIFF_NOTIFIER::Notify(const TARIFF_DATA &)
187 {
188 smux.UpdateTables();
189 }
190
191 inline
192 void ADD_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
193 {
194 smux.SetNotifier(userPtr);
195 smux.UpdateTables();
196 }
197
198 inline
199 void DEL_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
200 {
201 smux.UnsetNotifier(userPtr);
202 smux.UpdateTables();
203 }
204
205 extern "C" PLUGIN * GetPlugin();
206
207 #endif