]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.h
Add tables update on user add and del
[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
17 #include "sensors.h"
18 #include "tables.h"
19 #include "types.h"
20
21 extern "C" PLUGIN * GetPlugin();
22
23 class USER;
24 class SETTINGS;
25 class SMUX;
26 class USERS;
27 class TARIFFS;
28 class SERVICES;
29 class CORPORATIONS;
30 class TRAFFCOUNTER;
31
32 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
33 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
34 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
35 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
36 //-----------------------------------------------------------------------------
37 class SMUX_SETTINGS {
38 public:
39     SMUX_SETTINGS();
40     virtual ~SMUX_SETTINGS() {}
41     const std::string & GetStrError() const { return errorStr; }
42     int ParseSettings(const MODULE_SETTINGS & s);
43
44     uint32_t GetIP() const { return ip; }
45     uint16_t GetPort() const { return port; }
46     const std::string GetPassword() const { return password; }
47
48 private:
49     mutable std::string errorStr;
50
51     uint32_t ip;
52     uint16_t port;
53     std::string password;
54 };
55 //-----------------------------------------------------------------------------
56 class CHG_AFTER_NOTIFIER : public PROPERTY_NOTIFIER_BASE<std::string> {
57 public:
58                 CHG_AFTER_NOTIFIER(SMUX & s, const USER_PTR & u) : smux(s), userPtr(u) {}
59     void        Notify(const std::string &, const std::string &);
60
61     USER_PTR    GetUserPtr() { return userPtr; }
62
63 private:
64     SMUX & smux;
65     USER_PTR userPtr;
66 };
67 //-----------------------------------------------------------------------------
68 class ADD_DEL_USER_NOTIFIER : public NOTIFIER_BASE<USER_PTR> {
69 public:
70                 ADD_DEL_USER_NOTIFIER(SMUX & s) : smux(s) {}
71     void        Notify(const USER_PTR &);
72
73 private:
74     SMUX & smux;
75 };
76 //-----------------------------------------------------------------------------
77 class SMUX : public PLUGIN {
78 public:
79     SMUX();
80     virtual ~SMUX();
81
82     void SetUsers(USERS * u) { users = u; }
83     void SetTariffs(TARIFFS * t) { tariffs = t; }
84     void SetAdmins(ADMINS * a) { admins = a; }
85     void SetServices(SERVICES * s) { services = s; }
86     void SetTraffcounter(TRAFFCOUNTER * tc) { traffcounter = tc; }
87     void SetCorporations(CORPORATIONS * c) { corporations = c; }
88     void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
89     int ParseSettings();
90
91     int Start();
92     int Stop();
93     int Reload() { return 0; }
94     bool IsRunning() { return running && !stopped; }
95
96     const std::string & GetStrError() const { return errorStr; }
97     const std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
98     uint16_t GetStartPosition() const { return 100; }
99     uint16_t GetStopPosition() const { return 100; }
100
101     bool UpdateTables();
102
103 private:
104     static void * Runner(void * d);
105     void Run();
106     bool PrepareNet();
107
108     bool DispatchPDUs(const SMUX_PDUs_t * pdus);
109
110     bool CloseHandler(const SMUX_PDUs_t * pdus);
111     bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
112     bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
113     bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
114
115     bool GetRequestHandler(const PDUs_t * pdus);
116     bool GetNextRequestHandler(const PDUs_t * pdus);
117     bool SetRequestHandler(const PDUs_t * pdus);
118
119     void SetNotifiers();
120     void ResetNotifiers();
121
122     USERS * users;
123     TARIFFS * tariffs;
124     ADMINS * admins;
125     SERVICES * services;
126     CORPORATIONS * corporations;
127     TRAFFCOUNTER * traffcounter;
128
129     mutable std::string errorStr;
130     SMUX_SETTINGS smuxSettings;
131     MODULE_SETTINGS settings;
132
133     pthread_t thread;
134     pthread_mutex_t mutex;
135     bool running;
136     bool stopped;
137
138     int sock;
139
140     SMUXHandlers smuxHandlers;
141     PDUsHandlers pdusHandlers;
142     Sensors sensors;
143     Tables tables;
144
145     std::list<CHG_AFTER_NOTIFIER> notifiers;
146     ADD_DEL_USER_NOTIFIER addDelNotifier;
147 };
148 //-----------------------------------------------------------------------------
149
150 extern "C" PLUGIN * GetPlugin();
151
152 #endif