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