]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.cpp
Get<something>Num -> Count
[stg.git] / projects / stargazer / users_impl.cpp
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  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.61 $
27  $Date: 2010/09/13 05:56:42 $
28  $Author: faust $
29  */
30
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34
35 #include <pthread.h>
36 #include <csignal>
37 #include <cassert>
38 #include <algorithm>
39 #include <utility>
40 #include <string>
41 #include <vector>
42
43 #include "stg/settings.h"
44 #include "stg/common.h"
45 #include "users_impl.h"
46 #include "stg_timer.h"
47
48 using namespace std;
49
50 extern const volatile time_t stgTime;
51
52 //#define USERS_DEBUG 1
53
54 //-----------------------------------------------------------------------------
55 USERS_IMPL::USERS_IMPL(SETTINGS_IMPL * s, STORE * st, TARIFFS * t, const ADMIN * sa)
56     : USERS(),
57       users(),
58       usersToDelete(),
59       userIPNotifiersBefore(),
60       userIPNotifiersAfter(),
61       ipIndex(),
62       loginIndex(),
63       settings(s),
64       tariffs(t),
65       store(st),
66       sysAdmin(sa),
67       WriteServLog(GetStgLogger()),
68       nonstop(false),
69       isRunning(false),
70       mutex(),
71       thread(),
72       handle(0),
73       searchDescriptors(),
74       onAddNotifiers(),
75       onDelNotifiers(),
76       onAddNotifiersImpl(),
77       onDelNotifiersImpl()
78 {
79 pthread_mutexattr_t attr;
80 pthread_mutexattr_init(&attr);
81 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
82 pthread_mutex_init(&mutex, &attr);
83 }
84 //-----------------------------------------------------------------------------
85 USERS_IMPL::~USERS_IMPL()
86 {
87 pthread_mutex_destroy(&mutex);
88 }
89 //-----------------------------------------------------------------------------
90 int USERS_IMPL::FindByNameNonLock(const string & login, user_iter * user)
91 {
92 map<string, user_iter>::iterator iter;
93 iter = loginIndex.find(login);
94 if (iter != loginIndex.end())
95     {
96     if (user)
97         *user = iter->second;
98     return 0;
99     }
100 return -1;
101 }
102 //-----------------------------------------------------------------------------
103 int USERS_IMPL::FindByName(const string & login, USER_PTR * user)
104 {
105 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
106 user_iter u;
107 int res = FindByNameNonLock(login, &u);
108 if (res)
109     return -1;
110 *user = &(*u);
111 return 0;
112 }
113 //-----------------------------------------------------------------------------
114 bool USERS_IMPL::TariffInUse(const string & tariffName) const
115 {
116 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
117 list<USER_IMPL>::const_iterator iter;
118 iter = users.begin();
119 while (iter != users.end())
120     {
121     if (iter->GetProperty().tariffName.Get() == tariffName)
122         return true;
123     ++iter;
124     }
125 return false;
126 }
127 //-----------------------------------------------------------------------------
128 int USERS_IMPL::Add(const string & login, const ADMIN * admin)
129 {
130 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
131 const PRIV * priv = admin->GetPriv();
132
133 if (!priv->userAddDel)
134     {
135     WriteServLog("%s tried to add user \'%s\'. Access denied.",
136          admin->GetLogStr().c_str(), login.c_str());
137     /*errorStr = "Admin \'" + admin->GetLogin() +
138                "\': tried to add user \'" + ud->login + "\'. Access denied.";*/
139     return -1;
140     }
141
142 //////
143 if (store->AddUser(login))
144     {
145     //TODO
146     //WriteServLog("Admin \'%s\': tried to add user \'%s\'. Access denied.",
147     //     admin->GetLogin().c_str(), ud->login.c_str());
148     return -1;
149     }
150 //////
151
152 USER_IMPL u(settings, store, tariffs, sysAdmin, this);
153
154 /*struct tm * tms;
155 time_t t = stgTime;
156
157 tms = localtime(&t);
158
159 tms->tm_hour = 0;
160 tms->tm_min = 0;
161 tms->tm_sec = 0;
162
163 if (settings->GetDayResetTraff() > tms->tm_mday)
164     tms->tm_mon -= 1;
165
166 tms->tm_mday = settings->GetDayResetTraff();*/
167
168 u.SetLogin(login);
169
170 u.SetPassiveTimeAsNewUser();
171
172 u.WriteConf();
173 u.WriteStat();
174
175 WriteServLog("%s User \'%s\' added.",
176          admin->GetLogStr().c_str(), login.c_str());
177
178 u.OnAdd();
179
180 users.push_front(u);
181
182 AddUserIntoIndexes(users.begin());
183 SetUserNotifiers(users.begin());
184
185     {
186     // Fire all "on add" notifiers
187     set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onAddNotifiers.begin();
188     while (ni != onAddNotifiers.end())
189         {
190         (*ni)->Notify(&users.front());
191         ++ni;
192         }
193     }
194
195     {
196     // Fire all "on add" implementation notifiers
197     set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onAddNotifiersImpl.begin();
198     while (ni != onAddNotifiersImpl.end())
199         {
200         (*ni)->Notify(&users.front());
201         ++ni;
202         }
203     }
204
205 return 0;
206 }
207 //-----------------------------------------------------------------------------
208 void USERS_IMPL::Del(const string & login, const ADMIN * admin)
209 {
210 const PRIV * priv = admin->GetPriv();
211 user_iter u;
212
213 if (!priv->userAddDel)
214     {
215     WriteServLog("%s tried to remove user \'%s\'. Access denied.",
216          admin->GetLogStr().c_str(), login.c_str());
217     return;
218     }
219
220
221     {
222     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
223
224     if (FindByNameNonLock(login, &u))
225         {
226         WriteServLog("%s tried to delete user \'%s\': not found.",
227                      admin->GetLogStr().c_str(),
228                      login.c_str());
229         return;
230         }
231     }
232
233     {
234     set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onDelNotifiers.begin();
235     while (ni != onDelNotifiers.end())
236         {
237         (*ni)->Notify(&(*u));
238         ++ni;
239         }
240     }
241
242     {
243     set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onDelNotifiersImpl.begin();
244     while (ni != onDelNotifiersImpl.end())
245         {
246         (*ni)->Notify(&(*u));
247         ++ni;
248         }
249     }
250
251     {
252     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
253
254     u->OnDelete();
255     u->SetDeleted();
256
257     USER_TO_DEL utd;
258     utd.iter = u;
259     utd.delTime = stgTime;
260     usersToDelete.push_back(utd);
261
262     UnSetUserNotifiers(u);
263     DelUserFromIndexes(u);
264
265     WriteServLog("%s User \'%s\' deleted.",
266              admin->GetLogStr().c_str(), login.c_str());
267
268     }
269 }
270 //-----------------------------------------------------------------------------
271 int USERS_IMPL::ReadUsers()
272 {
273 vector<string> usersList;
274 usersList.clear();
275 if (store->GetUsersList(&usersList) < 0)
276     {
277     WriteServLog(store->GetStrError().c_str());
278     return -1;
279     }
280
281 user_iter ui;
282
283 for (unsigned int i = 0; i < usersList.size(); i++)
284     {
285     USER_IMPL u(settings, store, tariffs, sysAdmin, this);
286
287     u.SetLogin(usersList[i]);
288     users.push_front(u);
289     ui = users.begin();
290
291     AddUserIntoIndexes(ui);
292     SetUserNotifiers(ui);
293
294     if (ui->ReadConf() < 0)
295         return -1;
296
297     if (ui->ReadStat() < 0)
298         return -1;
299     }
300
301 return 0;
302 }
303 //-----------------------------------------------------------------------------
304 void * USERS_IMPL::Run(void * d)
305 {
306 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
307 USERS_IMPL * us = (USERS_IMPL*) d;
308
309 struct tm t;
310 time_t tt = stgTime;
311 localtime_r(&tt, &t);
312
313 int min = t.tm_min;
314 int day = t.tm_mday;
315
316 printfd(__FILE__,"Day = %d Min = %d\n", day, min);
317
318 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
319 string monFile = us->settings->GetMonitorDir() + "/users_r";
320 printfd(__FILE__, "Monitor=%d file USERS %s\n", us->settings->GetMonitoring(), monFile.c_str());
321
322 us->isRunning = true;
323 while (us->nonstop)
324     {
325     //printfd(__FILE__,"New Minute. old = %02d current = %02d\n", min, t->tm_min);
326     //printfd(__FILE__,"New Day.    old = %2d current = %2d\n", day, t->tm_mday);
327
328     for_each(us->users.begin(), us->users.end(), mem_fun_ref(&USER_IMPL::Run));
329
330     tt = stgTime;
331     localtime_r(&tt, &t);
332
333     if (min != t.tm_min)
334         {
335         printfd(__FILE__,"Sec = %d\n", stgTime);
336         printfd(__FILE__,"New Minute. old = %d current = %d\n", min, t.tm_min);
337         min = t.tm_min;
338
339         us->NewMinute(t);
340         }
341
342     if (day != t.tm_mday)
343         {
344         printfd(__FILE__,"Sec = %d\n", stgTime);
345         printfd(__FILE__,"New Day. old = %d current = %d\n", day, t.tm_mday);
346         day = t.tm_mday;
347         us->NewDay(t);
348         }
349
350     if (us->settings->GetMonitoring() && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
351         {
352         //printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
353         touchTime = stgTime;
354         TouchFile(monFile.c_str());
355         }
356
357     stgUsleep(100000);
358     } //while (us->nonstop)
359
360 user_iter ui = us->users.begin();
361 while (ui != us->users.end())
362     {
363     us->UnSetUserNotifiers(ui);
364     us->DelUserFromIndexes(ui);
365     ++ui;
366     }
367
368 list<USER_TO_DEL>::iterator iter;
369 iter = us->usersToDelete.begin();
370 while (iter != us->usersToDelete.end())
371     {
372     iter->delTime -= 2 * userDeleteDelayTime;
373     ++iter;
374     }
375 us->RealDelUser();
376
377 us->isRunning = false;
378
379 return NULL;
380 }
381 //-----------------------------------------------------------------------------
382 void USERS_IMPL::NewMinute(const struct tm & t)
383 {
384 //Write traff, reset session traff. Fake disconnect-connect
385 if (t.tm_hour == 23 && t.tm_min == 59)
386     {
387     printfd(__FILE__,"MidnightResetSessionStat\n");
388     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::MidnightResetSessionStat));
389     }
390
391 if (TimeToWriteDetailStat(t))
392     {
393     //printfd(__FILE__, "USER::WriteInetStat\n");
394     int usersCnt = 0;
395
396     // ðÉÛÅÍ ÀÚÅÒÏ× ÞÁÓÔÑÍÉ. ÷ ÐÅÒÅÒÙ×ÁÈ ×ÙÚÙ×ÁÅÍ USER::Run
397     list<USER_IMPL>::iterator usr = users.begin();
398     while (usr != users.end())
399         {
400         usersCnt++;
401         usr->WriteDetailStat();
402         ++usr;
403         if (usersCnt % 10 == 0)
404             for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
405         }
406     }
407
408 RealDelUser();
409 }
410 //-----------------------------------------------------------------------------
411 void USERS_IMPL::NewDay(const struct tm & t)
412 {
413 struct tm t1;
414 time_t tt = stgTime;
415 localtime_r(&tt, &t1);
416 int dayFee = settings->GetDayFee();
417
418 if (dayFee == 0)
419     dayFee = DaysInCurrentMonth();
420
421 printfd(__FILE__, "DayFee = %d\n", dayFee);
422 printfd(__FILE__, "Today = %d DayResetTraff = %d\n", t1.tm_mday, settings->GetDayResetTraff());
423 printfd(__FILE__, "DayFeeIsLastDay = %d\n", settings->GetDayFeeIsLastDay());
424
425 if (!settings->GetDayFeeIsLastDay())
426     {
427     printfd(__FILE__, "DayResetTraff - 1 -\n");
428     DayResetTraff(t1);
429     //printfd(__FILE__, "DayResetTraff - 1 - 1 -\n");
430     }
431
432 if (settings->GetSpreadFee())
433     {
434     printfd(__FILE__, "Spread DayFee\n");
435     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFeeSpread));
436     }
437 else
438     {
439     if (t.tm_mday == dayFee)
440         {
441         printfd(__FILE__, "DayFee\n");
442         for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFee));
443         }
444     }
445
446 if (settings->GetDayFeeIsLastDay())
447     {
448     printfd(__FILE__, "DayResetTraff - 2 -\n");
449     DayResetTraff(t1);
450     }
451 }
452 //-----------------------------------------------------------------------------
453 void USERS_IMPL::DayResetTraff(const struct tm & t1)
454 {
455 int dayResetTraff = settings->GetDayResetTraff();
456 if (dayResetTraff == 0)
457     dayResetTraff = DaysInCurrentMonth();
458 if (t1.tm_mday == dayResetTraff)
459     {
460     printfd(__FILE__, "ResetTraff\n");
461     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessNewMonth));
462     //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff));
463     }
464 }
465 //-----------------------------------------------------------------------------
466 int USERS_IMPL::Start()
467 {
468 if (ReadUsers())
469     {
470     WriteServLog("USERS: Error: Cannot read users!");
471     return -1;
472     }
473
474 nonstop = true;
475 if (pthread_create(&thread, NULL, Run, this))
476     {
477     WriteServLog("USERS: Error: Cannot start thread!");
478     return -1;
479     }
480 return 0;
481 }
482 //-----------------------------------------------------------------------------
483 int USERS_IMPL::Stop()
484 {
485 printfd(__FILE__, "USERS::Stop()\n");
486
487 if (!isRunning)
488     {
489     //printfd(__FILE__, "Alredy stopped\n");
490     return 0;
491     }
492
493 nonstop = false;
494
495 //5 seconds to thread stops itself
496 struct timespec ts = {0, 200000000};
497 for (size_t i = 0; i < 25 * (users.size() / 50 + 1); i++)
498     {
499     if (!isRunning)
500         break;
501
502     nanosleep(&ts, NULL);
503     }
504
505 //after 5 seconds waiting thread still running. now kill it
506 if (isRunning)
507     {
508     printfd(__FILE__, "kill USERS thread.\n");
509     //TODO pthread_cancel()
510     if (pthread_kill(thread, SIGINT))
511         {
512         //errorStr = "Cannot kill USERS thread.";
513         //printfd(__FILE__, "Cannot kill USERS thread.\n");
514         //return 0;
515         }
516     printfd(__FILE__, "USERS killed\n");
517     }
518
519 printfd(__FILE__, "Before USERS::Run()\n");
520 for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
521
522 // 'cause bind2st accepts only constant first param
523 for (list<USER_IMPL>::iterator it = users.begin();
524      it != users.end();
525      ++it)
526     it->WriteDetailStat(true);
527
528 for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteStat));
529 //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf));
530
531 printfd(__FILE__, "USERS::Stop()\n");
532 return 0;
533 }
534 //-----------------------------------------------------------------------------
535 void USERS_IMPL::RealDelUser()
536 {
537 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
538
539 printfd(__FILE__, "RealDelUser() users to del: %d\n", usersToDelete.size());
540
541 list<USER_TO_DEL>::iterator iter;
542 iter = usersToDelete.begin();
543 while (iter != usersToDelete.end())
544     {
545     printfd(__FILE__, "RealDelUser() user=%s\n", iter->iter->GetLogin().c_str());
546     if (iter->delTime + userDeleteDelayTime < stgTime)
547         {
548         printfd(__FILE__, "RealDelUser() user=%s removed from DB\n", iter->iter->GetLogin().c_str());
549         if (store->DelUser(iter->iter->GetLogin()))
550             {
551             WriteServLog("Error removing user \'%s\' from database.", iter->iter->GetLogin().c_str());
552             }
553         users.erase(iter->iter);
554         usersToDelete.erase(iter++);
555         }
556     else
557         {
558         ++iter;
559         }
560     }
561 return;
562 }
563 //-----------------------------------------------------------------------------
564 void USERS_IMPL::AddToIPIdx(user_iter user)
565 {
566 printfd(__FILE__, "USERS: Add IP Idx\n");
567 uint32_t ip = user->GetCurrIP();
568 //assert(ip && "User has non-null ip");
569 if (!ip)
570     return; // User has disconnected
571
572 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
573
574 const map<uint32_t, user_iter>::iterator it(
575         ipIndex.lower_bound(ip)
576 );
577
578 assert((it == ipIndex.end() || it->first != ip) && "User is not in index");
579
580 ipIndex.insert(it, std::make_pair(ip, user));
581 }
582 //-----------------------------------------------------------------------------
583 void USERS_IMPL::DelFromIPIdx(uint32_t ip)
584 {
585 printfd(__FILE__, "USERS: Del IP Idx\n");
586 assert(ip && "User has non-null ip");
587
588 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
589
590 const map<uint32_t, user_iter>::iterator it(
591         ipIndex.find(ip)
592 );
593
594 //assert(it != ipIndex.end() && "User is in index");
595 if (it == ipIndex.end())
596     return; // User has not been added
597
598 ipIndex.erase(it);
599 }
600 //-----------------------------------------------------------------------------
601 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_PTR * usr) const
602 {
603     USER_IMPL * ptr = NULL;
604     if (FindByIPIdx(ip, &ptr))
605         return -1;
606     *usr = ptr;
607     return 0;
608 }
609 //-----------------------------------------------------------------------------
610 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_IMPL ** usr) const
611 {
612 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
613
614 map<uint32_t, user_iter>::const_iterator it;
615 it = ipIndex.find(ip);
616
617 if (it == ipIndex.end())
618     {
619     //printfd(__FILE__, "User NOT found in IP_Index!!!\n");
620     return -1;
621     }
622 *usr = &(*it->second);
623 //printfd(__FILE__, "User found in IP_Index\n");
624 return 0;
625 }
626 //-----------------------------------------------------------------------------
627 bool USERS_IMPL::IsIPInIndex(uint32_t ip) const
628 {
629 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
630
631 map<uint32_t, user_iter>::const_iterator it;
632 it = ipIndex.find(ip);
633
634 return it != ipIndex.end();
635 }
636 //-----------------------------------------------------------------------------
637 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
638 {
639 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
640 onAddNotifiers.insert(n);
641 }
642 //-----------------------------------------------------------------------------
643 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
644 {
645 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
646 onAddNotifiers.erase(n);
647 }
648 //-----------------------------------------------------------------------------
649 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
650 {
651 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
652 onDelNotifiers.insert(n);
653 }
654 //-----------------------------------------------------------------------------
655 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
656 {
657 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
658 onDelNotifiers.erase(n);
659 }
660 //-----------------------------------------------------------------------------
661 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
662 {
663 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
664 onAddNotifiersImpl.insert(n);
665 }
666 //-----------------------------------------------------------------------------
667 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
668 {
669 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
670 onAddNotifiersImpl.erase(n);
671 }
672 //-----------------------------------------------------------------------------
673 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
674 {
675 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
676 onDelNotifiersImpl.insert(n);
677 }
678 //-----------------------------------------------------------------------------
679 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
680 {
681 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
682 onDelNotifiersImpl.erase(n);
683 }
684 //-----------------------------------------------------------------------------
685 int USERS_IMPL::OpenSearch()
686 {
687 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
688 handle++;
689 searchDescriptors[handle] = users.begin();
690 return handle;
691 }
692 //-----------------------------------------------------------------------------
693 int USERS_IMPL::SearchNext(int h, USER_PTR * user)
694 {
695     USER_IMPL * ptr = NULL;
696     if (SearchNext(h, &ptr))
697         return -1;
698     *user = ptr;
699     return 0;
700 }
701 //-----------------------------------------------------------------------------
702 int USERS_IMPL::SearchNext(int h, USER_IMPL ** user)
703 {
704 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
705
706 if (searchDescriptors.find(h) == searchDescriptors.end())
707     {
708     WriteServLog("USERS. Incorrect search handle.");
709     return -1;
710     }
711
712 if (searchDescriptors[h] == users.end())
713     return -1;
714
715 while (searchDescriptors[h]->GetDeleted())
716     {
717     ++searchDescriptors[h];
718     if (searchDescriptors[h] == users.end())
719         {
720         return -1;
721         }
722     }
723
724 *user = &(*searchDescriptors[h]);
725
726 ++searchDescriptors[h];
727
728 return 0;
729 }
730 //-----------------------------------------------------------------------------
731 int USERS_IMPL::CloseSearch(int h)
732 {
733 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
734 if (searchDescriptors.find(h) != searchDescriptors.end())
735     {
736     searchDescriptors.erase(searchDescriptors.find(h));
737     return 0;
738     }
739
740 WriteServLog("USERS. Incorrect search handle.");
741 return -1;
742 }
743 //-----------------------------------------------------------------------------
744 void USERS_IMPL::SetUserNotifiers(user_iter user)
745 {
746 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
747
748 PROPERTY_NOTIFER_IP_BEFORE nb(*this, user);
749 PROPERTY_NOTIFER_IP_AFTER  na(*this, user);
750
751 userIPNotifiersBefore.push_front(nb);
752 userIPNotifiersAfter.push_front(na);
753
754 user->AddCurrIPBeforeNotifier(&(*userIPNotifiersBefore.begin()));
755 user->AddCurrIPAfterNotifier(&(*userIPNotifiersAfter.begin()));
756 }
757 //-----------------------------------------------------------------------------
758 void USERS_IMPL::UnSetUserNotifiers(user_iter user)
759 {
760 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
761
762 list<PROPERTY_NOTIFER_IP_BEFORE>::iterator  bi;
763 list<PROPERTY_NOTIFER_IP_AFTER>::iterator   ai;
764
765 bi = userIPNotifiersBefore.begin();
766 while (bi != userIPNotifiersBefore.end())
767     {
768     if (bi->GetUser() == user)
769         {
770         bi->GetUser()->DelCurrIPBeforeNotifier(&(*bi));
771         userIPNotifiersBefore.erase(bi);
772         //printfd(__FILE__, "Notifier Before removed. User %s\n", bi->GetUser()->GetLogin().c_str());
773         break;
774         }
775     ++bi;
776     }
777
778 ai = userIPNotifiersAfter.begin();
779 while (ai != userIPNotifiersAfter.end())
780     {
781     if (ai->GetUser() == user)
782         {
783         ai->GetUser()->DelCurrIPAfterNotifier(&(*ai));
784         userIPNotifiersAfter.erase(ai);
785         //printfd(__FILE__, "Notifier After removed. User %s\n", ai->GetUser()->GetLogin().c_str());
786         break;
787         }
788     ++ai;
789     }
790 }
791 //-----------------------------------------------------------------------------
792 void USERS_IMPL::AddUserIntoIndexes(user_iter user)
793 {
794 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
795 loginIndex.insert(make_pair(user->GetLogin(), user));
796 }
797 //-----------------------------------------------------------------------------
798 void USERS_IMPL::DelUserFromIndexes(user_iter user)
799 {
800 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
801 loginIndex.erase(user->GetLogin());
802 }
803 //-----------------------------------------------------------------------------
804 bool USERS_IMPL::TimeToWriteDetailStat(const struct tm & t)
805 {
806 int statTime = settings->GetDetailStatWritePeriod();
807
808 switch (statTime)
809     {
810     case dsPeriod_1:
811         if (t.tm_min == 0)
812             return true;
813         break;
814     case dsPeriod_1_2:
815         if (t.tm_min % 30 == 0)
816             return true;
817         break;
818     case dsPeriod_1_4:
819         if (t.tm_min % 15 == 0)
820             return true;
821         break;
822     case dsPeriod_1_6:
823         if (t.tm_min % 10 == 0)
824             return true;
825         break;
826     }
827 return false;
828 }