X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/fcafc446aa1f15975b1802e47952ed371446e4d4..a5cb4cf37e5dfa9bb9ce5c5e4ccf8d5978d3576f:/projects/stargazer/services_impl.cpp diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp index 400b34b3..36640c88 100644 --- a/projects/stargazer/services_impl.cpp +++ b/projects/stargazer/services_impl.cpp @@ -18,264 +18,239 @@ * 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) { -pthread_mutex_init(&mutex, NULL); -ReadServices(); +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->adminChg) +if (!priv.serviceChg) { - string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; + std::string s = admin->logStr() + " Add service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -ADMIN_IMPL adm(0, login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +iterator si(std::find(data.begin(), data.end(), service)); -if (ai != data.end()) +if (si != data.end()) { - strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + strError = "Service \'" + service.name + "\' cannot not be added. Service already exist."; + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -data.push_back(adm); +data.push_back(service); -if (store->AddAdmin(login) == 0) +if (store->AddService(service.name) == 0) { - WriteServLog("%s Administrator \'%s\' added.", - admin->GetLogStr().c_str(), login.c_str()); + WriteServLog("%s Service \'%s\' added.", + admin->logStr().c_str(), service.name.c_str()); return 0; } -strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError(); -WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); +strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError(); +WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Del(const string & login, const ADMIN * admin) +int ServicesImpl::Del(const std::string & name, const Admin * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN_IMPL adm(0, login, ""); -const PRIV * priv = admin->GetPriv(); +std::lock_guard lock(mutex); +const auto& priv = admin->priv(); -if (!priv->adminChg) +if (!priv.serviceChg) { - string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; + std::string s = admin->logStr() + " Delete service \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -admin_iter ai(find(data.begin(), data.end(), adm)); +iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); -if (ai == data.end()) +if (si == data.end()) { - strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + strError = "Service \'" + name + "\' cannot be deleted. Service does not exist."; + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -map::iterator si; -si = searchDescriptors.begin(); -while (si != searchDescriptors.end()) +std::map::iterator csi; +csi = searchDescriptors.begin(); +while (csi != searchDescriptors.end()) { - if (si->second == ai) - (si->second)++; - si++; + if (csi->second == si) + (csi->second)++; + ++csi; } -data.remove(*ai); -if (store->DelAdmin(login) < 0) +data.erase(si); +if (store->DelService(name) < 0) { - strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError(); - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError(); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str()); +WriteServLog("%s Service \'%s\' deleted.", admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Change(const ADMIN_CONF & ac, 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->adminChg) +if (!priv.serviceChg) { - string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; + std::string s = admin->logStr() + " Change service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -ADMIN_IMPL adm(0, ac.login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +iterator si(std::find(data.begin(), data.end(), service)); -if (ai == data.end()) +if (si == data.end()) { - strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist."; + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -*ai = ac; -if (store->SaveAdmin(ac)) +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 admin %s.", ac.login.c_str()); + WriteServLog("Cannot write service %s.", service.name.c_str()); WriteServLog("%s", store->GetStrError().c_str()); return -1; } -WriteServLog("%s Administrator \'%s\' changed.", - admin->GetLogStr().c_str(), ac.login.c_str()); +WriteServLog("%s Service \'%s\' changed.", + admin->logStr().c_str(), service.name.c_str()); return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::ReadAdmins() +bool ServicesImpl::Read() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -vector adminsList; -if (store->GetAdminsList(&adminsList) < 0) +std::lock_guard lock(mutex); +std::vector servicesList; +if (store->GetServicesList(&servicesList) < 0) { WriteServLog(store->GetStrError().c_str()); - return -1; + return true; } -for (unsigned int i = 0; i < adminsList.size(); i++) +for (size_t i = 0; i < servicesList.size(); i++) { - ADMIN_CONF ac(0, adminsList[i], ""); + ServiceConf service; - if (store->RestoreAdmin(&ac, adminsList[i])) + if (store->RestoreService(&service, servicesList[i])) { WriteServLog(store->GetStrError().c_str()); - return -1; + return true; } - data.push_back(ADMIN_IMPL(ac)); - } -return 0; -} -//----------------------------------------------------------------------------- -void SERVICES_IMPL::PrintAdmins() const -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const_admin_iter ai(data.begin()); -while (ai != data.end()) - { - ai->Print(); - ai++; + data.push_back(service); } +return false; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::FindAdmin(const string & l, ADMIN ** admin) +bool ServicesImpl::Find(const std::string & name, ServiceConf * service) const { -assert(admin != NULL && "Pointer to admin is not null"); +assert(service != NULL && "Pointer to service is not null"); -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(mutex); if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - *admin = &noAdmin; - return false; - } + return true; -ADMIN_IMPL adm(0, l, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); -if (ai != data.end()) +if (si != data.end()) { - *admin = &(*ai); + *service = *si; return false; } return true; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::AdminExists(const string & login) const +bool ServicesImpl::Find(const std::string & name, ServiceConfOpt * service) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +assert(service != NULL && "Pointer to service is not null"); + +std::lock_guard lock(mutex); if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); return true; - } -ADMIN_IMPL adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); -if (ai != data.end()) - return true; +if (si != data.end()) + { + *service = *si; + return false; + } -return false; +return true; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN ** admin) +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; } -ADMIN_IMPL adm(0, login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); -if (ai == data.end()) - { - return false; - } - -if (ai->GetPassword() != password) - { - return false; - } - -*admin = &(*ai); +if (si != data.end()) + return true; -return true; +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, ADMIN_CONF * ac) 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."); @@ -285,16 +260,14 @@ if (searchDescriptors.find(h) == searchDescriptors.end()) if (searchDescriptors[h] == data.end()) return -1; -ADMIN_IMPL a = *searchDescriptors[h]++; - -*ac = a.GetConf(); +*service = *searchDescriptors[h]++; 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));