X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/d25555ce9c6e09344356868780e678c75bbbd164..0907aa4037b12b6b88ee24495d4577a064d4f8db:/projects/stargazer/corps_impl.cpp diff --git a/projects/stargazer/corps_impl.cpp b/projects/stargazer/corps_impl.cpp index 456523dc..b41b4559 100644 --- a/projects/stargazer/corps_impl.cpp +++ b/projects/stargazer/corps_impl.cpp @@ -18,35 +18,35 @@ * Author : Maxim Mamontov */ -#include -#include -#include +#include "corps_impl.h" #include "stg/admin.h" +#include "stg/admin_conf.h" +#include "stg/store.h" #include "stg/common.h" -#include "corps_impl.h" + +#include +#include + +using STG::CorporationsImpl; //----------------------------------------------------------------------------- -CORPORATIONS_IMPL::CORPORATIONS_IMPL(STORE * st) - : CORPORATIONS(), - data(), - store(st), - WriteServLog(GetStgLogger()), - searchDescriptors(), +CorporationsImpl::CorporationsImpl(Store * st) + : store(st), + WriteServLog(Logger::get()), handle(0) { -pthread_mutex_init(&mutex, NULL); Read(); } //----------------------------------------------------------------------------- -int CORPORATIONS_IMPL::Add(const CORP_CONF & corp, const ADMIN * admin) +int CorporationsImpl::Add(const CorpConf & corp, const Admin * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const PRIV * priv = admin->GetPriv(); +std::lock_guard lock(mutex); +const auto& priv = admin->priv(); -if (!priv->corpChg) +if (!priv.corpChg) { - string s = admin->GetLogStr() + " Add corporation \'" + corp.name + "\'. Access denied."; + std::string s = admin->logStr() + " Add corporation \'" + corp.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -57,7 +57,7 @@ crp_iter si(find(data.begin(), data.end(), corp)); if (si != data.end()) { strError = "Corporation \'" + corp.name + "\' cannot not be added. Corporation already exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -67,68 +67,68 @@ data.push_back(corp); if (store->AddCorp(corp.name) == 0) { WriteServLog("%s Corporation \'%s\' added.", - admin->GetLogStr().c_str(), corp.name.c_str()); + admin->logStr().c_str(), corp.name.c_str()); return 0; } strError = "Corporation \'" + corp.name + "\' was not added. Error: " + store->GetStrError(); -WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); +WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } //----------------------------------------------------------------------------- -int CORPORATIONS_IMPL::Del(const string & name, const ADMIN * admin) +int CorporationsImpl::Del(const std::string & name, const Admin * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const PRIV * priv = admin->GetPriv(); +std::lock_guard lock(mutex); +const auto& priv = admin->priv(); -if (!priv->corpChg) +if (!priv.corpChg) { - string s = admin->GetLogStr() + " Delete corporation \'" + name + "\'. Access denied."; + std::string s = admin->logStr() + " Delete corporation \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); +crp_iter si(find(data.begin(), data.end(), CorpConf(name))); if (si == data.end()) { strError = "Corporation \'" + name + "\' cannot be deleted. Corporation 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; } -map::iterator csi; +std::map::iterator csi; csi = searchDescriptors.begin(); while (csi != searchDescriptors.end()) { if (csi->second == si) (csi->second)++; - csi++; + ++csi; } -data.remove(*si); +data.erase(si); if (store->DelCorp(name) < 0) { strError = "Corporation \'" + name + "\' was not deleted. Error: " + store->GetStrError(); - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -WriteServLog("%s Corporation \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); +WriteServLog("%s Corporation \'%s\' deleted.", admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- -int CORPORATIONS_IMPL::Change(const CORP_CONF & corp, const ADMIN * admin) +int CorporationsImpl::Change(const CorpConf & corp, const Admin * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const PRIV * priv = admin->GetPriv(); +std::lock_guard lock(mutex); +const auto& priv = admin->priv(); -if (!priv->corpChg) +if (!priv.corpChg) { - string s = admin->GetLogStr() + " Change corporation \'" + corp.name + "\'. Access denied."; + std::string s = admin->logStr() + " Change corporation \'" + corp.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -139,7 +139,7 @@ crp_iter si(find(data.begin(), data.end(), corp)); if (si == data.end()) { strError = "Corporation \'" + corp.name + "\' cannot be changed " + ". Corporation 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; } @@ -152,15 +152,15 @@ if (store->SaveCorp(corp)) } WriteServLog("%s Corporation \'%s\' changed.", - admin->GetLogStr().c_str(), corp.name.c_str()); + admin->logStr().c_str(), corp.name.c_str()); return 0; } //----------------------------------------------------------------------------- -bool CORPORATIONS_IMPL::Read() +bool CorporationsImpl::Read() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -vector corpsList; +std::lock_guard lock(mutex); +std::vector corpsList; if (store->GetCorpsList(&corpsList) < 0) { WriteServLog(store->GetStrError().c_str()); @@ -169,7 +169,7 @@ if (store->GetCorpsList(&corpsList) < 0) for (size_t i = 0; i < corpsList.size(); i++) { - CORP_CONF corp; + CorpConf corp; if (store->RestoreCorp(&corp, corpsList[i])) { @@ -182,15 +182,15 @@ for (size_t i = 0; i < corpsList.size(); i++) return false; } //----------------------------------------------------------------------------- -bool CORPORATIONS_IMPL::Find(const string & name, CORP_CONF * corp) +bool CorporationsImpl::Find(const std::string & name, CorpConf * corp) { assert(corp != NULL && "Pointer to corporation is not null"); -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); if (data.empty()) return false; -crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); +crp_iter si(find(data.begin(), data.end(), CorpConf(name))); if (si != data.end()) { @@ -201,16 +201,16 @@ if (si != data.end()) return true; } //----------------------------------------------------------------------------- -bool CORPORATIONS_IMPL::Exists(const string & name) const +bool CorporationsImpl::Exists(const std::string & name) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); if (data.empty()) { - printfd(__FILE__, "no admin in system!\n"); + printfd(__FILE__, "no corporations in system!\n"); return true; } -const_crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); +const_crp_iter si(find(data.begin(), data.end(), CorpConf(name))); if (si != data.end()) return true; @@ -218,17 +218,17 @@ if (si != data.end()) return false; } //----------------------------------------------------------------------------- -int CORPORATIONS_IMPL::OpenSearch() const +int CorporationsImpl::OpenSearch() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); handle++; searchDescriptors[handle] = data.begin(); return handle; } //----------------------------------------------------------------------------- -int CORPORATIONS_IMPL::SearchNext(int h, CORP_CONF * corp) const +int CorporationsImpl::SearchNext(int h, CorpConf * corp) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); if (searchDescriptors.find(h) == searchDescriptors.end()) { WriteServLog("CORPORATIONS. Incorrect search handle."); @@ -243,9 +243,9 @@ if (searchDescriptors[h] == data.end()) return 0; } //----------------------------------------------------------------------------- -int CORPORATIONS_IMPL::CloseSearch(int h) const +int CorporationsImpl::CloseSearch(int h) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); if (searchDescriptors.find(h) != searchDescriptors.end()) { searchDescriptors.erase(searchDescriptors.find(h));