]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Introduced logger for plugins.
[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
142     bool DispatchPDUs(const SMUX_PDUs_t * pdus);
143
144     bool CloseHandler(const SMUX_PDUs_t * pdus);
145     bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
146     bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
147     bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
148
149     bool GetRequestHandler(const PDUs_t * pdus);
150     bool GetNextRequestHandler(const PDUs_t * pdus);
151     bool SetRequestHandler(const PDUs_t * pdus);
152
153     void SetNotifiers();
154     void ResetNotifiers();
155
156     USERS * users;
157     TARIFFS * tariffs;
158     ADMINS * admins;
159     SERVICES * services;
160     CORPORATIONS * corporations;
161     TRAFFCOUNTER * traffcounter;
162
163     mutable std::string errorStr;
164     SMUX_SETTINGS smuxSettings;
165     MODULE_SETTINGS settings;
166
167     pthread_t thread;
168     pthread_mutex_t mutex;
169     bool running;
170     bool stopped;
171
172     int sock;
173
174     SMUXHandlers smuxHandlers;
175     PDUsHandlers pdusHandlers;
176     Sensors sensors;
177     Tables tables;
178
179     std::list<CHG_AFTER_NOTIFIER> notifiers;
180     ADD_USER_NOTIFIER addUserNotifier;
181     DEL_USER_NOTIFIER delUserNotifier;
182     ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
183
184     PLUGIN_LOGGER logger;
185 };
186 //-----------------------------------------------------------------------------
187
188 inline
189 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
190 {
191 smux.UpdateTables();
192 }
193
194 inline
195 void ADD_DEL_TARIFF_NOTIFIER::Notify(const TARIFF_DATA &)
196 {
197 smux.UpdateTables();
198 }
199
200 inline
201 void ADD_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
202 {
203 smux.SetNotifier(userPtr);
204 smux.UpdateTables();
205 }
206
207 inline
208 void DEL_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
209 {
210 smux.UnsetNotifier(userPtr);
211 smux.UpdateTables();
212 }
213
214 extern "C" PLUGIN * GetPlugin();
215
216 #endif