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