X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/becf6dfe4fe2ecd43792aa53a302c5866483f306..b27841d687ec9e84983340b5581376dfb24010ea:/projects/stargazer/tariffs_impl.cpp?ds=inline diff --git a/projects/stargazer/tariffs_impl.cpp b/projects/stargazer/tariffs_impl.cpp index 1f8a4230..792e53da 100644 --- a/projects/stargazer/tariffs_impl.cpp +++ b/projects/stargazer/tariffs_impl.cpp @@ -28,77 +28,68 @@ $Author: faust $ */ +#include "tariffs_impl.h" + +#include "stg/locker.h" +#include "stg/logger.h" +#include "stg/store.h" +#include "stg/admin.h" +#include "stg/admin_conf.h" + #include #include -#include - -#include "tariffs.h" -#include "stg_locker.h" -#include "stg_logger.h" -#include "base_store.h" -#include "admin.h" -using namespace std; +using STG::TariffsImpl; //----------------------------------------------------------------------------- -TARIFFS::TARIFFS(BASE_STORE * st) - : tariffs(), - store(st), - WriteServLog(GetStgLogger()), - strError(), +TariffsImpl::TariffsImpl(Store * st) + : store(st), + WriteServLog(Logger::get()), noTariff(NO_TARIFF_NAME) { -pthread_mutex_init(&mutex, NULL); -ReadTariffs(); } //----------------------------------------------------------------------------- -TARIFFS::~TARIFFS() +int TariffsImpl::ReadTariffs() { -pthread_mutex_destroy(&mutex); -} -//----------------------------------------------------------------------------- -int TARIFFS::ReadTariffs() -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); -vector tariffsList; +std::vector tariffsList; if (store->GetTariffsList(&tariffsList)) { WriteServLog("Cannot get tariffs list."); WriteServLog("%s", store->GetStrError().c_str()); } -int tariffsNum = tariffsList.size(); +Data::size_type tariffsNum = tariffsList.size(); -for (int i = 0; i < tariffsNum; i++) +for (Data::size_type i = 0; i < tariffsNum; i++) { - TARIFF_DATA td; + TariffData td; if (store->RestoreTariff(&td, tariffsList[i])) { WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str()); WriteServLog("%s", store->GetStrError().c_str()); return -1; } - tariffs.push_back(TARIFF(td)); + tariffs.push_back(TariffImpl(td)); } return 0; } //----------------------------------------------------------------------------- -int TARIFFS::GetTariffsNum() const +size_t TariffsImpl::Count() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); return tariffs.size(); } //----------------------------------------------------------------------------- -const TARIFF * TARIFFS::FindByName(const string & name) const +const STG::Tariff* TariffsImpl::FindByName(const std::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)); +std::lock_guard lock(m_mutex); +const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name)); if (ti != tariffs.end()) return &(*ti); @@ -106,28 +97,27 @@ if (ti != tariffs.end()) return NULL; } //----------------------------------------------------------------------------- -int TARIFFS::Chg(const TARIFF_DATA & td, const ADMIN & admin) +int TariffsImpl::Chg(const TariffData & td, const Admin * admin) { -const PRIV * priv = admin.GetPriv(); +const auto& priv = admin->priv(); -if (!priv->tariffChg) +if (!priv.tariffChg) { - string s = admin.GetLogStr() + " Change tariff \'" + std::string s = admin->logStr() + " Change tariff \'" + td.tariffConf.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF(td.tariffConf.name)); +auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name)); if (ti == tariffs.end()) { strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist."; - WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -135,83 +125,91 @@ if (ti == tariffs.end()) if (store->SaveTariff(td, td.tariffConf.name)) { - string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError(); + std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError(); WriteServLog(error.c_str()); return -1; } WriteServLog("%s Tariff \'%s\' changed.", - admin.GetLogStr().c_str(), td.tariffConf.name.c_str()); + admin->logStr().c_str(), td.tariffConf.name.c_str()); return 0; } //----------------------------------------------------------------------------- -int TARIFFS::Del(const string & name, const ADMIN & admin) +int TariffsImpl::Del(const std::string & name, const Admin * admin) { -const PRIV * priv = admin.GetPriv(); +const auto& priv = admin->priv(); -if (!priv->tariffChg) +if (!priv.tariffChg) { - string s = admin.GetLogStr() + " Delete tariff \'" + std::string s = admin->logStr() + " Delete tariff \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -STG_LOCKER lock(&mutex, __FILE__, __LINE__); - -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF(name)); +TariffData td; -if (ti == tariffs.end()) { - strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist."; - WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str()); - return -1; - } + std::lock_guard lock(m_mutex); -if (store->DelTariff(name)) - { - WriteServLog("Cannot delete tariff %s.", name.c_str()); - WriteServLog("%s", store->GetStrError().c_str()); - return -1; + const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name)); + + if (ti == tariffs.end()) + { + strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist."; + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); + return -1; + } + + if (store->DelTariff(name)) + { + WriteServLog("Cannot delete tariff %s.", name.c_str()); + WriteServLog("%s", store->GetStrError().c_str()); + return -1; + } + + td = ti->GetTariffData(); + + tariffs.erase(ti); } -tariffs.erase(ti); +m_onDelCallbacks.notify(td); WriteServLog("%s Tariff \'%s\' deleted.", - admin.GetLogStr().c_str(), + admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- -int TARIFFS::Add(const string & name, const ADMIN & admin) +int TariffsImpl::Add(const std::string & name, const Admin * admin) { -const PRIV * priv = admin.GetPriv(); +const auto& priv = admin->priv(); -if (!priv->tariffChg) +if (!priv.tariffChg) { - string s = admin.GetLogStr() + " Add tariff \'" + std::string s = admin->logStr() + " Add tariff \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -STG_LOCKER lock(&mutex, __FILE__, __LINE__); + { + std::lock_guard lock(m_mutex); -list::iterator ti; -ti = find(tariffs.begin(), tariffs.end(), TARIFF(name)); + const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name)); -if (ti != tariffs.end()) - { - strError = "Tariff \'" + name + "\' cannot be added. Tariff alredy exist."; - WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str()); - return -1; - } + if (ti != tariffs.end()) + { + strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist."; + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); + return -1; + } -tariffs.push_back(TARIFF(name)); + tariffs.push_back(TariffImpl(name)); + } if (store->AddTariff(name) < 0) { @@ -220,23 +218,23 @@ if (store->AddTariff(name) < 0) return -1; } +m_onAddCallbacks.notify(tariffs.back().GetTariffData()); + WriteServLog("%s Tariff \'%s\' added.", - admin.GetLogStr().c_str(), name.c_str()); + admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- -void TARIFFS::GetTariffsData(std::list * tdl) +void TariffsImpl::GetTariffsData(std::vector * tdl) const { assert(tdl != NULL && "Tariffs data list is not null"); -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); -std::list::const_iterator it = tariffs.begin(); -TARIFF_DATA td; +auto it = tariffs.begin(); for (; it != tariffs.end(); ++it) { - it->GetTariffData(&td); - tdl->push_back(td); + tdl->push_back(it->GetTariffData()); } } //-----------------------------------------------------------------------------