X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/fcafc446aa1f15975b1802e47952ed371446e4d4..afcbfd1a09e22ff4839ba5db42047c96f355506c:/projects/stargazer/services_impl.cpp diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp index 400b34b3..109a327b 100644 --- a/projects/stargazer/services_impl.cpp +++ b/projects/stargazer/services_impl.cpp @@ -33,249 +33,225 @@ SERVICES_IMPL::SERVICES_IMPL(STORE * st) store(st), WriteServLog(GetStgLogger()), searchDescriptors(), - handle(0) + handle(0), + mutex(), + strError() { pthread_mutex_init(&mutex, NULL); -ReadServices(); +Read(); } //----------------------------------------------------------------------------- int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; + std::string s = admin->GetLogStr() + " 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."; + strError = "Service \'" + service.name + "\' cannot not be added. Service already exist."; WriteServLog("%s %s", admin->GetLogStr().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->GetLogStr().c_str(), service.name.c_str()); return 0; } -strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError(); +strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError(); WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Del(const string & login, const ADMIN * admin) +int SERVICES_IMPL::Del(const std::string & name, const ADMIN * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN_IMPL adm(0, login, ""); +STG_LOCKER lock(&mutex); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; + std::string s = admin->GetLogStr() + " 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(), SERVICE_CONF(name))); -if (ai == data.end()) +if (si == data.end()) { - strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist."; + strError = "Service \'" + name + "\' cannot be deleted. Service does not exist."; WriteServLog("%s %s", admin->GetLogStr().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.remove(*si); +if (store->DelService(name) < 0) { - strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError(); + strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError(); WriteServLog("%s %s", admin->GetLogStr().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->GetLogStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin) +int SERVICES_IMPL::Change(const SERVICE_CONF & service, const ADMIN * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; + std::string s = admin->GetLogStr() + " 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."; + strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist."; WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } -*ai = ac; -if (store->SaveAdmin(ac)) +printfd(__FILE__, "Old cost = %f, old pay day = %d\n", si->cost, (unsigned)si->payDay); +*si = service; +printfd(__FILE__, "New cost = %f, New pay day = %d\n", si->cost, (unsigned)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->GetLogStr().c_str(), service.name.c_str()); return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::ReadAdmins() +bool SERVICES_IMPL::Read() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -vector adminsList; -if (store->GetAdminsList(&adminsList) < 0) +STG_LOCKER 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], ""); + SERVICE_CONF 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 SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF * 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__); +STG_LOCKER 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(), SERVICE_CONF(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 SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF_RES * service) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +assert(service != NULL && "Pointer to service is not null"); + +STG_LOCKER 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(), SERVICE_CONF(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 SERVICES_IMPL::Exists(const std::string & name) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER 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)); - -if (ai == data.end()) - { - return false; - } +const_iterator si(std::find(data.begin(), data.end(), SERVICE_CONF(name))); -if (ai->GetPassword() != password) - { - return false; - } - -*admin = &(*ai); +if (si != data.end()) + return true; -return true; +return false; } //----------------------------------------------------------------------------- int SERVICES_IMPL::OpenSearch() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); handle++; searchDescriptors[handle] = data.begin(); return handle; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::SearchNext(int h, ADMIN_CONF * ac) const +int SERVICES_IMPL::SearchNext(int h, SERVICE_CONF * service) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (searchDescriptors.find(h) == searchDescriptors.end()) { WriteServLog("SERVICES. Incorrect search handle."); @@ -285,16 +261,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 { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (searchDescriptors.find(h) != searchDescriptors.end()) { searchDescriptors.erase(searchDescriptors.find(h));