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