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