From 6f7160dd027c8ce7e2760aa369768ba39ab80181 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 17 Mar 2011 12:26:37 +0200 Subject: [PATCH 1/1] Fix compilation errors after splitting classes into interface and implementation --- projects/stargazer/admin_impl.cpp | 34 +++---- projects/stargazer/admin_impl.h | 52 +++++------ projects/stargazer/admins_impl.cpp | 55 ++++++------ projects/stargazer/admins_impl.h | 55 ++++++------ projects/stargazer/tariff_impl.cpp | 33 +++---- projects/stargazer/tariff_impl.h | 41 +++++---- projects/stargazer/tariffs_impl.cpp | 46 +++++----- projects/stargazer/tariffs_impl.h | 19 ++-- projects/stargazer/user_impl.cpp | 110 +++++++++++------------ projects/stargazer/user_impl.h | 74 ++++++++------- projects/stargazer/users_impl.cpp | 135 +++++++++++++++------------- projects/stargazer/users_impl.h | 101 +++++++++++---------- 12 files changed, 372 insertions(+), 383 deletions(-) diff --git a/projects/stargazer/admin_impl.cpp b/projects/stargazer/admin_impl.cpp index 867717aa..d14bfa7d 100644 --- a/projects/stargazer/admin_impl.cpp +++ b/projects/stargazer/admin_impl.cpp @@ -28,73 +28,75 @@ $Author: faust $ */ -#include "admin.h" +#include "admin_impl.h" #include "common.h" //----------------------------------------------------------------------------- -ADMIN::ADMIN() +ADMIN_IMPL::ADMIN_IMPL() : conf(), ip(0), WriteServLog(GetStgLogger()) { } //----------------------------------------------------------------------------- -ADMIN::ADMIN(const ADMIN_CONF & ac) +ADMIN_IMPL::ADMIN_IMPL(const ADMIN_CONF & ac) : conf(ac), ip(0), WriteServLog(GetStgLogger()) { } //----------------------------------------------------------------------------- -ADMIN::ADMIN(const PRIV & priv, const std::string & login, const std::string & password) +ADMIN_IMPL::ADMIN_IMPL(const PRIV & priv, + const std::string & login, + const std::string & password) : conf(priv, login, password), ip(0), WriteServLog(GetStgLogger()) { } //----------------------------------------------------------------------------- -ADMIN & ADMIN::operator=(const ADMIN & adm) +ADMIN & ADMIN_IMPL::operator=(const ADMIN & adm) { if (&adm == this) return *this; -conf = adm.conf; -ip = adm.ip; +conf = adm.GetConf(); +ip = adm.GetIP(); return *this; } //----------------------------------------------------------------------------- -ADMIN & ADMIN::operator=(const ADMIN_CONF & ac) +ADMIN & ADMIN_IMPL::operator=(const ADMIN_CONF & ac) { conf = ac; return *this; } //----------------------------------------------------------------------------- -bool ADMIN::operator==(const ADMIN & rhs) const +bool ADMIN_IMPL::operator==(const ADMIN & rhs) const { return conf.login == rhs.GetLogin(); } //----------------------------------------------------------------------------- -bool ADMIN::operator!=(const ADMIN & rhs) const +bool ADMIN_IMPL::operator!=(const ADMIN & rhs) const { return conf.login != rhs.GetLogin(); } //----------------------------------------------------------------------------- -bool ADMIN::operator<(const ADMIN & rhs) const +bool ADMIN_IMPL::operator<(const ADMIN & rhs) const { return conf.login < rhs.GetLogin(); } //----------------------------------------------------------------------------- -bool ADMIN::operator<=(const ADMIN & rhs) const +bool ADMIN_IMPL::operator<=(const ADMIN & rhs) const { return conf.login <= rhs.GetLogin(); } //----------------------------------------------------------------------------- -string ADMIN::GetAdminIPStr() const +std::string ADMIN_IMPL::GetIPStr() const { return inet_ntostring(ip); } //----------------------------------------------------------------------------- -void ADMIN::PrintAdmin() const +void ADMIN_IMPL::Print() const { printfd(__FILE__, "=======================================\n"); printfd(__FILE__, "login %s\n", conf.login.c_str()); @@ -108,8 +110,8 @@ printfd(__FILE__, "ChgTariff %d\n", conf.priv.tariffChg); printfd(__FILE__, "=======================================\n"); } //----------------------------------------------------------------------------- -const string ADMIN::GetLogStr() const +const std::string ADMIN_IMPL::GetLogStr() const { -return "Admin \'" + conf.login + "\', " + GetAdminIPStr() + ":"; +return "Admin \'" + conf.login + "\', " + GetIPStr() + ":"; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/admin_impl.h b/projects/stargazer/admin_impl.h index 45979ab7..8dd8a898 100644 --- a/projects/stargazer/admin_impl.h +++ b/projects/stargazer/admin_impl.h @@ -28,51 +28,47 @@ $Author: faust $ */ -#ifndef ADMIN_H -#define ADMIN_H +#ifndef ADMIN_IMPL_H +#define ADMIN_IMPL_H #include +#include "admin.h" #include "os_int.h" #include "admin_conf.h" #include "stg_logger.h" -using namespace std; - -//----------------------------------------------------------------------------- -class ADMIN -{ +class ADMIN_IMPL : public ADMIN { public: - ADMIN(); - ADMIN(const ADMIN_CONF & ac); - ADMIN(const PRIV & priv, + ADMIN_IMPL(); + ADMIN_IMPL(const ADMIN_CONF & ac); + ADMIN_IMPL(const PRIV & priv, const std::string & login, const std::string & password); - ~ADMIN() {}; + virtual ~ADMIN_IMPL() {}; - ADMIN & operator=(const ADMIN &); - ADMIN & operator=(const ADMIN_CONF &); - bool operator==(const ADMIN & rhs) const; - bool operator!=(const ADMIN & rhs) const; - bool operator<(const ADMIN & rhs) const; - bool operator<=(const ADMIN & rhs) const; + ADMIN & operator=(const ADMIN &); + ADMIN & operator=(const ADMIN_CONF &); + bool operator==(const ADMIN & rhs) const; + bool operator!=(const ADMIN & rhs) const; + bool operator<(const ADMIN & rhs) const; + bool operator<=(const ADMIN & rhs) const; - const string & GetPassword() const { return conf.password; }; - const string & GetLogin() const { return conf.login; }; - PRIV const * GetPriv() const { return &conf.priv; }; - uint16_t GetPrivAsInt() const { return conf.priv.ToInt(); }; - const ADMIN_CONF & GetConf() const { return conf; }; - void PrintAdmin() const; - uint32_t GetAdminIP() const { return ip; }; - string GetAdminIPStr() const; - void SetAdminIP(uint32_t ip) { ADMIN::ip = ip; }; - const string GetLogStr() const; + const std::string & GetPassword() const { return conf.password; }; + const std::string & GetLogin() const { return conf.login; }; + PRIV const * GetPriv() const { return &conf.priv; }; + uint16_t GetPrivAsInt() const { return conf.priv.ToInt(); }; + const ADMIN_CONF & GetConf() const { return conf; }; + void Print() const; + uint32_t GetIP() const { return ip; }; + std::string GetIPStr() const; + void SetIP(uint32_t ip) { ADMIN_IMPL::ip = ip; }; + const std::string GetLogStr() const; private: ADMIN_CONF conf; uint32_t ip; STG_LOGGER & WriteServLog; }; -//----------------------------------------------------------------------------- #endif diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 4e89c9fd..20bd8ef8 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -32,14 +32,16 @@ #include #include +#include "admins_impl.h" #include "admins.h" #include "admin.h" +#include "admin_impl.h" #include "common.h" using namespace std; //----------------------------------------------------------------------------- -ADMINS::ADMINS(BASE_STORE * st) +ADMINS_IMPL::ADMINS_IMPL(BASE_STORE * st) : stg(0xFFFF, "@stargazer", ""), noAdmin(0xFFFF, "NO-ADMIN", ""), data(), @@ -52,7 +54,7 @@ pthread_mutex_init(&mutex, NULL); ReadAdmins(); } //----------------------------------------------------------------------------- -int ADMINS::Add(const string & login, const ADMIN & admin) +int ADMINS_IMPL::Add(const string & login, const ADMIN & admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); const PRIV * priv = admin.GetPriv(); @@ -65,7 +67,7 @@ if (!priv->adminChg) return -1; } -ADMIN adm(0, login, ""); +ADMIN_IMPL adm(0, login, ""); admin_iter ai(find(data.begin(), data.end(), adm)); if (ai != data.end()) @@ -77,9 +79,8 @@ if (ai != data.end()) } data.push_back(adm); -/*ADMIN_CONF ac; -ac.login = login;*/ -if (store->AddAdmin(login) == 0 /*&& store->SaveAdmin(ac) == 0*/) + +if (store->AddAdmin(login) == 0) { WriteServLog("%s Administrator \'%s\' added.", admin.GetLogStr().c_str(), login.c_str()); @@ -92,10 +93,10 @@ WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str()); return -1; } //----------------------------------------------------------------------------- -int ADMINS::Del(const string & login, const ADMIN & admin) +int ADMINS_IMPL::Del(const string & login, const ADMIN & admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN adm(0, login, ""); +ADMIN_IMPL adm(0, login, ""); const PRIV * priv = admin.GetPriv(); if (!priv->adminChg) @@ -137,7 +138,7 @@ WriteServLog("%s Administrator \'%s\' deleted.", admin.GetLogStr().c_str(), logi return 0; } //----------------------------------------------------------------------------- -int ADMINS::Change(const ADMIN_CONF & ac, const ADMIN & admin) +int ADMINS_IMPL::Change(const ADMIN_CONF & ac, const ADMIN & admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); const PRIV * priv = admin.GetPriv(); @@ -150,7 +151,7 @@ if (!priv->adminChg) return -1; } -ADMIN adm(0, ac.login, ""); +ADMIN_IMPL adm(0, ac.login, ""); admin_iter ai(find(data.begin(), data.end(), adm)); if (ai == data.end()) @@ -174,7 +175,7 @@ WriteServLog("%s Administrator \'%s\' changed.", return 0; } //----------------------------------------------------------------------------- -int ADMINS::ReadAdmins() +int ADMINS_IMPL::ReadAdmins() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); vector adminsList; @@ -194,23 +195,23 @@ for (unsigned int i = 0; i < adminsList.size(); i++) return -1; } - data.push_back(ADMIN(ac)); + data.push_back(ADMIN_IMPL(ac)); } return 0; } //----------------------------------------------------------------------------- -void ADMINS::PrintAdmins() const +void ADMINS_IMPL::PrintAdmins() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); const_admin_iter ai(data.begin()); while (ai != data.end()) { - ai->PrintAdmin(); + ai->Print(); ai++; } } //----------------------------------------------------------------------------- -bool ADMINS::FindAdmin(const string & l, ADMIN * admin) const +bool ADMINS_IMPL::FindAdmin(const string & l, ADMIN ** admin) { assert(admin != NULL && "Pointer to admin is not null"); @@ -218,23 +219,23 @@ STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) { printfd(__FILE__, "no admin in system!\n"); - *admin = noAdmin; + *admin = &noAdmin; return false; } -ADMIN adm(0, l, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +ADMIN_IMPL adm(0, l, ""); +admin_iter ai(find(data.begin(), data.end(), adm)); if (ai != data.end()) { - *admin = *ai; + *admin = &(*ai); return false; } return true; } //----------------------------------------------------------------------------- -bool ADMINS::AdminExists(const string & login) const +bool ADMINS_IMPL::AdminExists(const string & login) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) @@ -243,7 +244,7 @@ if (data.empty()) return true; } -ADMIN adm(0, login, ""); +ADMIN_IMPL adm(0, login, ""); const_admin_iter ai(find(data.begin(), data.end(), adm)); if (ai != data.end()) @@ -252,7 +253,7 @@ if (ai != data.end()) return false; } //----------------------------------------------------------------------------- -bool ADMINS::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const +bool ADMINS_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) @@ -261,7 +262,7 @@ if (data.empty()) return true; } -ADMIN adm(0, login, ""); +ADMIN_IMPL adm(0, login, ""); const_admin_iter ai(find(data.begin(), data.end(), adm)); if (ai == data.end()) @@ -279,7 +280,7 @@ if (ai->GetPassword() != password) return true; } //----------------------------------------------------------------------------- -int ADMINS::OpenSearch() const +int ADMINS_IMPL::OpenSearch() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); handle++; @@ -287,7 +288,7 @@ searchDescriptors[handle] = data.begin(); return handle; } //----------------------------------------------------------------------------- -int ADMINS::SearchNext(int h, ADMIN_CONF * ac) const +int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (searchDescriptors.find(h) == searchDescriptors.end()) @@ -299,14 +300,14 @@ if (searchDescriptors.find(h) == searchDescriptors.end()) if (searchDescriptors[h] == data.end()) return -1; -ADMIN a = *searchDescriptors[h]++; +ADMIN_IMPL a = *searchDescriptors[h]++; *ac = a.GetConf(); return 0; } //----------------------------------------------------------------------------- -int ADMINS::CloseSearch(int h) const +int ADMINS_IMPL::CloseSearch(int h) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (searchDescriptors.find(h) != searchDescriptors.end()) diff --git a/projects/stargazer/admins_impl.h b/projects/stargazer/admins_impl.h index 09933230..8318fc21 100644 --- a/projects/stargazer/admins_impl.h +++ b/projects/stargazer/admins_impl.h @@ -28,61 +28,56 @@ $Author: faust $ */ -#ifndef ADMINS_H -#define ADMINS_H +#ifndef ADMINS_IMPL_H +#define ADMINS_IMPL_H #include #include #include +#include "admins.h" #include "admin.h" +#include "admin_impl.h" #include "stg_locker.h" #include "base_store.h" #include "noncopyable.h" -using namespace std; - -//----------------------------------------------------------------------------- -class ADMINS : private NONCOPYABLE -{ +class ADMINS_IMPL : private NONCOPYABLE, public ADMINS { public: - ADMINS(BASE_STORE * st); - ~ADMINS() {}; + ADMINS_IMPL(BASE_STORE * st); + virtual ~ADMINS_IMPL() {}; - int Add(const string & login, const ADMIN & admin); - int Del(const string & login, const ADMIN & admin); - int Change(const ADMIN_CONF & ac, const ADMIN & admin); - void PrintAdmins() const; - const ADMIN GetSysAdmin() const { return stg; }; - const ADMIN GetNoAdmin() const { return noAdmin; }; - bool FindAdmin(const string & l, ADMIN * admin) const; - bool AdminExists(const std::string & login) const; - bool AdminCorrect(const std::string & login, - const std::string & password, - ADMIN * admin) const; - const string & GetStrError() const { return strError; }; + int Add(const string & login, const ADMIN & admin); + int Del(const string & login, const ADMIN & admin); + int Change(const ADMIN_CONF & ac, const ADMIN & admin); + void PrintAdmins() const; + const ADMIN * GetSysAdmin() const { return &stg; }; + const ADMIN * GetNoAdmin() const { return &noAdmin; }; + bool FindAdmin(const std::string & l, ADMIN ** admin); + bool AdminExists(const std::string & login) const; + bool AdminCorrect(const std::string & login, + const std::string & password, + ADMIN * admin) const; + const std::string & GetStrError() const { return strError; }; int OpenSearch() const; int SearchNext(int, ADMIN_CONF * ac) const; int CloseSearch(int) const; private: - typedef list::iterator admin_iter; - typedef list::const_iterator const_admin_iter; + typedef list::iterator admin_iter; + typedef list::const_iterator const_admin_iter; int ReadAdmins(); - ADMIN stg; - ADMIN noAdmin; - list data; + ADMIN_IMPL stg; + ADMIN_IMPL noAdmin; + list data; BASE_STORE * store; STG_LOGGER & WriteServLog; mutable map searchDescriptors; mutable unsigned int handle; mutable pthread_mutex_t mutex; - string strError; + std::string strError; }; -//----------------------------------------------------------------------------- #endif - - diff --git a/projects/stargazer/tariff_impl.cpp b/projects/stargazer/tariff_impl.cpp index 70b75576..bfae5215 100644 --- a/projects/stargazer/tariff_impl.cpp +++ b/projects/stargazer/tariff_impl.cpp @@ -31,23 +31,24 @@ #include #include // std::max -#include "tariff.h" +#include "tariff_impl.h" #include "stg_timer.h" +#include "common.h" //----------------------------------------------------------------------------- -TARIFF & TARIFF::operator=(const TARIFF_DATA & td) +TARIFF & TARIFF_IMPL::operator=(const TARIFF_DATA & td) { tariffData = td; return *this; } //----------------------------------------------------------------------------- -TARIFF & TARIFF::operator=(const TARIFF & t) +TARIFF & TARIFF_IMPL::operator=(const TARIFF & t) { -tariffData = t.tariffData; +tariffData = t.GetTariffData(); return *this; } //----------------------------------------------------------------------------- -double TARIFF::GetPriceWithTraffType(uint64_t up, +double TARIFF_IMPL::GetPriceWithTraffType(uint64_t up, uint64_t down, int dir, time_t t) const @@ -55,7 +56,7 @@ double TARIFF::GetPriceWithTraffType(uint64_t up, return GetPriceWithoutFreeMb(dir, GetTraffByType(up, down) / (1024 * 1024), t); } //----------------------------------------------------------------------------- -int64_t TARIFF::GetTraffByType(uint64_t up, uint64_t down) const +int64_t TARIFF_IMPL::GetTraffByType(uint64_t up, uint64_t down) const { switch (tariffData.tariffConf.traffType) { @@ -73,27 +74,17 @@ switch (tariffData.tariffConf.traffType) } } //----------------------------------------------------------------------------- -int TARIFF::GetThreshold(int dir) const +int TARIFF_IMPL::GetThreshold(int dir) const { return tariffData.dirPrice[dir].threshold; } //----------------------------------------------------------------------------- -void TARIFF::PrintTariff() const +void TARIFF_IMPL::Print() const { -//printfd(__FILE__, "Traiff name: %s\n", tariffConf.name.c_str()); -//printfd(__FILE__, "Price: %8.3f %8.3f \n", dirPrice[0].GetPrice(0, 0), dirPrice[0].GetPrice(1, 0)); -//printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[1].GetPrice(0), dirPrice[1].GetPrice(1), dirPrice[1].GetThreshold()); -//printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[2].GetPrice(0), dirPrice[2].GetPrice(1), dirPrice[2].GetThreshold()); -//printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[3].GetPrice(0), dirPrice[3].GetPrice(1), dirPrice[3].GetThreshold()); -//printfd(__FILE__, "Free: %8.3f\n", tariffConf.free); +printfd(__FILE__, "Traiff name: %s\n", tariffData.tariffConf.name.c_str()); } //----------------------------------------------------------------------------- -void TARIFF::GetTariffData(TARIFF_DATA * td) const -{ -*td = tariffData; -} -//----------------------------------------------------------------------------- -int TARIFF::Interval(int dir, time_t t) const +int TARIFF_IMPL::Interval(int dir, time_t t) const { // Start of the day (and end of the night) in sec from 00:00:00 int s1 = tariffData.dirPrice[dir].hDay * 3600 + @@ -128,7 +119,7 @@ else } } //----------------------------------------------------------------------------- -double TARIFF::GetPriceWithoutFreeMb(int dir, int mb, time_t t) const +double TARIFF_IMPL::GetPriceWithoutFreeMb(int dir, int mb, time_t t) const { int interval = Interval(dir, t); diff --git a/projects/stargazer/tariff_impl.h b/projects/stargazer/tariff_impl.h index 2086b488..aaa6a3b9 100644 --- a/projects/stargazer/tariff_impl.h +++ b/projects/stargazer/tariff_impl.h @@ -28,60 +28,60 @@ $Author: faust $ */ -#ifndef TARIFF_H -#define TARIFF_H +#ifndef TARIFF_IMPL_H +#define TARIFF_IMPL_H #include #include #include +#include "tariff.h" #include "os_int.h" #include "tariff_conf.h" #define TARIFF_DAY 0 #define TARIFF_NIGHT 1 -class TARIFF -{ +class TARIFF_IMPL : public TARIFF { public: - TARIFF() + TARIFF_IMPL() : tariffData() {}; - TARIFF(const std::string & name) + TARIFF_IMPL(const std::string & name) : tariffData(name) {}; - TARIFF(const TARIFF_DATA & td) + TARIFF_IMPL(const TARIFF_DATA & td) : tariffData(td) {}; - TARIFF(const TARIFF & t) + TARIFF_IMPL(const TARIFF_IMPL & t) : tariffData(t.tariffData) {}; - ~TARIFF() {}; + virtual ~TARIFF_IMPL() {}; double GetPriceWithTraffType(uint64_t up, uint64_t down, int dir, time_t t) const; - double GetFreeMb() const { return tariffData.tariffConf.free; }; - double GetPassiveCost() const { return tariffData.tariffConf.passiveCost; }; - double GetFee() const { return tariffData.tariffConf.fee; }; - double GetFree() const { return tariffData.tariffConf.free; }; + double GetFreeMb() const { return tariffData.tariffConf.free; } + double GetPassiveCost() const { return tariffData.tariffConf.passiveCost; } + double GetFee() const { return tariffData.tariffConf.fee; } + double GetFree() const { return tariffData.tariffConf.free; } - void PrintTariff() const; + void Print() const; - const std::string & GetName() const { return tariffData.tariffConf.name; }; - void SetName(const std::string & name) { tariffData.tariffConf.name = name; }; + const std::string & GetName() const { return tariffData.tariffConf.name; } + void SetName(const std::string & name) { tariffData.tariffConf.name = name; } - int GetTraffType() const { return tariffData.tariffConf.traffType; }; + int GetTraffType() const { return tariffData.tariffConf.traffType; } int64_t GetTraffByType(uint64_t up, uint64_t down) const; int GetThreshold(int dir) const; - void GetTariffData(TARIFF_DATA * td) const; + const TARIFF_DATA & GetTariffData() const { return tariffData; } TARIFF & operator=(const TARIFF_DATA & td); TARIFF & operator=(const TARIFF & t); - bool operator==(const TARIFF & rhs) const { return GetName() == rhs.GetName(); }; - bool operator!=(const TARIFF & rhs) const { return GetName() != rhs.GetName(); }; + bool operator==(const TARIFF & rhs) const { return GetName() == rhs.GetName(); } + bool operator!=(const TARIFF & rhs) const { return GetName() != rhs.GetName(); } private: TARIFF_DATA tariffData; @@ -89,6 +89,5 @@ private: double GetPriceWithoutFreeMb(int dir, int mb, time_t t) const; int Interval(int dir, time_t t) const; }; -//----------------------------------------------------------------------------- #endif diff --git a/projects/stargazer/tariffs_impl.cpp b/projects/stargazer/tariffs_impl.cpp index 1f8a4230..2caf17aa 100644 --- a/projects/stargazer/tariffs_impl.cpp +++ b/projects/stargazer/tariffs_impl.cpp @@ -32,7 +32,7 @@ #include #include -#include "tariffs.h" +#include "tariffs_impl.h" #include "stg_locker.h" #include "stg_logger.h" #include "base_store.h" @@ -41,7 +41,7 @@ using namespace std; //----------------------------------------------------------------------------- -TARIFFS::TARIFFS(BASE_STORE * st) +TARIFFS_IMPL::TARIFFS_IMPL(BASE_STORE * st) : tariffs(), store(st), WriteServLog(GetStgLogger()), @@ -52,12 +52,12 @@ pthread_mutex_init(&mutex, NULL); ReadTariffs(); } //----------------------------------------------------------------------------- -TARIFFS::~TARIFFS() +TARIFFS_IMPL::~TARIFFS_IMPL() { pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- -int TARIFFS::ReadTariffs() +int TARIFFS_IMPL::ReadTariffs() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -79,26 +79,26 @@ for (int i = 0; i < tariffsNum; i++) WriteServLog("%s", store->GetStrError().c_str()); return -1; } - tariffs.push_back(TARIFF(td)); + tariffs.push_back(TARIFF_IMPL(td)); } return 0; } //----------------------------------------------------------------------------- -int TARIFFS::GetTariffsNum() const +int TARIFFS_IMPL::GetTariffsNum() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); return tariffs.size(); } //----------------------------------------------------------------------------- -const TARIFF * TARIFFS::FindByName(const string & name) const +const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const { if (name == NO_TARIFF_NAME) return &noTariff; STG_LOCKER lock(&mutex, __FILE__, __LINE__); -list::const_iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF(name)); +list::const_iterator ti; +ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name)); if (ti != tariffs.end()) return &(*ti); @@ -106,7 +106,7 @@ if (ti != tariffs.end()) return NULL; } //----------------------------------------------------------------------------- -int TARIFFS::Chg(const TARIFF_DATA & td, const ADMIN & admin) +int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN & admin) { const PRIV * priv = admin.GetPriv(); @@ -121,8 +121,8 @@ if (!priv->tariffChg) STG_LOCKER lock(&mutex, __FILE__, __LINE__); -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF(td.tariffConf.name)); +list::iterator ti; +ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name)); if (ti == tariffs.end()) { @@ -146,7 +146,7 @@ WriteServLog("%s Tariff \'%s\' changed.", return 0; } //----------------------------------------------------------------------------- -int TARIFFS::Del(const string & name, const ADMIN & admin) +int TARIFFS_IMPL::Del(const string & name, const ADMIN & admin) { const PRIV * priv = admin.GetPriv(); @@ -161,8 +161,8 @@ if (!priv->tariffChg) STG_LOCKER lock(&mutex, __FILE__, __LINE__); -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF(name)); +list::iterator ti; +ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name)); if (ti == tariffs.end()) { @@ -186,7 +186,7 @@ WriteServLog("%s Tariff \'%s\' deleted.", return 0; } //----------------------------------------------------------------------------- -int TARIFFS::Add(const string & name, const ADMIN & admin) +int TARIFFS_IMPL::Add(const string & name, const ADMIN & admin) { const PRIV * priv = admin.GetPriv(); @@ -201,8 +201,8 @@ if (!priv->tariffChg) STG_LOCKER lock(&mutex, __FILE__, __LINE__); -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF(name)); +list::iterator ti; +ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name)); if (ti != tariffs.end()) { @@ -211,7 +211,7 @@ if (ti != tariffs.end()) return -1; } -tariffs.push_back(TARIFF(name)); +tariffs.push_back(TARIFF_IMPL(name)); if (store->AddTariff(name) < 0) { @@ -226,17 +226,15 @@ WriteServLog("%s Tariff \'%s\' added.", return 0; } //----------------------------------------------------------------------------- -void TARIFFS::GetTariffsData(std::list * tdl) +void TARIFFS_IMPL::GetTariffsData(std::list * tdl) { assert(tdl != NULL && "Tariffs data list is not null"); STG_LOCKER lock(&mutex, __FILE__, __LINE__); -std::list::const_iterator it = tariffs.begin(); -TARIFF_DATA td; +std::list::const_iterator it = tariffs.begin(); for (; it != tariffs.end(); ++it) { - it->GetTariffData(&td); - tdl->push_back(td); + tdl->push_back(it->GetTariffData()); } } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/tariffs_impl.h b/projects/stargazer/tariffs_impl.h index 5fce068e..8c2ae4d0 100644 --- a/projects/stargazer/tariffs_impl.h +++ b/projects/stargazer/tariffs_impl.h @@ -33,8 +33,8 @@ $Author: faust $ */ -#ifndef TARIFFS_H -#define TARIFFS_H +#ifndef TARIFFS_IMPL_H +#define TARIFFS_IMPL_H #include @@ -42,6 +42,8 @@ #include #include "tariff.h" +#include "tariff_impl.h" +#include "tariffs.h" #include "tariff_conf.h" #define TARIFF_DAY 0 @@ -51,12 +53,10 @@ class BASE_STORE; class STG_LOGGER; class ADMIN; -//----------------------------------------------------------------------------- -class TARIFFS -{ +class TARIFFS_IMPL : public TARIFFS { public: - TARIFFS(BASE_STORE * store); - ~TARIFFS(); + TARIFFS_IMPL(BASE_STORE * store); + virtual ~TARIFFS_IMPL(); int ReadTariffs (); const TARIFF * FindByName(const std::string & name) const; const TARIFF * GetNoTariff() const { return &noTariff; }; @@ -69,13 +69,12 @@ public: const std::string & GetStrError() const { return strError; }; private: - std::list tariffs; + std::list tariffs; BASE_STORE * store; STG_LOGGER & WriteServLog; mutable pthread_mutex_t mutex; std::string strError; - TARIFF noTariff; + TARIFF_IMPL noTariff; }; -//----------------------------------------------------------------------------- #endif diff --git a/projects/stargazer/user_impl.cpp b/projects/stargazer/user_impl.cpp index d8368c4d..f18dd76e 100644 --- a/projects/stargazer/user_impl.cpp +++ b/projects/stargazer/user_impl.cpp @@ -37,7 +37,8 @@ #include -#include "user.h" +#include "user_impl.h" +#include "users.h" #include "common.h" #include "settings.h" #include "script_executer.h" @@ -45,12 +46,13 @@ #include "tariffs.h" #include "admin.h" -USER::USER(const SETTINGS * s, +USER_IMPL::USER_IMPL(const SETTINGS * s, const BASE_STORE * st, const TARIFFS * t, const ADMIN & a, - const map * ipIdx) - : property(s), + const USERS * u) + : users(u), + property(s), WriteServLog(GetStgLogger()), login(), id(0), @@ -105,7 +107,6 @@ USER::USER(const SETTINGS * s, ipNotifier(this) { settings = s; -ipIndex = ipIdx; password = "*_EMPTY_PASSWORD_*"; tariffName = NO_TARIFF_NAME; @@ -129,8 +130,9 @@ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&mutex, &attr); } //----------------------------------------------------------------------------- -USER::USER(const USER & u) - : property(u.settings), +USER_IMPL::USER_IMPL(const USER_IMPL & u) + : users(u.users), + property(u.settings), WriteServLog(GetStgLogger()), login(u.login), id(u.id), @@ -188,8 +190,6 @@ if (&u == this) connected = 0; -ipIndex = u.ipIndex; - deleted = u.deleted; lastWriteStat = u.lastWriteStat; @@ -212,14 +212,14 @@ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&mutex, &attr); } //----------------------------------------------------------------------------- -USER::~USER() +USER_IMPL::~USER_IMPL() { property.passive.DelBeforeNotifier(&passiveNotifier); property.tariffName.DelBeforeNotifier(&tariffNotifier); pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- -void USER::SetLogin(string const & l) +void USER_IMPL::SetLogin(string const & l) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); assert(login.empty() && "Login is already set"); @@ -227,7 +227,7 @@ login = l; id = userIDGenerator.GetNextID(); } //----------------------------------------------------------------------------- -int USER::ReadConf() +int USER_IMPL::ReadConf() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); USER_CONF conf; @@ -272,7 +272,7 @@ for (it = hdrsList.begin(); it != hdrsList.end(); ++it) return 0; } //----------------------------------------------------------------------------- -int USER::ReadStat() +int USER_IMPL::ReadStat() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); USER_STAT stat; @@ -291,7 +291,7 @@ property.SetStat(stat); return 0; } //----------------------------------------------------------------------------- -int USER::WriteConf() +int USER_IMPL::WriteConf() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); USER_CONF conf(property.GetConf()); @@ -310,7 +310,7 @@ if (store->SaveUserConf(conf, login)) return 0; } //----------------------------------------------------------------------------- -int USER::WriteStat() +int USER_IMPL::WriteStat() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); USER_STAT stat(property.GetStat()); @@ -331,7 +331,7 @@ lastWriteStat = stgTime; return 0; } //----------------------------------------------------------------------------- -int USER::WriteMonthStat() +int USER_IMPL::WriteMonthStat() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); time_t tt = stgTime - 3600; @@ -351,7 +351,7 @@ if (store->SaveMonthStat(stat, t1.tm_mon, t1.tm_year, login)) return 0; } //----------------------------------------------------------------------------- -int USER::Authorize(uint32_t ip, const string &, uint32_t dirs, const BASE_AUTH * auth) +int USER_IMPL::Authorize(uint32_t ip, const string &, uint32_t dirs, const BASE_AUTH * auth) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); /* @@ -383,12 +383,12 @@ if (authorizedBy.size()) return -1; } - map::const_iterator ci = ipIndex->find(ip); - if (ci != ipIndex->end()) + USER * u = NULL; + if (!users->FindByIPIdx(ip, &u)) { // Address is already present in IP-index // If it's not our IP - throw an error - if (&(*ci->second) != this) + if (u != this) { errorStr = "IP address " + inet_ntostring(ip) + " alredy in use"; return -1; @@ -397,7 +397,7 @@ if (authorizedBy.size()) } else { - if (ipIndex->find(ip) != ipIndex->end()) + if (users->IsIPInIndex(ip)) { // Address is already present in IP-index errorStr = "IP address " + inet_ntostring(ip) + " alredy in use"; @@ -424,7 +424,7 @@ ScanMessage(); return 0; } //----------------------------------------------------------------------------- -void USER::Unauthorize(const BASE_AUTH * auth) +void USER_IMPL::Unauthorize(const BASE_AUTH * auth) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); /* @@ -441,14 +441,14 @@ if (authorizedBy.empty()) } } //----------------------------------------------------------------------------- -bool USER::IsAuthorizedBy(const BASE_AUTH * auth) const +bool USER_IMPL::IsAuthorizedBy(const BASE_AUTH * auth) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); // Is this user authorized by specified authorizer? return authorizedBy.find(auth) != authorizedBy.end(); } //----------------------------------------------------------------------------- -void USER::Connect(bool fakeConnect) +void USER_IMPL::Connect(bool fakeConnect) { /* * Connect user to Internet. This function is differ from Authorize() !!! @@ -499,7 +499,7 @@ if (!fakeConnect) lastIPForDisconnect = currIP; } //----------------------------------------------------------------------------- -void USER::Disconnect(bool fakeDisconnect, const std::string & reason) +void USER_IMPL::Disconnect(bool fakeDisconnect, const std::string & reason) { /* * Disconnect user from Internet. This function is differ from UnAuthorize() !!! @@ -561,7 +561,7 @@ sessionUpload = zeroSesssion; sessionDownload = zeroSesssion; } //----------------------------------------------------------------------------- -void USER::PrintUser() const +void USER_IMPL::PrintUser() const { //return; STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -598,7 +598,7 @@ cout << "lastActivityTime=" << lastActivityTime << endl; cout << "============================================================" << endl; } //----------------------------------------------------------------------------- -void USER::Run() +void USER_IMPL::Run() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -659,7 +659,7 @@ else } //----------------------------------------------------------------------------- -void USER::UpdatePingTime(time_t t) +void USER_IMPL::UpdatePingTime(time_t t) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); //printfd(__FILE__, "UpdatePingTime(%d) %s\n", t, login.c_str()); @@ -669,7 +669,7 @@ else pingTime = stgTime; } //----------------------------------------------------------------------------- -bool USER::IsInetable() +bool USER_IMPL::IsInetable() { //STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -690,7 +690,7 @@ if (settings->GetShowFeeInCash()) return (cash - tariff->GetFee() >= -credit); } //----------------------------------------------------------------------------- -string USER::GetEnabledDirs() +string USER_IMPL::GetEnabledDirs() { //STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -701,9 +701,9 @@ return dirs; } //----------------------------------------------------------------------------- #ifdef TRAFF_STAT_WITH_PORTS -void USER::AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len) +void USER_IMPL::AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len) #else -void USER::AddTraffStatU(int dir, uint32_t ip, uint32_t len) +void USER_IMPL::AddTraffStatU(int dir, uint32_t ip, uint32_t len) #endif { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -756,8 +756,8 @@ if (tt == TRAFF_UP || cost = dc - freeMb.ConstData(); // Direct access to internal data structures via friend-specifier - property.stat.freeMb -= dc; - property.stat.cash -= cost; + property.Stat().freeMb -= dc; + property.Stat().cash -= cost; cash.ModifyTime(); freeMb.ModifyTime(); } @@ -793,9 +793,9 @@ else } //----------------------------------------------------------------------------- #ifdef TRAFF_STAT_WITH_PORTS -void USER::AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len) +void USER_IMPL::AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len) #else -void USER::AddTraffStatD(int dir, uint32_t ip, uint32_t len) +void USER_IMPL::AddTraffStatD(int dir, uint32_t ip, uint32_t len) #endif { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -847,8 +847,8 @@ if (tt == TRAFF_DOWN || else if (freeMb.ConstData() < dc) // FreeMb is partially exhausted cost = dc - freeMb.ConstData(); - property.stat.freeMb -= dc; - property.stat.cash -= cost; + property.Stat().freeMb -= dc; + property.Stat().cash -= cost; cash.ModifyTime(); freeMb.ModifyTime(); } @@ -883,31 +883,31 @@ else } } //----------------------------------------------------------------------------- -void USER::AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE * n) +void USER_IMPL::AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); currIP.AddBeforeNotifier(n); } //----------------------------------------------------------------------------- -void USER::DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE * n) +void USER_IMPL::DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); currIP.DelBeforeNotifier(n); } //----------------------------------------------------------------------------- -void USER::AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE * n) +void USER_IMPL::AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); currIP.AddAfterNotifier(n); } //----------------------------------------------------------------------------- -void USER::DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE * n) +void USER_IMPL::DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); currIP.DelAfterNotifier(n); } //----------------------------------------------------------------------------- -void USER::OnAdd() +void USER_IMPL::OnAdd() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -929,7 +929,7 @@ else } } //----------------------------------------------------------------------------- -void USER::OnDelete() +void USER_IMPL::OnDelete() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -953,7 +953,7 @@ else Run(); } //----------------------------------------------------------------------------- -int USER::WriteDetailStat(bool hard) +int USER_IMPL::WriteDetailStat(bool hard) { printfd(__FILE__, "USER::WriteDetailedStat() - saved size = %d\n", traffStatSaved.second.size()); @@ -999,7 +999,7 @@ lastWriteDeatiledStat = stgTime; return 0; } //----------------------------------------------------------------------------- -double USER::GetPassiveTimePart() const +double USER_IMPL::GetPassiveTimePart() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1026,7 +1026,7 @@ if (dt < 0) return double(dt) / (secMonth); } //----------------------------------------------------------------------------- -void USER::SetPassiveTimeAsNewUser() +void USER_IMPL::SetPassiveTimeAsNewUser() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1039,7 +1039,7 @@ double pt = (tm.tm_mday - 1) / (double)daysCurrMon; passiveTime = (time_t)(pt * 24 * 3600 * daysCurrMon); } //----------------------------------------------------------------------------- -void USER::MidnightResetSessionStat() +void USER_IMPL::MidnightResetSessionStat() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1050,7 +1050,7 @@ if (connected) } } //----------------------------------------------------------------------------- -void USER::ProcessNewMonth() +void USER_IMPL::ProcessNewMonth() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); // Reset traff @@ -1090,7 +1090,7 @@ if (nextTariff.ConstData() != "") } } //----------------------------------------------------------------------------- -void USER::ProcessDayFeeSpread() +void USER_IMPL::ProcessDayFeeSpread() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1107,7 +1107,7 @@ property.cash.Set(c - f, sysAdmin, login, store, "Subscriber fee charge"); ResetPassiveTime(); } //----------------------------------------------------------------------------- -void USER::ProcessDayFee() +void USER_IMPL::ProcessDayFee() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1140,14 +1140,14 @@ printfd(__FILE__, "login: %8s Fee=%f PassiveTimePart=%f fee=%f\n", property.cash.Set(c - f, sysAdmin, login, store, "Subscriber fee charge"); } //----------------------------------------------------------------------------- -void USER::SetPrepaidTraff() +void USER_IMPL::SetPrepaidTraff() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); property.freeMb.Set(tariff->GetFree(), sysAdmin, login, store, "Prepaid traffic"); } //----------------------------------------------------------------------------- -int USER::AddMessage(STG_MSG * msg) +int USER_IMPL::AddMessage(STG_MSG * msg) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1186,7 +1186,7 @@ else return 0; } //----------------------------------------------------------------------------- -int USER::SendMessage(STG_MSG & msg) const +int USER_IMPL::SendMessage(STG_MSG & msg) const { // No lock `cause we are already locked from caller int ret = -1; @@ -1209,7 +1209,7 @@ if (!ret) return ret; } //----------------------------------------------------------------------------- -void USER::ScanMessage() +void USER_IMPL::ScanMessage() { // No lock `cause we are already locked from caller // We need not check for the authorizedBy `cause it has already checked by caller diff --git a/projects/stargazer/user_impl.h b/projects/stargazer/user_impl.h index 1c84ba1c..dc21f696 100644 --- a/projects/stargazer/user_impl.h +++ b/projects/stargazer/user_impl.h @@ -24,8 +24,8 @@ $Author: faust $ */ -#ifndef USER_H -#define USER_H +#ifndef USER_IMPL_H +#define USER_IMPL_H #include #include @@ -33,6 +33,7 @@ #include #include +#include "user.h" #include "os_int.h" #include "stg_const.h" #include "user_stat.h" @@ -46,79 +47,71 @@ using namespace std; //----------------------------------------------------------------------------- -class USER; class TARIFF; class TARIFFS; class ADMIN; -typedef list::iterator user_iter; -typedef list::const_iterator const_user_iter; +class USER_IMPL; //----------------------------------------------------------------------------- -class USER_ID_GENERATOR -{ -friend class USER; +class USER_ID_GENERATOR { +friend class USER_IMPL; private: USER_ID_GENERATOR() {} int GetNextID() { static int id = 0; return id++; } }; //----------------------------------------------------------------------------- class CHG_PASSIVE_NOTIFIER : public PROPERTY_NOTIFIER_BASE, - private NONCOPYABLE -{ + private NONCOPYABLE { public: - CHG_PASSIVE_NOTIFIER(USER * u) : user(u) {} + CHG_PASSIVE_NOTIFIER(USER_IMPL * u) : user(u) {} void Notify(const int & oldPassive, const int & newPassive); private: - USER * user; + USER_IMPL * user; }; //----------------------------------------------------------------------------- class CHG_TARIFF_NOTIFIER : public PROPERTY_NOTIFIER_BASE, - private NONCOPYABLE -{ + private NONCOPYABLE { public: - CHG_TARIFF_NOTIFIER(USER * u) : user(u) {} + CHG_TARIFF_NOTIFIER(USER_IMPL * u) : user(u) {} void Notify(const string & oldTariff, const string & newTariff); private: - USER * user; + USER_IMPL * user; }; //----------------------------------------------------------------------------- class CHG_CASH_NOTIFIER : public PROPERTY_NOTIFIER_BASE, - private NONCOPYABLE -{ + private NONCOPYABLE { public: - CHG_CASH_NOTIFIER(USER * u) : user(u) {} + CHG_CASH_NOTIFIER(USER_IMPL * u) : user(u) {} void Notify(const double & oldCash, const double & newCash); private: - USER * user; + USER_IMPL * user; }; //----------------------------------------------------------------------------- class CHG_IP_NOTIFIER : public PROPERTY_NOTIFIER_BASE, - private NONCOPYABLE -{ + private NONCOPYABLE { public: - CHG_IP_NOTIFIER(USER * u) : user(u) {} + CHG_IP_NOTIFIER(USER_IMPL * u) : user(u) {} void Notify(const uint32_t & oldCash, const uint32_t & newCash); private: - USER * user; + USER_IMPL * user; }; //----------------------------------------------------------------------------- -class USER -{ +class USER_IMPL : public USER { friend class CHG_PASSIVE_NOTIFIER; friend class CHG_TARIFF_NOTIFIER; friend class CHG_CASH_NOTIFIER; friend class CHG_IP_NOTIFIER; public: - USER(const SETTINGS * settings, - const BASE_STORE * store, - const TARIFFS * tariffs, - const ADMIN & sysAdmin, - const map * ipIndex); - USER(const USER & u); - ~USER(); + USER_IMPL(const SETTINGS * settings, + const BASE_STORE * store, + const TARIFFS * tariffs, + const ADMIN & sysAdmin, + const USERS * u); + USER_IMPL(const USER_IMPL & u); + virtual ~USER_IMPL(); int ReadConf(); int ReadStat(); @@ -166,8 +159,6 @@ public: int Authorize(uint32_t ip, const string & iface, uint32_t enabledDirs, const BASE_AUTH * auth); void Unauthorize(const BASE_AUTH * auth); bool IsAuthorizedBy(const BASE_AUTH * auth) const; - void OnAdd(); - void OnDelete(); int AddMessage(STG_MSG * msg); @@ -179,7 +170,8 @@ public: const string & GetStrError() const { return errorStr; } - USER_PROPERTIES property; + USER_PROPERTIES & GetProperty() { return property; }; + const USER_PROPERTIES & GetProperty() const { return property; }; void SetDeleted() { deleted = true; } bool GetDeleted() const { return deleted; } @@ -195,7 +187,12 @@ public: bool IsInetable(); string GetEnabledDirs(); + void OnAdd(); + void OnDelete(); + private: + const USERS * users; + USER_PROPERTIES property; STG_LOGGER & WriteServLog; void Connect(bool fakeConnect = false); @@ -204,6 +201,7 @@ private: int SendMessage(STG_MSG & msg) const; void ScanMessage(); + time_t lastScanMessages; string login; @@ -226,7 +224,7 @@ private: time_t pingTime; - const ADMIN sysAdmin; + const ADMIN & sysAdmin; const BASE_STORE * store; const TARIFFS * tariffs; @@ -239,8 +237,6 @@ private: set authorizedBy; - const map * ipIndex; - list messages; bool deleted; diff --git a/projects/stargazer/users_impl.cpp b/projects/stargazer/users_impl.cpp index 84a10227..35237ba2 100644 --- a/projects/stargazer/users_impl.cpp +++ b/projects/stargazer/users_impl.cpp @@ -41,8 +41,7 @@ #include #include "settings.h" -#include "users.h" -#include "user.h" +#include "users_impl.h" #include "common.h" #include "stg_timer.h" @@ -53,7 +52,7 @@ extern const volatile time_t stgTime; //#define USERS_DEBUG 1 //----------------------------------------------------------------------------- -USERS::USERS(SETTINGS * s, BASE_STORE * st, TARIFFS * t, const ADMIN & sa) +USERS_IMPL::USERS_IMPL(SETTINGS * s, BASE_STORE * st, TARIFFS * t, const ADMIN & sa) : users(), usersToDelete(), userIPNotifiersBefore(), @@ -80,14 +79,14 @@ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&mutex, &attr); } //----------------------------------------------------------------------------- -USERS::~USERS() +USERS_IMPL::~USERS_IMPL() { pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- -int USERS::FindByNameNonLock(const string & login, user_iter * user) const +int USERS_IMPL::FindByNameNonLock(const string & login, user_iter * user) { -map::const_iterator iter; +map::iterator iter; iter = loginIndex.find(login); if (iter != loginIndex.end()) { @@ -98,27 +97,32 @@ if (iter != loginIndex.end()) return -1; } //----------------------------------------------------------------------------- -int USERS::FindByName(const string & login, user_iter * user) const +int USERS_IMPL::FindByName(const string & login, USER_PTR * user) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -return FindByNameNonLock(login, user); +user_iter u; +int res = FindByNameNonLock(login, &u); +if (res) + return -1; +*user = &(*u); +return 0; } //----------------------------------------------------------------------------- -bool USERS::TariffInUse(const string & tariffName) +bool USERS_IMPL::TariffInUse(const string & tariffName) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -list::iterator iter; +list::const_iterator iter; iter = users.begin(); while (iter != users.end()) { - if (iter->property.tariffName.Get() == tariffName) + if (iter->GetProperty().tariffName.Get() == tariffName) return true; ++iter; } return false; } //----------------------------------------------------------------------------- -int USERS::Add(const string & login, const ADMIN & admin) +int USERS_IMPL::Add(const string & login, const ADMIN & admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); const PRIV * priv = admin.GetPriv(); @@ -142,7 +146,7 @@ if (store->AddUser(login)) } ////// -USER u(settings, store, tariffs, sysAdmin, &ipIndex); +USER_IMPL u(settings, store, tariffs, sysAdmin, this); /*struct tm * tms; time_t t = stgTime; @@ -176,17 +180,17 @@ AddUserIntoIndexes(users.begin()); SetUserNotifiers(users.begin()); // õ×ÅÄÏÍÌÑÅÍ ×ÓÅÈ ÖÅÌÁÀÝÉÈ, ÞÔÏ ÄÏÂÁ×ÌÅÎ ÎÏ×ÙÊ ÐÏÌØÚÏ×ÁÔÅÌØ -set *>::iterator ni = onAddNotifiers.begin(); +set *>::iterator ni = onAddNotifiers.begin(); while (ni != onAddNotifiers.end()) { - (*ni)->Notify(users.begin()); + (*ni)->Notify(&users.front()); ++ni; } return 0; } //----------------------------------------------------------------------------- -void USERS::Del(const string & login, const ADMIN & admin) +void USERS_IMPL::Del(const string & login, const ADMIN & admin) { const PRIV * priv = admin.GetPriv(); user_iter u; @@ -211,10 +215,10 @@ if (!priv->userAddDel) } } -set *>::iterator ni = onDelNotifiers.begin(); +set *>::iterator ni = onDelNotifiers.begin(); while (ni != onDelNotifiers.end()) { - (*ni)->Notify(u); + (*ni)->Notify(&(*u)); ++ni; } @@ -238,7 +242,7 @@ while (ni != onDelNotifiers.end()) } } //----------------------------------------------------------------------------- -int USERS::ReadUsers() +int USERS_IMPL::ReadUsers() { vector usersList; usersList.clear(); @@ -252,7 +256,7 @@ user_iter ui; for (unsigned int i = 0; i < usersList.size(); i++) { - USER u(settings, store, tariffs, sysAdmin, &ipIndex); + USER_IMPL u(settings, store, tariffs, sysAdmin, this); u.SetLogin(usersList[i]); users.push_front(u); @@ -271,10 +275,10 @@ for (unsigned int i = 0; i < usersList.size(); i++) return 0; } //----------------------------------------------------------------------------- -void * USERS::Run(void * d) +void * USERS_IMPL::Run(void * d) { printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid()); -USERS * us = (USERS*) d; +USERS_IMPL * us = (USERS_IMPL*) d; struct tm t; time_t tt = stgTime; @@ -295,7 +299,7 @@ while (us->nonstop) //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); - for_each(us->users.begin(), us->users.end(), mem_fun_ref(&USER::Run)); + for_each(us->users.begin(), us->users.end(), mem_fun_ref(&USER_IMPL::Run)); tt = stgTime; localtime_r(&tt, &t); @@ -349,13 +353,13 @@ us->isRunning = false; return NULL; } //----------------------------------------------------------------------------- -void USERS::NewMinute(const struct tm & t) +void USERS_IMPL::NewMinute(const struct tm & t) { //Write traff, reset session traff. Fake disconnect-connect if (t.tm_hour == 23 && t.tm_min == 59) { printfd(__FILE__,"MidnightResetSessionStat\n"); - for_each(users.begin(), users.end(), mem_fun_ref(&USER::MidnightResetSessionStat)); + for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::MidnightResetSessionStat)); } if (TimeToWriteDetailStat(t)) @@ -364,21 +368,21 @@ if (TimeToWriteDetailStat(t)) int usersCnt = 0; // ðÉÛÅÍ ÀÚÅÒÏ× ÞÁÓÔÑÍÉ. ÷ ÐÅÒÅÒÙ×ÁÈ ×ÙÚÙ×ÁÅÍ USER::Run - list::iterator usr = users.begin(); + list::iterator usr = users.begin(); while (usr != users.end()) { usersCnt++; usr->WriteDetailStat(); usr++; if (usersCnt % 10 == 0) - for_each(users.begin(), users.end(), mem_fun_ref(&USER::Run)); + for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run)); } } RealDelUser(); } //----------------------------------------------------------------------------- -void USERS::NewDay(const struct tm & t) +void USERS_IMPL::NewDay(const struct tm & t) { struct tm t1; time_t tt = stgTime; @@ -402,14 +406,14 @@ if (!settings->GetDayFeeIsLastDay()) if (settings->GetSpreadFee()) { printfd(__FILE__, "Spread DayFee\n"); - for_each(users.begin(), users.end(), mem_fun_ref(&USER::ProcessDayFeeSpread)); + for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFeeSpread)); } else { if (t.tm_mday == dayFee) { printfd(__FILE__, "DayFee\n"); - for_each(users.begin(), users.end(), mem_fun_ref(&USER::ProcessDayFee)); + for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFee)); } } @@ -420,7 +424,7 @@ if (settings->GetDayFeeIsLastDay()) } } //----------------------------------------------------------------------------- -void USERS::DayResetTraff(const struct tm & t1) +void USERS_IMPL::DayResetTraff(const struct tm & t1) { int dayResetTraff = settings->GetDayResetTraff(); if (dayResetTraff == 0) @@ -428,12 +432,12 @@ if (dayResetTraff == 0) if (t1.tm_mday == dayResetTraff) { printfd(__FILE__, "ResetTraff\n"); - for_each(users.begin(), users.end(), mem_fun_ref(&USER::ProcessNewMonth)); - for_each(users.begin(), users.end(), mem_fun_ref(&USER::SetPrepaidTraff)); + for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessNewMonth)); + for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff)); } } //----------------------------------------------------------------------------- -int USERS::Start() +int USERS_IMPL::Start() { if (ReadUsers()) { @@ -450,7 +454,7 @@ if (pthread_create(&thread, NULL, Run, this)) return 0; } //----------------------------------------------------------------------------- -int USERS::Stop() +int USERS_IMPL::Stop() { printfd(__FILE__, "USERS::Stop()\n"); @@ -487,22 +491,22 @@ if (isRunning) } printfd(__FILE__, "Before USERS::Run()\n"); -for_each(users.begin(), users.end(), mem_fun_ref(&USER::Run)); +for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run)); // 'cause bind2st accepts only constant first param -for (list::iterator it = users.begin(); +for (list::iterator it = users.begin(); it != users.end(); ++it) it->WriteDetailStat(true); -for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteStat)); -for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteConf)); +for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteStat)); +for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf)); printfd(__FILE__, "USERS::Stop()\n"); return 0; } //----------------------------------------------------------------------------- -void USERS::RealDelUser() +void USERS_IMPL::RealDelUser() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -531,13 +535,12 @@ while (iter != usersToDelete.end()) return; } //----------------------------------------------------------------------------- -int USERS::GetUserNum() +int USERS_IMPL::GetUserNum() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); return users.size(); } //----------------------------------------------------------------------------- -void USERS::AddToIPIdx(user_iter user) +void USERS_IMPL::AddToIPIdx(user_iter user) { printfd(__FILE__, "USERS: Add IP Idx\n"); uint32_t ip = user->GetCurrIP(); @@ -556,7 +559,7 @@ assert((it == ipIndex.end() || it->first != ip) && "User is not in index"); ipIndex.insert(it, std::make_pair(ip, user)); } //----------------------------------------------------------------------------- -void USERS::DelFromIPIdx(uint32_t ip) +void USERS_IMPL::DelFromIPIdx(uint32_t ip) { printfd(__FILE__, "USERS: Del IP Idx\n"); assert(ip && "User has non-null ip"); @@ -574,11 +577,11 @@ if (it == ipIndex.end()) ipIndex.erase(it); } //----------------------------------------------------------------------------- -int USERS::FindByIPIdx(uint32_t ip, user_iter * usr) +int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_PTR * usr) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -map::iterator it; +map::const_iterator it; it = ipIndex.find(ip); if (it == ipIndex.end()) @@ -586,37 +589,47 @@ if (it == ipIndex.end()) //printfd(__FILE__, "User NOT found in IP_Index!!!\n"); return -1; } -*usr = it->second; +*usr = &(*it->second); //printfd(__FILE__, "User found in IP_Index\n"); return 0; } //----------------------------------------------------------------------------- -void USERS::AddNotifierUserAdd(NOTIFIER_BASE * n) +bool USERS_IMPL::IsIPInIndex(uint32_t ip) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); + +map::const_iterator it; +it = ipIndex.find(ip); + +return it != ipIndex.end(); +} +//----------------------------------------------------------------------------- +void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); onAddNotifiers.insert(n); } //----------------------------------------------------------------------------- -void USERS::DelNotifierUserAdd(NOTIFIER_BASE * n) +void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); //printfd(__FILE__, "DelNotifierUserAdd\n"); onAddNotifiers.erase(n); } //----------------------------------------------------------------------------- -void USERS::AddNotifierUserDel(NOTIFIER_BASE * n) +void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); onDelNotifiers.insert(n); } //----------------------------------------------------------------------------- -void USERS::DelNotifierUserDel(NOTIFIER_BASE * n) +void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE * n) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); onDelNotifiers.erase(n); } //----------------------------------------------------------------------------- -int USERS::OpenSearch() +int USERS_IMPL::OpenSearch() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); handle++; @@ -624,7 +637,7 @@ searchDescriptors[handle] = users.begin(); return handle; } //----------------------------------------------------------------------------- -int USERS::SearchNext(int h, user_iter * u) +int USERS_IMPL::SearchNext(int h, USER_PTR * user) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -646,14 +659,14 @@ while (searchDescriptors[h]->GetDeleted()) } } -*u = searchDescriptors[h]; +*user = &(*searchDescriptors[h]); ++searchDescriptors[h]; return 0; } //----------------------------------------------------------------------------- -int USERS::CloseSearch(int h) +int USERS_IMPL::CloseSearch(int h) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (searchDescriptors.find(h) != searchDescriptors.end()) @@ -666,7 +679,7 @@ WriteServLog("USERS. Incorrect search handle."); return -1; } //----------------------------------------------------------------------------- -void USERS::SetUserNotifiers(user_iter user) +void USERS_IMPL::SetUserNotifiers(user_iter user) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -680,7 +693,7 @@ user->AddCurrIPBeforeNotifier(&(*userIPNotifiersBefore.begin())); user->AddCurrIPAfterNotifier(&(*userIPNotifiersAfter.begin())); } //----------------------------------------------------------------------------- -void USERS::UnSetUserNotifiers(user_iter user) +void USERS_IMPL::UnSetUserNotifiers(user_iter user) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -714,19 +727,19 @@ while (ai != userIPNotifiersAfter.end()) } } //----------------------------------------------------------------------------- -void USERS::AddUserIntoIndexes(user_iter user) +void USERS_IMPL::AddUserIntoIndexes(user_iter user) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -loginIndex.insert(pair(user->GetLogin(), user)); +loginIndex.insert(make_pair(user->GetLogin(), user)); } //----------------------------------------------------------------------------- -void USERS::DelUserFromIndexes(user_iter user) +void USERS_IMPL::DelUserFromIndexes(user_iter user) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); loginIndex.erase(user->GetLogin()); } //----------------------------------------------------------------------------- -bool USERS::TimeToWriteDetailStat(const struct tm & t) +bool USERS_IMPL::TimeToWriteDetailStat(const struct tm & t) { int statTime = settings->GetDetailStatWritePeriod(); diff --git a/projects/stargazer/users_impl.h b/projects/stargazer/users_impl.h index 424958da..2e0b5296 100644 --- a/projects/stargazer/users_impl.h +++ b/projects/stargazer/users_impl.h @@ -25,8 +25,8 @@ $Author: faust $ */ -#ifndef USERS_H -#define USERS_H +#ifndef USERS_IMPL_H +#define USERS_IMPL_H #include @@ -38,8 +38,11 @@ $Author: faust $ #include "os_int.h" +#include "base_store.h" #include "settings.h" +#include "users.h" #include "user.h" +#include "user_impl.h" #include "tariffs.h" #include "stg_logger.h" #include "notifer.h" @@ -49,83 +52,80 @@ $Author: faust $ const int userDeleteDelayTime = 120; -using namespace std; -class USERS; +typedef std::list::iterator user_iter; +typedef std::list::const_iterator const_user_iter; + +class USERS_IMPL; //----------------------------------------------------------------------------- -class PROPERTY_NOTIFER_IP_BEFORE: public PROPERTY_NOTIFIER_BASE -{ +class PROPERTY_NOTIFER_IP_BEFORE: public PROPERTY_NOTIFIER_BASE { public: - PROPERTY_NOTIFER_IP_BEFORE(USERS & us, user_iter u) : users(us), user(u) {}; + PROPERTY_NOTIFER_IP_BEFORE(USERS_IMPL & us, user_iter u) : users(us), user(u) {} void Notify(const uint32_t & oldValue, const uint32_t & newValue); - user_iter GetUser() const { return user; }; + user_iter GetUser() const { return user; } private: - USERS & users; - user_iter user; + USERS_IMPL & users; + user_iter user; }; //----------------------------------------------------------------------------- -class PROPERTY_NOTIFER_IP_AFTER: public PROPERTY_NOTIFIER_BASE -{ +class PROPERTY_NOTIFER_IP_AFTER: public PROPERTY_NOTIFIER_BASE { public: - PROPERTY_NOTIFER_IP_AFTER(USERS & us, user_iter u) : users(us), user(u) {}; + PROPERTY_NOTIFER_IP_AFTER(USERS_IMPL & us, user_iter u) : users(us), user(u) {} void Notify(const uint32_t & oldValue, const uint32_t & newValue); - user_iter GetUser() const { return user; }; + user_iter GetUser() const { return user; } private: - USERS & users; - user_iter user; + USERS_IMPL & users; + user_iter user; }; //----------------------------------------------------------------------------- -struct USER_TO_DEL -{ +struct USER_TO_DEL { USER_TO_DEL() : iter(), delTime(0) -{}; +{} -list::iterator iter; +std::list::iterator iter; time_t delTime; }; //----------------------------------------------------------------------------- -class USERS : private NONCOPYABLE - { +class USERS_IMPL : private NONCOPYABLE, public USERS { friend class PROPERTY_NOTIFER_IP_BEFORE; friend class PROPERTY_NOTIFER_IP_AFTER; public: - USERS(SETTINGS * s, BASE_STORE * store, TARIFFS * tariffs, const ADMIN & sysAdmin); - ~USERS(); + USERS_IMPL(SETTINGS * s, BASE_STORE * store, TARIFFS * tariffs, const ADMIN & sysAdmin); + virtual ~USERS_IMPL(); - int FindByName(const string & login, user_iter * user) const; - int FindByID(int id, user_iter * user); + int FindByName(const std::string & login, USER_PTR * user); - bool TariffInUse(const string & tariffName); + bool TariffInUse(const std::string & tariffName) const; - void AddNotifierUserAdd(NOTIFIER_BASE *); - void DelNotifierUserAdd(NOTIFIER_BASE *); + void AddNotifierUserAdd(NOTIFIER_BASE *); + void DelNotifierUserAdd(NOTIFIER_BASE *); - void AddNotifierUserDel(NOTIFIER_BASE *); - void DelNotifierUserDel(NOTIFIER_BASE *); + void AddNotifierUserDel(NOTIFIER_BASE *); + void DelNotifierUserDel(NOTIFIER_BASE *); - int Add(const string & login, const ADMIN & admin); - void Del(const string & login, const ADMIN & admin); + int Add(const std::string & login, const ADMIN & admin); + void Del(const std::string & login, const ADMIN & admin); int ReadUsers(); - int GetUserNum(); + int GetUserNum() const; - int FindByIPIdx(uint32_t ip, user_iter * user); + int FindByIPIdx(uint32_t ip, USER_PTR * user) const; + bool IsIPInIndex(uint32_t ip) const; int OpenSearch(); - int SearchNext(int handler, user_iter * u); + int SearchNext(int handler, USER_PTR * user); int CloseSearch(int handler); int Start(); int Stop(); private: - void AddToIPIdx(user_iter); + void AddToIPIdx(user_iter user); void DelFromIPIdx(uint32_t ip); - int FindByNameNonLock(const string & login, user_iter * user) const; - int FindByIDNonLock(int id, user_iter * user); + int FindByNameNonLock(const std::string & login, user_iter * user); void RealDelUser(); void ProcessActions(); @@ -143,18 +143,18 @@ private: bool TimeToWriteDetailStat(const struct tm & t); - list users; - list usersToDelete; - list userIPNotifiersBefore; - list userIPNotifiersAfter; + std::list users; + std::list usersToDelete; + std::list userIPNotifiersBefore; + std::list userIPNotifiersAfter; - map ipIndex; - map loginIndex; + std::map ipIndex; + std::map loginIndex; SETTINGS * settings; TARIFFS * tariffs; BASE_STORE * store; - const ADMIN sysAdmin; + const ADMIN & sysAdmin; STG_LOGGER & WriteServLog; bool nonstop; @@ -164,11 +164,11 @@ private: pthread_t thread; mutable unsigned int handle; - mutable map searchDescriptors; + mutable std::map searchDescriptors; - set *> onAddNotifiers; - set *> onDelNotifiers; - }; + std::set*> onAddNotifiers; + std::set*> onDelNotifiers; +}; //----------------------------------------------------------------------------- inline void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue, @@ -195,4 +195,3 @@ users.AddToIPIdx(user); } //----------------------------------------------------------------------------- #endif - -- 2.44.2