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