X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/fd947779e36534941c5d7516ba34e4da21b8406b..afcbfd1a09e22ff4839ba5db42047c96f355506c:/projects/stargazer/services_impl.cpp diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp index f7fd1d49..109a327b 100644 --- a/projects/stargazer/services_impl.cpp +++ b/projects/stargazer/services_impl.cpp @@ -33,7 +33,9 @@ SERVICES_IMPL::SERVICES_IMPL(STORE * st) store(st), WriteServLog(GetStgLogger()), searchDescriptors(), - handle(0) + handle(0), + mutex(), + strError() { pthread_mutex_init(&mutex, NULL); Read(); @@ -41,18 +43,18 @@ 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->serviceChg) { - string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied."; + std::string s = admin->GetLogStr() + " 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()) { @@ -77,20 +79,20 @@ WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Del(const string & name, const ADMIN * admin) +int SERVICES_IMPL::Del(const std::string & name, const ADMIN * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); const PRIV * priv = admin->GetPriv(); if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied."; + std::string s = admin->GetLogStr() + " 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(), SERVICE_CONF(name))); if (si == data.end()) { @@ -99,13 +101,13 @@ if (si == data.end()) 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); @@ -123,18 +125,18 @@ return 0; //----------------------------------------------------------------------------- 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->serviceChg) { - string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied."; + std::string s = admin->GetLogStr() + " 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()) { @@ -143,7 +145,9 @@ if (si == data.end()) return -1; } +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 service %s.", service.name.c_str()); @@ -159,8 +163,8 @@ return 0; //----------------------------------------------------------------------------- bool SERVICES_IMPL::Read() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -vector servicesList; +STG_LOCKER lock(&mutex); +std::vector servicesList; if (store->GetServicesList(&servicesList) < 0) { WriteServLog(store->GetStrError().c_str()); @@ -182,15 +186,34 @@ for (size_t i = 0; i < servicesList.size(); i++) return false; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::Find(const string & name, SERVICE_CONF * service) +bool SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF * service) const { assert(service != NULL && "Pointer to service is not null"); -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (data.empty()) + return true; + +const_iterator si(std::find(data.begin(), data.end(), SERVICE_CONF(name))); + +if (si != data.end()) + { + *service = *si; return false; + } + +return true; +} +//----------------------------------------------------------------------------- +bool SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF_RES * service) const +{ +assert(service != NULL && "Pointer to service is not null"); + +STG_LOCKER 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(), SERVICE_CONF(name))); if (si != data.end()) { @@ -201,16 +224,16 @@ if (si != data.end()) return true; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::Exists(const string & name) const +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; } -const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); +const_iterator si(std::find(data.begin(), data.end(), SERVICE_CONF(name))); if (si != data.end()) return true; @@ -220,7 +243,7 @@ return false; //----------------------------------------------------------------------------- int SERVICES_IMPL::OpenSearch() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); handle++; searchDescriptors[handle] = data.begin(); return handle; @@ -228,7 +251,7 @@ return handle; //----------------------------------------------------------------------------- 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."); @@ -245,7 +268,7 @@ 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));