X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/5ff8211bdcd5f110621c0ff01fa0854b5e6d9762..5dfab5bea026e33031d041b233c73e196469b4ac:/projects/stargazer/services_impl.cpp diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp index 85b0f4e5..36640c88 100644 --- a/projects/stargazer/services_impl.cpp +++ b/projects/stargazer/services_impl.cpp @@ -18,48 +18,47 @@ * Author : Maxim Mamontov */ -#include -#include -#include +#include "services_impl.h" #include "stg/admin.h" +#include "stg/admin_conf.h" +#include "stg/store.h" #include "stg/common.h" -#include "services_impl.h" + +#include +#include + +using STG::ServicesImpl; //----------------------------------------------------------------------------- -SERVICES_IMPL::SERVICES_IMPL(STORE * st) - : SERVICES(), - data(), - store(st), - WriteServLog(GetStgLogger()), +ServicesImpl::ServicesImpl(Store * st) + : store(st), + WriteServLog(Logger::get()), searchDescriptors(), - handle(0), - mutex(), - strError() + handle(0) { -pthread_mutex_init(&mutex, NULL); Read(); } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin) +int ServicesImpl::Add(const ServiceConf & service, 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->serviceChg) +if (!priv.serviceChg) { - string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied."; + std::string s = admin->logStr() + " Add service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -srv_iter si(find(data.begin(), data.end(), service)); +iterator si(std::find(data.begin(), data.end(), service)); if (si != data.end()) { strError = "Service \'" + service.name + "\' cannot not be added. Service already exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -69,83 +68,85 @@ data.push_back(service); if (store->AddService(service.name) == 0) { WriteServLog("%s Service \'%s\' added.", - admin->GetLogStr().c_str(), service.name.c_str()); + admin->logStr().c_str(), service.name.c_str()); return 0; } strError = "Service \'" + service.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 SERVICES_IMPL::Del(const string & name, const ADMIN * admin) +int ServicesImpl::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->serviceChg) +if (!priv.serviceChg) { - string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied."; + std::string s = admin->logStr() + " Delete service \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); +iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); if (si == data.end()) { strError = "Service \'" + name + "\' cannot be deleted. Service 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->DelService(name) < 0) { strError = "Service \'" + 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 Service \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); +WriteServLog("%s Service \'%s\' deleted.", admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Change(const SERVICE_CONF & service, const ADMIN * admin) +int ServicesImpl::Change(const ServiceConf & service, 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->serviceChg) +if (!priv.serviceChg) { - string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied."; + std::string s = admin->logStr() + " Change service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -srv_iter si(find(data.begin(), data.end(), service)); +iterator si(std::find(data.begin(), data.end(), service)); if (si == data.end()) { strError = "Service \'" + service.name + "\' cannot be changed " + ". Service 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; } +printfd(__FILE__, "Old cost = %f, old pay day = %u\n", si->cost, static_cast(si->payDay)); *si = service; +printfd(__FILE__, "New cost = %f, New pay day = %u\n", si->cost, static_cast(si->payDay)); if (store->SaveService(service)) { WriteServLog("Cannot write service %s.", service.name.c_str()); @@ -154,15 +155,15 @@ if (store->SaveService(service)) } WriteServLog("%s Service \'%s\' changed.", - admin->GetLogStr().c_str(), service.name.c_str()); + admin->logStr().c_str(), service.name.c_str()); return 0; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::Read() +bool ServicesImpl::Read() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -vector servicesList; +std::lock_guard lock(mutex); +std::vector servicesList; if (store->GetServicesList(&servicesList) < 0) { WriteServLog(store->GetStrError().c_str()); @@ -171,7 +172,7 @@ if (store->GetServicesList(&servicesList) < 0) for (size_t i = 0; i < servicesList.size(); i++) { - SERVICE_CONF service; + ServiceConf service; if (store->RestoreService(&service, servicesList[i])) { @@ -184,15 +185,34 @@ for (size_t i = 0; i < servicesList.size(); i++) return false; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::Find(const string & name, SERVICE_CONF * service) +bool ServicesImpl::Find(const std::string & name, ServiceConf * service) const { assert(service != NULL && "Pointer to service is not null"); -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); if (data.empty()) + return true; + +const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); + +if (si != data.end()) + { + *service = *si; return false; + } + +return true; +} +//----------------------------------------------------------------------------- +bool ServicesImpl::Find(const std::string & name, ServiceConfOpt * service) const +{ +assert(service != NULL && "Pointer to service is not null"); + +std::lock_guard lock(mutex); +if (data.empty()) + return true; -srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); +const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); if (si != data.end()) { @@ -203,16 +223,16 @@ if (si != data.end()) return true; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::Exists(const string & name) const +bool ServicesImpl::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 services in the system!\n"); return true; } -const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); +const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); if (si != data.end()) return true; @@ -220,17 +240,17 @@ if (si != data.end()) return false; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::OpenSearch() const +int ServicesImpl::OpenSearch() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); handle++; searchDescriptors[handle] = data.begin(); return handle; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::SearchNext(int h, SERVICE_CONF * service) const +int ServicesImpl::SearchNext(int h, ServiceConf * service) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); if (searchDescriptors.find(h) == searchDescriptors.end()) { WriteServLog("SERVICES. Incorrect search handle."); @@ -245,9 +265,9 @@ if (searchDescriptors[h] == data.end()) return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::CloseSearch(int h) const +int ServicesImpl::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));