]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/stress/stress.h
Fix auth_stress plugin to be up-to-date with current plugin interface
[stg.git] / projects / stargazer / plugins / authorization / stress / stress.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.3 $
23  $Date: 2009/06/19 12:50:32 $
24  $Author: faust $
25  */
26
27
28 #ifndef STRESS_H
29 #define STRESS_H
30
31 #include <pthread.h>
32
33 #include <string>
34 #include <list>
35
36 #include "stg/auth.h"
37 #include "stg/notifer.h"
38 #include "stg/user_ips.h"
39 #include "stg/users.h"
40 #include "stg/module_settings.h"
41
42 extern "C" PLUGIN * GetPlugin();
43
44 class AUTH_STRESS;
45 //-----------------------------------------------------------------------------
46 template <typename varParamType>
47 class CHG_BEFORE_NOTIFIER: public PROPERTY_NOTIFIER_BASE<varParamType> {
48 public:
49     void Notify(const varParamType & oldValue, const varParamType & newValue);
50     void        SetUser(USER_PTR u) { user = u; }
51     USER_PTR    GetUser() {return user; }
52     void        SetAuthorizator(const AUTH_STRESS * a) { auth = a; }
53
54 private:
55     USER_PTR    user;
56     const       AUTH_STRESS * auth;
57 };
58 //-----------------------------------------------------------------------------
59 template <typename varParamType>
60 class CHG_AFTER_NOTIFIER: public PROPERTY_NOTIFIER_BASE<varParamType> {
61 public:
62     void        Notify(const varParamType & oldValue, const varParamType & newValue);
63     void        SetUser(USER_PTR u) { user = u; }
64     USER_PTR    GetUser() {return user; }
65     void        SetAuthorizator(const AUTH_STRESS * a) { auth = a; }
66
67 private:
68     USER_PTR   user;
69     const AUTH_STRESS * auth;
70 };
71 //-----------------------------------------------------------------------------
72 class AUTH_STRESS_SETTINGS {
73 public:
74                     AUTH_STRESS_SETTINGS();
75     const std::string & GetStrError() const { return errorStr; }
76     int             ParseSettings(const MODULE_SETTINGS & s);
77     int             GetAverageOnlineTime() const;
78 private:
79     int             ParseIntInRange(const std::string & str, int min, int max, int * val);
80     int             averageOnlineTime;
81     std::string     errorStr;
82 };
83 //-----------------------------------------------------------------------------
84 class AUTH_STRESS :public AUTH {
85 friend class CHG_BEFORE_NOTIFIER<USER_IPS>;
86 friend class CHG_AFTER_NOTIFIER<USER_IPS>;
87 public:
88     AUTH_STRESS();
89     virtual ~AUTH_STRESS() {}
90
91     void                SetUsers(USERS * u);
92     void                SetTariffs(TARIFFS *) {}
93     void                SetAdmins(ADMINS *) {}
94     void                SetTraffcounter(TRAFFCOUNTER *) {}
95     void                SetStore(STORE *) {}
96     void                SetStgSettings(const SETTINGS *) {}
97
98     int                 Start();
99     int                 Stop();
100     int                 Reload() { return 0; }
101     bool                IsRunning();
102     void                SetSettings(const MODULE_SETTINGS & s);
103     int                 ParseSettings();
104     const std::string & GetStrError() const;
105     const std::string   GetVersion() const;
106     uint16_t            GetStartPosition() const;
107     uint16_t            GetStopPosition() const;
108
109     int                 SendMessage(const STG_MSG & msg, uint32_t ip) const;
110
111 private:
112     void                GetUsers();
113     void                SetUserNotifiers(USER_PTR u);
114     void                UnSetUserNotifiers(USER_PTR u);
115
116     void                AddUser(USER_PTR u);
117     void                DelUser(USER_PTR u);
118
119     void                Authorize(USER_PTR u) const;
120     void                Unauthorize(USER_PTR u) const;
121
122     static void *       Run(void *);
123
124     bool                nonstop;
125
126     mutable std::string errorStr;
127     AUTH_STRESS_SETTINGS stressSettings;
128     USERS             * users;
129     std::list<USER_PTR> usersList;
130     bool                isRunning;
131     MODULE_SETTINGS     settings;
132
133     pthread_t           thread;
134     pthread_mutex_t     mutex;
135
136     std::list<CHG_BEFORE_NOTIFIER<USER_IPS> > BeforeChgIPNotifierList;
137     std::list<CHG_AFTER_NOTIFIER<USER_IPS> > AfterChgIPNotifierList;
138
139     class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
140     public:
141         ADD_USER_NONIFIER() {}
142         virtual ~ADD_USER_NONIFIER() {}
143
144         void SetAuthorizator(AUTH_STRESS * a) { auth = a; }
145         void Notify(const USER_PTR & user)
146             {
147             auth->AddUser(user);
148             }
149
150     private:
151         AUTH_STRESS * auth;
152     } onAddUserNotifier;
153
154     class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
155     public:
156         DEL_USER_NONIFIER() {}
157         virtual ~DEL_USER_NONIFIER() {}
158
159         void SetAuthorizator(AUTH_STRESS * a) { auth = a; }
160         void Notify(const USER_PTR & user)
161             {
162             auth->DelUser(user);
163             }
164
165     private:
166         AUTH_STRESS * auth;
167     } onDelUserNotifier;
168
169 };
170 //-----------------------------------------------------------------------------
171
172 #endif