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