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