]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.h
Break internal classes to interface and implementation
[stg.git] / projects / stargazer / users_impl.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.31 $
23 $Date: 2010/10/07 20:04:48 $
24 $Author: faust $
25 */
26
27
28 #ifndef USERS_H
29 #define USERS_H
30
31 #include <pthread.h>
32
33 #include <ctime>
34 #include <string>
35 #include <map>
36 #include <list>
37 #include <set>
38
39 #include "os_int.h"
40
41 #include "settings.h"
42 #include "user.h"
43 #include "tariffs.h"
44 #include "stg_logger.h"
45 #include "notifer.h"
46 #include "actions.h"
47 #include "noncopyable.h"
48 #include "eventloop.h"
49
50 const int userDeleteDelayTime = 120;
51
52 using namespace std;
53 class USERS;
54 //-----------------------------------------------------------------------------
55 class PROPERTY_NOTIFER_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t>
56 {
57 public:
58     PROPERTY_NOTIFER_IP_BEFORE(USERS & us, user_iter u) : users(us), user(u) {};
59     void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
60     user_iter   GetUser() const { return user; };
61 private:
62     USERS &     users;
63     user_iter   user;
64 };
65 //-----------------------------------------------------------------------------
66 class PROPERTY_NOTIFER_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t>
67 {
68 public:
69     PROPERTY_NOTIFER_IP_AFTER(USERS & us, user_iter u) : users(us), user(u) {};
70     void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
71     user_iter   GetUser() const { return user; };
72 private:
73     USERS &     users;
74     user_iter   user;
75 };
76 //-----------------------------------------------------------------------------
77 struct USER_TO_DEL
78 {
79 USER_TO_DEL()
80     : iter(),
81       delTime(0)
82 {};
83
84 list<USER>::iterator iter;
85 time_t  delTime;
86 };
87 //-----------------------------------------------------------------------------
88 class USERS : private NONCOPYABLE
89     {
90     friend class PROPERTY_NOTIFER_IP_BEFORE;
91     friend class PROPERTY_NOTIFER_IP_AFTER;
92
93 public:
94     USERS(SETTINGS * s, BASE_STORE * store, TARIFFS * tariffs, const ADMIN & sysAdmin);
95     ~USERS();
96
97     int             FindByName(const string & login, user_iter * user) const;
98     int             FindByID(int id, user_iter * user);
99
100     bool            TariffInUse(const string & tariffName);
101
102     void            AddNotifierUserAdd(NOTIFIER_BASE<user_iter> *);
103     void            DelNotifierUserAdd(NOTIFIER_BASE<user_iter> *);
104
105     void            AddNotifierUserDel(NOTIFIER_BASE<user_iter> *);
106     void            DelNotifierUserDel(NOTIFIER_BASE<user_iter> *);
107
108     int             Add(const string & login, const ADMIN & admin);
109     void            Del(const string & login, const ADMIN & admin);
110
111     int             ReadUsers();
112     int             GetUserNum();
113
114     int             FindByIPIdx(uint32_t ip, user_iter * user);
115
116     int             OpenSearch();
117     int             SearchNext(int handler, user_iter * u);
118     int             CloseSearch(int handler);
119
120     int             Start();
121     int             Stop();
122
123 private:
124     void            AddToIPIdx(user_iter);
125     void            DelFromIPIdx(uint32_t ip);
126
127     int             FindByNameNonLock(const string & login, user_iter * user) const;
128     int             FindByIDNonLock(int id, user_iter * user);
129
130     void            RealDelUser();
131     void            ProcessActions();
132
133     void            SetUserNotifiers(user_iter user);
134     void            UnSetUserNotifiers(user_iter user);
135
136     void            AddUserIntoIndexes(user_iter user);
137     void            DelUserFromIndexes(user_iter user);
138
139     static void *   Run(void *);
140     void            NewMinute(const struct tm & t);
141     void            NewDay(const struct tm & t);
142     void            DayResetTraff(const struct tm & t);
143
144     bool            TimeToWriteDetailStat(const struct tm & t);
145
146     list<USER>          users;
147     list<USER_TO_DEL>   usersToDelete;
148     list<PROPERTY_NOTIFER_IP_BEFORE> userIPNotifiersBefore;
149     list<PROPERTY_NOTIFER_IP_AFTER>  userIPNotifiersAfter;
150
151     map<uint32_t, user_iter> ipIndex;
152     map<string,   user_iter> loginIndex;
153
154     SETTINGS *          settings;
155     TARIFFS *           tariffs;
156     BASE_STORE *        store;
157     const ADMIN         sysAdmin;
158     STG_LOGGER &        WriteServLog;
159
160     bool                nonstop;
161     bool                isRunning;
162
163     mutable pthread_mutex_t mutex;
164     pthread_t               thread;
165     mutable unsigned int    handle;
166
167     mutable map<int, user_iter> searchDescriptors;
168
169     set <NOTIFIER_BASE<user_iter>*> onAddNotifiers;
170     set <NOTIFIER_BASE<user_iter>*> onDelNotifiers;
171     };
172 //-----------------------------------------------------------------------------
173 inline
174 void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue,
175                                         const uint32_t &)
176 {
177 if (!oldValue)
178     return;
179
180 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::DelFromIPIdx, oldValue);
181 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
182 users.DelFromIPIdx(oldValue);
183 }
184 //-----------------------------------------------------------------------------
185 inline
186 void PROPERTY_NOTIFER_IP_AFTER::Notify(const uint32_t &,
187                                        const uint32_t & newValue)
188 {
189 if (!newValue)
190     return;
191
192 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::AddToIPIdx, user);
193 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
194 users.AddToIPIdx(user);
195 }
196 //-----------------------------------------------------------------------------
197 #endif
198