]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/sensors.h
Rules counter added to the SMUX plugin
[stg.git] / projects / stargazer / plugins / other / smux / sensors.h
1 #ifndef __SENSORS_H__
2 #define __SENSORS_H__
3
4 #include <map>
5
6 #include "stg/users.h"
7 #include "stg/tariffs.h"
8 #include "stg/admins.h"
9 #include "stg/services.h"
10 #include "stg/corporations.h"
11 #include "stg/traffcounter.h"
12 #include "stg/user_property.h"
13
14 #include "stg/ObjectSyntax.h"
15
16 #include "value2os.h"
17 #include "types.h"
18
19 class Sensor {
20     public:
21         virtual bool GetValue(ObjectSyntax_t * objectSyntax) const = 0;
22 #ifdef DEBUG
23         virtual std::string ToString() const = 0;
24 #endif
25 };
26
27 typedef std::map<OID, Sensor *> Sensors;
28
29 class TotalUsersSensor : public Sensor {
30     public:
31         TotalUsersSensor(const USERS & u) : users(u) {}
32         virtual ~TotalUsersSensor() {}
33
34         bool GetValue(ObjectSyntax_t * objectSyntax) const
35         {
36         ValueToOS(users.Count(), objectSyntax);
37         return true;
38         }
39
40 #ifdef DEBUG
41         std::string ToString() const
42         { std::string res; x2str(users.Count(), res); return res; }
43 #endif
44
45     private:
46         const USERS & users;
47 };
48
49 class UsersSensor : public Sensor {
50     public:
51         UsersSensor(USERS & u) : users(u) {}
52         virtual ~UsersSensor() {}
53
54         bool GetValue(ObjectSyntax_t * objectSyntax) const;
55 #ifdef DEBUG
56         std::string ToString() const;
57 #endif
58
59     private:
60         USERS & users;
61
62         virtual bool UserPredicate(USER_PTR userPtr) const = 0;
63 };
64
65 class ConnectedUsersSensor : public UsersSensor {
66     public:
67         ConnectedUsersSensor(USERS & u) : UsersSensor(u) {}
68         virtual ~ConnectedUsersSensor() {}
69
70     private:
71         bool UserPredicate(USER_PTR userPtr) const
72         { return userPtr->GetConnected(); }
73 };
74
75 class AuthorizedUsersSensor : public UsersSensor {
76     public:
77         AuthorizedUsersSensor(USERS & u) : UsersSensor(u) {}
78         virtual ~AuthorizedUsersSensor() {}
79
80     private:
81         bool UserPredicate(USER_PTR userPtr) const
82         { return userPtr->GetAuthorized(); }
83 };
84
85 class AlwaysOnlineUsersSensor : public UsersSensor {
86     public:
87         AlwaysOnlineUsersSensor(USERS & u) : UsersSensor(u) {}
88         virtual ~AlwaysOnlineUsersSensor() {}
89
90     private:
91         bool UserPredicate(USER_PTR userPtr) const
92         { return userPtr->GetProperty().alwaysOnline; }
93 };
94
95 class NoCashUsersSensor : public UsersSensor {
96     public:
97         NoCashUsersSensor(USERS & u) : UsersSensor(u) {}
98         virtual ~NoCashUsersSensor() {}
99
100     private:
101         bool UserPredicate(USER_PTR userPtr) const
102         { return userPtr->GetProperty().cash < 0; }
103 };
104
105 class DisabledDetailStatsUsersSensor : public UsersSensor {
106     public:
107         DisabledDetailStatsUsersSensor(USERS & u) : UsersSensor(u) {}
108         virtual ~DisabledDetailStatsUsersSensor() {}
109
110     private:
111         bool UserPredicate(USER_PTR userPtr) const
112         { return userPtr->GetProperty().disabledDetailStat; }
113 };
114
115 class DisabledUsersSensor : public UsersSensor {
116     public:
117         DisabledUsersSensor(USERS & u) : UsersSensor(u) {}
118         virtual ~DisabledUsersSensor() {}
119
120     private:
121         bool UserPredicate(USER_PTR userPtr) const
122         { return userPtr->GetProperty().disabled; }
123 };
124
125 class PassiveUsersSensor : public UsersSensor {
126     public:
127         PassiveUsersSensor(USERS & u) : UsersSensor(u) {}
128         virtual ~PassiveUsersSensor() {}
129
130     private:
131         bool UserPredicate(USER_PTR userPtr) const
132         { return userPtr->GetProperty().passive; }
133 };
134
135 class CreditUsersSensor : public UsersSensor {
136     public:
137         CreditUsersSensor(USERS & u) : UsersSensor(u) {}
138         virtual ~CreditUsersSensor() {}
139
140     private:
141         bool UserPredicate(USER_PTR userPtr) const
142         { return userPtr->GetProperty().credit > 0; }
143 };
144
145 class FreeMbUsersSensor : public UsersSensor {
146     public:
147         FreeMbUsersSensor(USERS & u) : UsersSensor(u) {}
148         virtual ~FreeMbUsersSensor() {}
149
150     private:
151         bool UserPredicate(USER_PTR userPtr) const
152         { return userPtr->GetProperty().freeMb > 0; }
153 };
154
155 class TariffChangeUsersSensor : public UsersSensor {
156     public:
157         TariffChangeUsersSensor(USERS & u) : UsersSensor(u) {}
158         virtual ~TariffChangeUsersSensor() {}
159
160     private:
161         bool UserPredicate(USER_PTR userPtr) const
162         { return userPtr->GetProperty().nextTariff.ConstData().empty(); }
163 };
164
165 class TotalTariffsSensor : public Sensor {
166     public:
167         TotalTariffsSensor(const TARIFFS & t) : tariffs(t) {}
168         virtual ~TotalTariffsSensor() {}
169
170         bool GetValue(ObjectSyntax_t * objectSyntax) const
171         {
172         ValueToOS(tariffs.Count(), objectSyntax);
173         return true;
174         }
175
176 #ifdef DEBUG
177         std::string ToString() const
178         { std::string res; x2str(tariffs.Count(), res); return res; }
179 #endif
180
181     private:
182         const TARIFFS & tariffs;
183 };
184
185 class TotalAdminsSensor : public Sensor {
186     public:
187         TotalAdminsSensor(const ADMINS & a) : admins(a) {}
188         virtual ~TotalAdminsSensor() {}
189
190         bool GetValue(ObjectSyntax_t * objectSyntax) const
191         {
192         ValueToOS(admins.Count(), objectSyntax);
193         return true;
194         }
195
196 #ifdef DEBUG
197         std::string ToString() const
198         { std::string res; x2str(admins.Count(), res); return res; }
199 #endif
200
201     private:
202         const ADMINS & admins;
203 };
204
205 class TotalServicesSensor : public Sensor {
206     public:
207         TotalServicesSensor(const SERVICES & s) : services(s) {}
208         virtual ~TotalServicesSensor() {}
209
210         bool GetValue(ObjectSyntax_t * objectSyntax) const
211         {
212         ValueToOS(services.Count(), objectSyntax);
213         return true;
214         }
215
216 #ifdef DEBUG
217         std::string ToString() const
218         { std::string res; x2str(services.Count(), res); return res; }
219 #endif
220
221     private:
222         const SERVICES & services;
223 };
224
225 class TotalCorporationsSensor : public Sensor {
226     public:
227         TotalCorporationsSensor(const CORPORATIONS & c) : corporations(c) {}
228         virtual ~TotalCorporationsSensor() {}
229
230         bool GetValue(ObjectSyntax_t * objectSyntax) const
231         {
232         ValueToOS(corporations.Count(), objectSyntax);
233         return true;
234         }
235
236 #ifdef DEBUG
237         std::string ToString() const
238         { std::string res; x2str(corporations.Count(), res); return res; }
239 #endif
240
241     private:
242         const CORPORATIONS & corporations;
243 };
244
245 class TotalRulesSensor : public Sensor {
246     public:
247         TotalRulesSensor(const TRAFFCOUNTER & t) : traffcounter(t) {}
248         virtual ~TotalRulesSensor() {}
249
250         bool GetValue(ObjectSyntax_t * objectSyntax) const
251         {
252         ValueToOS(traffcounter.RulesCount(), objectSyntax);
253         return true;
254         }
255
256 #ifdef DEBUG
257         std::string ToString() const
258         { std::string res; x2str(traffcounter.RulesCount(), res); return res; }
259 #endif
260
261     private:
262         const TRAFFCOUNTER & traffcounter;
263 };
264
265 template <typename T>
266 class ConstSensor : public Sensor {
267     public:
268         ConstSensor(const T & v) : value(v) {}
269         virtual ~ConstSensor() {}
270
271         bool GetValue(ObjectSyntax * objectSyntax) const
272         { return ValueToOS(value, objectSyntax); }
273
274 #ifdef DEBUG
275         std::string ToString() const
276         { std::string res; x2str(value, res); return res; }
277 #endif
278
279     private:
280         T value;
281 };
282
283 template <>
284 inline
285 std::string ConstSensor<std::string>::ToString() const
286 {
287 return value;
288 }
289
290 #endif