-USERS_IMPL::USERS_IMPL(SETTINGS_IMPL * s, STORE * st,
- TARIFFS * t, SERVICES & svcs,
- const ADMIN * sa)
+UsersImpl::UsersImpl(SettingsImpl * s, Store * store,
+ Tariffs * tariffs, Services & svcs,
+ const Admin& sysAdmin)
-pthread_mutexattr_t attr;
-pthread_mutexattr_init(&attr);
-pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-pthread_mutex_init(&mutex, &attr);
-pthread_mutex_destroy(&mutex);
-}
-//-----------------------------------------------------------------------------
-int USERS_IMPL::FindByNameNonLock(const std::string & login, user_iter * user)
-{
-const std::map<std::string, user_iter>::const_iterator iter(loginIndex.find(login));
+const auto iter = loginIndex.find(login);
-STG_LOCKER lock(&mutex);
-const std::map<std::string, user_iter>::const_iterator iter(loginIndex.find(login));
+std::lock_guard<std::mutex> lock(m_mutex);
+const auto iter = loginIndex.find(login);
return iter != loginIndex.end();
}
//-----------------------------------------------------------------------------
return iter != loginIndex.end();
}
//-----------------------------------------------------------------------------
- admin->GetLogStr().c_str(), login.c_str());
- /*errorStr = "Admin \'" + admin->GetLogin() +
- "\': tried to add user \'" + ud->login + "\'. Access denied.";*/
+ admin->logStr().c_str(), login.c_str());
-//////
-if (store->AddUser(login))
- {
- //TODO
- //WriteServLog("Admin \'%s\': tried to add user \'%s\'. Access denied.",
- // admin->GetLogin().c_str(), ud->login.c_str());
+if (m_store->AddUser(login) != 0)
-tms = localtime(&t);
-
-tms->tm_hour = 0;
-tms->tm_min = 0;
-tms->tm_sec = 0;
-
-if (settings->GetDayResetTraff() > tms->tm_mday)
- tms->tm_mon -= 1;
-
-tms->tm_mday = settings->GetDayResetTraff();*/
+UserImpl u(settings, m_store, m_tariffs, &m_sysAdmin, this, m_services);
-bool USERS_IMPL::Authorize(const std::string & login, uint32_t ip,
- uint32_t enabledDirs, const AUTH * auth)
+bool UsersImpl::Authorize(const std::string & login, uint32_t ip,
+ uint32_t enabledDirs, const Auth * auth)
-STG_LOCKER lock(&mutex);
-if (FindByNameNonLock(login, &iter))
+std::lock_guard<std::mutex> lock(m_mutex);
+if (!FindByNameNonLock(login, &iter))
-STG_LOCKER lock(&mutex);
-if (FindByNameNonLock(login, &iter))
+std::lock_guard<std::mutex> lock(m_mutex);
+if (!FindByNameNonLock(login, &iter))
- USER_IMPL u(settings, store, tariffs, sysAdmin, this, m_services);
+ UserImpl u(settings, m_store, m_tariffs, &m_sysAdmin, this, m_services);
printfd(__FILE__,"Day = %d Min = %d\n", day, min);
time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
printfd(__FILE__,"Day = %d Min = %d\n", day, min);
time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
-std::string monFile = us->settings->GetMonitorDir() + "/users_r";
-printfd(__FILE__, "Monitor=%d file USERS %s\n", us->settings->GetMonitoring(), monFile.c_str());
+std::string monFile = settings->GetMonitorDir() + "/users_r";
+printfd(__FILE__, "Monitor=%d file USERS %s\n", settings->GetMonitoring(), monFile.c_str());
{
//printfd(__FILE__,"New Minute. old = %02d current = %02d\n", min, t->tm_min);
//printfd(__FILE__,"New Day. old = %2d current = %2d\n", day, t->tm_mday);
{
//printfd(__FILE__,"New Minute. old = %02d current = %02d\n", min, t->tm_min);
//printfd(__FILE__,"New Day. old = %2d current = %2d\n", day, t->tm_mday);
printfd(__FILE__,"New Minute. old = %d current = %d\n", min, t.tm_min);
min = t.tm_min;
printfd(__FILE__,"New Minute. old = %d current = %d\n", min, t.tm_min);
min = t.tm_min;
printfd(__FILE__,"Sec = %d\n", stgTime);
printfd(__FILE__,"New Day. old = %d current = %d\n", day, t.tm_mday);
day = t.tm_mday;
printfd(__FILE__,"Sec = %d\n", stgTime);
printfd(__FILE__,"New Day. old = %d current = %d\n", day, t.tm_mday);
day = t.tm_mday;
{
//printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
touchTime = stgTime;
{
//printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
touchTime = stgTime;
-std::list<USER_TO_DEL>::iterator iter;
-iter = us->usersToDelete.begin();
-while (iter != us->usersToDelete.end())
+auto iter = usersToDelete.begin();
+while (iter != usersToDelete.end())
{
//Write traff, reset session traff. Fake disconnect-connect
if (t.tm_hour == 23 && t.tm_min == 59)
{
printfd(__FILE__,"MidnightResetSessionStat\n");
{
//Write traff, reset session traff. Fake disconnect-connect
if (t.tm_hour == 23 && t.tm_min == 59)
{
printfd(__FILE__,"MidnightResetSessionStat\n");
if (settings->GetSpreadFee())
{
printfd(__FILE__, "Spread DayFee\n");
if (settings->GetSpreadFee())
{
printfd(__FILE__, "Spread DayFee\n");
-std::for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessDailyFee));
-std::for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessServices));
+std::for_each(users.begin(), users.end(), [](auto& user){ user.ProcessDailyFee(); });
+std::for_each(users.begin(), users.end(), [](auto& user){ user.ProcessServices(); });
if (dayResetTraff == 0)
dayResetTraff = DaysInCurrentMonth();
if (t1.tm_mday == dayResetTraff)
{
printfd(__FILE__, "ResetTraff\n");
if (dayResetTraff == 0)
dayResetTraff = DaysInCurrentMonth();
if (t1.tm_mday == dayResetTraff)
{
printfd(__FILE__, "ResetTraff\n");
- for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessNewMonth));
- //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff));
+ for_each(users.begin(), users.end(), [](auto& user){ user.ProcessNewMonth(); });
+ //for_each(users.begin(), users.end(), mem_fun_ref(&UserImpl::SetPrepaidTraff));
-nonstop = true;
-if (pthread_create(&thread, NULL, Run, this))
- {
- WriteServLog("USERS: Error: Cannot start thread!");
- return -1;
- }
+m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
//5 seconds to thread stops itself
struct timespec ts = {0, 200000000};
//5 seconds to thread stops itself
struct timespec ts = {0, 200000000};
- if (pthread_kill(thread, SIGINT))
- {
- //errorStr = "Cannot kill USERS thread.";
- //printfd(__FILE__, "Cannot kill USERS thread.\n");
- //return 0;
- }
- printfd(__FILE__, "USERS killed\n");
-// 'cause bind2st accepts only constant first param
-for (std::list<USER_IMPL>::iterator it = users.begin();
- it != users.end();
- ++it)
- it->WriteDetailStat(true);
+for (auto& user : users)
+ user.WriteDetailStat(true);
-for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::WriteStat));
-//for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf));
+for_each(users.begin(), users.end(), [](auto& user){ user.WriteStat(); });
+//for_each(users.begin(), users.end(), mem_fun_ref(&UserImpl::WriteConf));
printfd(__FILE__, "USERS::Stop()\n");
return 0;
}
//-----------------------------------------------------------------------------
printfd(__FILE__, "USERS::Stop()\n");
return 0;
}
//-----------------------------------------------------------------------------
while (iter != usersToDelete.end())
{
printfd(__FILE__, "RealDelUser() user=%s\n", iter->iter->GetLogin().c_str());
if (iter->delTime + userDeleteDelayTime < stgTime)
{
printfd(__FILE__, "RealDelUser() user=%s removed from DB\n", iter->iter->GetLogin().c_str());
while (iter != usersToDelete.end())
{
printfd(__FILE__, "RealDelUser() user=%s\n", iter->iter->GetLogin().c_str());
if (iter->delTime + userDeleteDelayTime < stgTime)
{
printfd(__FILE__, "RealDelUser() user=%s removed from DB\n", iter->iter->GetLogin().c_str());
{
printfd(__FILE__, "USERS: Add IP Idx\n");
uint32_t ip = user->GetCurrIP();
//assert(ip && "User has non-null ip");
{
printfd(__FILE__, "USERS: Add IP Idx\n");
uint32_t ip = user->GetCurrIP();
//assert(ip && "User has non-null ip");
assert((it == ipIndex.end() || it->first != ip) && "User is not in index");
ipIndex.insert(it, std::make_pair(ip, user));
}
//-----------------------------------------------------------------------------
assert((it == ipIndex.end() || it->first != ip) && "User is not in index");
ipIndex.insert(it, std::make_pair(ip, user));
}
//-----------------------------------------------------------------------------
{
printfd(__FILE__, "USERS: Del IP Idx\n");
assert(ip && "User has non-null ip");
{
printfd(__FILE__, "USERS: Del IP Idx\n");
assert(ip && "User has non-null ip");
onAddNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
onAddNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
onDelNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
onDelNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
onAddNotifiersImpl.insert(n);
}
//-----------------------------------------------------------------------------
onAddNotifiersImpl.insert(n);
}
//-----------------------------------------------------------------------------
onAddNotifiersImpl.erase(n);
}
//-----------------------------------------------------------------------------
onAddNotifiersImpl.erase(n);
}
//-----------------------------------------------------------------------------
onDelNotifiersImpl.insert(n);
}
//-----------------------------------------------------------------------------
onDelNotifiersImpl.insert(n);
}
//-----------------------------------------------------------------------------
onDelNotifiersImpl.erase(n);
}
//-----------------------------------------------------------------------------
onDelNotifiersImpl.erase(n);
}
//-----------------------------------------------------------------------------
handle++;
searchDescriptors[handle] = users.begin();
return handle;
}
//-----------------------------------------------------------------------------
handle++;
searchDescriptors[handle] = users.begin();
return handle;
}
//-----------------------------------------------------------------------------
- USER_IMPL * ptr = NULL;
- if (SearchNext(h, &ptr))
+ UserImpl * ptr = nullptr;
+ if (SearchNext(h, &ptr) != 0)
if (searchDescriptors.find(h) != searchDescriptors.end())
{
searchDescriptors.erase(searchDescriptors.find(h));
if (searchDescriptors.find(h) != searchDescriptors.end())
{
searchDescriptors.erase(searchDescriptors.find(h));
loginIndex.insert(make_pair(user->GetLogin(), user));
}
//-----------------------------------------------------------------------------
loginIndex.insert(make_pair(user->GetLogin(), user));
}
//-----------------------------------------------------------------------------
loginIndex.erase(user->GetLogin());
}
//-----------------------------------------------------------------------------
loginIndex.erase(user->GetLogin());
}
//-----------------------------------------------------------------------------