]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Initialize class members for SMUX_SETTINGS in constructor
[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) : smux(s), userPtr(u) {}
61              CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER & rvalue) : smux(rvalue.smux), userPtr(rvalue.userPtr) {}
62     void     Notify(const std::string &, const std::string &);
63
64     USER_PTR GetUserPtr() { return userPtr; }
65
66 private:
67     CHG_AFTER_NOTIFIER & operator=(const CHG_AFTER_NOTIFIER & rvalue);
68     SMUX & smux;
69     USER_PTR userPtr;
70 };
71 //-----------------------------------------------------------------------------
72 class ADD_DEL_TARIFF_NOTIFIER : public NOTIFIER_BASE<TARIFF_DATA>, private NONCOPYABLE {
73 public:
74          ADD_DEL_TARIFF_NOTIFIER(SMUX & s) : smux(s) {}
75     void Notify(const TARIFF_DATA &);
76
77 private:
78     SMUX & smux;
79 };
80 //-----------------------------------------------------------------------------
81 class ADD_USER_NOTIFIER : public NOTIFIER_BASE<USER_PTR>, private NONCOPYABLE {
82 public:
83          ADD_USER_NOTIFIER(SMUX & s) : smux(s) {}
84     void Notify(const USER_PTR &);
85
86 private:
87     SMUX & smux;
88 };
89 //-----------------------------------------------------------------------------
90 class DEL_USER_NOTIFIER : public NOTIFIER_BASE<USER_PTR>, private NONCOPYABLE {
91 public:
92          DEL_USER_NOTIFIER(SMUX & s) : smux(s) {}
93     void Notify(const USER_PTR &);
94
95 private:
96     SMUX & smux;
97 };
98 //-----------------------------------------------------------------------------
99 class SMUX : public PLUGIN {
100 public:
101     SMUX();
102     virtual ~SMUX();
103
104     void SetUsers(USERS * u) { users = u; }
105     void SetTariffs(TARIFFS * t) { tariffs = t; }
106     void SetAdmins(ADMINS * a) { admins = a; }
107     void SetServices(SERVICES * s) { services = s; }
108     void SetTraffcounter(TRAFFCOUNTER * tc) { traffcounter = tc; }
109     void SetCorporations(CORPORATIONS * c) { corporations = c; }
110     void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
111     int ParseSettings();
112
113     int Start();
114     int Stop();
115     int Reload();
116     bool IsRunning() { return running && !stopped; }
117
118     const std::string & GetStrError() const { return errorStr; }
119     const std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
120     uint16_t GetStartPosition() const { return 100; }
121     uint16_t GetStopPosition() const { return 100; }
122
123     bool UpdateTables();
124
125     void SetNotifier(USER_PTR userPtr);
126     void UnsetNotifier(USER_PTR userPtr);
127
128 private:
129     SMUX(const SMUX & rvalue);
130     SMUX & operator=(const SMUX & rvalue);
131
132     static void * Runner(void * d);
133     void Run();
134     bool PrepareNet();
135
136     bool DispatchPDUs(const SMUX_PDUs_t * pdus);
137
138     bool CloseHandler(const SMUX_PDUs_t * pdus);
139     bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
140     bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
141     bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
142
143     bool GetRequestHandler(const PDUs_t * pdus);
144     bool GetNextRequestHandler(const PDUs_t * pdus);
145     bool SetRequestHandler(const PDUs_t * pdus);
146
147     void SetNotifiers();
148     void ResetNotifiers();
149
150     USERS * users;
151     TARIFFS * tariffs;
152     ADMINS * admins;
153     SERVICES * services;
154     CORPORATIONS * corporations;
155     TRAFFCOUNTER * traffcounter;
156
157     mutable std::string errorStr;
158     SMUX_SETTINGS smuxSettings;
159     MODULE_SETTINGS settings;
160
161     pthread_t thread;
162     pthread_mutex_t mutex;
163     bool running;
164     bool stopped;
165
166     int sock;
167
168     SMUXHandlers smuxHandlers;
169     PDUsHandlers pdusHandlers;
170     Sensors sensors;
171     Tables tables;
172
173     std::list<CHG_AFTER_NOTIFIER> notifiers;
174     ADD_USER_NOTIFIER addUserNotifier;
175     DEL_USER_NOTIFIER delUserNotifier;
176     ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
177 };
178 //-----------------------------------------------------------------------------
179
180 inline
181 void ADD_DEL_TARIFF_NOTIFIER::Notify(const TARIFF_DATA &)
182 {
183 smux.UpdateTables();
184 }
185
186 inline
187 void ADD_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
188 {
189 smux.SetNotifier(userPtr);
190 smux.UpdateTables();
191 }
192
193 inline
194 void DEL_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
195 {
196 smux.UnsetNotifier(userPtr);
197 smux.UpdateTables();
198 }
199
200 extern "C" PLUGIN * GetPlugin();
201
202 #endif