From fd947779e36534941c5d7516ba34e4da21b8406b Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:10:00 +0300 Subject: [PATCH] Some fixes in service implementation --- .../stargazer/plugins/other/smux/sensors.h | 23 +-- projects/stargazer/services_impl.cpp | 165 ++++++------------ projects/stargazer/services_impl.h | 13 +- 3 files changed, 79 insertions(+), 122 deletions(-) diff --git a/projects/stargazer/plugins/other/smux/sensors.h b/projects/stargazer/plugins/other/smux/sensors.h index 16331b15..55780987 100644 --- a/projects/stargazer/plugins/other/smux/sensors.h +++ b/projects/stargazer/plugins/other/smux/sensors.h @@ -5,6 +5,9 @@ #include "stg/users.h" #include "stg/tariffs.h" +#include "stg/admins.h" +#include "stg/services.h" +#include "stg/corporations.h" #include "stg/user_property.h" #include "stg/ObjectSyntax.h" @@ -29,13 +32,13 @@ class TotalUsersSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(users.GetUserNum(), objectSyntax); + ValueToOS(users.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(users.GetUserNum(), res); return res; } + { std::string res; x2str(users.Count(), res); return res; } #endif private: @@ -165,13 +168,13 @@ class TotalTariffsSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(tariffs.GetTariffsNum(), objectSyntax); + ValueToOS(tariffs.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(tariffs.GetTariffsNum(), res); return res; } + { std::string res; x2str(tariffs.Count(), res); return res; } #endif private: @@ -185,13 +188,13 @@ class TotalAdminsSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(admins.GetAdminsNum(), objectSyntax); + ValueToOS(admins.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(admins.GetAdminsNum(), res); return res; } + { std::string res; x2str(admins.Count(), res); return res; } #endif private: @@ -205,13 +208,13 @@ class TotalServicesSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(services.GetServicesNum(), objectSyntax); + ValueToOS(services.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(services.GetServicesNum(), res); return res; } + { std::string res; x2str(services.Count(), res); return res; } #endif private: @@ -225,13 +228,13 @@ class TotalCorporationsSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(corporations.GetCorporationsNum(), objectSyntax); + ValueToOS(corporations.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(services.GetCorporationsNum(), res); return res; } + { std::string res; x2str(corporations.Count(), res); return res; } #endif private: diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp index 400b34b3..f7fd1d49 100644 --- a/projects/stargazer/services_impl.cpp +++ b/projects/stargazer/services_impl.cpp @@ -36,7 +36,7 @@ SERVICES_IMPL::SERVICES_IMPL(STORE * st) handle(0) { pthread_mutex_init(&mutex, NULL); -ReadServices(); +Read(); } //----------------------------------------------------------------------------- int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin) @@ -44,183 +44,164 @@ int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin) STG_LOCKER lock(&mutex, __FILE__, __LINE__); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; + 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)); +srv_iter si(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 string & name, const ADMIN * admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN_IMPL adm(0, login, ""); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; + 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)); +srv_iter si(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()) +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__); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; + 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)); +srv_iter si(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)) +*si = service; +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) +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 string & name, SERVICE_CONF * service) { -assert(admin != NULL && "Pointer to admin is not null"); +assert(service != NULL && "Pointer to service is not null"); STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - *admin = &noAdmin; return false; - } -ADMIN_IMPL adm(0, l, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +srv_iter si(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::Exists(const string & name) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) @@ -229,42 +210,14 @@ if (data.empty()) return true; } -ADMIN_IMPL adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); -if (ai != data.end()) +if (si != data.end()) return true; return false; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN ** admin) -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - return true; - } - -ADMIN_IMPL adm(0, login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); - -if (ai == data.end()) - { - return false; - } - -if (ai->GetPassword() != password) - { - return false; - } - -*admin = &(*ai); - -return true; -} -//----------------------------------------------------------------------------- int SERVICES_IMPL::OpenSearch() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -273,7 +226,7 @@ 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__); if (searchDescriptors.find(h) == searchDescriptors.end()) @@ -285,9 +238,7 @@ 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; } diff --git a/projects/stargazer/services_impl.h b/projects/stargazer/services_impl.h index 4c7044fa..d0c1c52c 100644 --- a/projects/stargazer/services_impl.h +++ b/projects/stargazer/services_impl.h @@ -32,20 +32,21 @@ #include "stg/locker.h" #include "stg/store.h" #include "stg/noncopyable.h" +#include "stg/logger.h" class ADMIN; class SERVICES_IMPL : private NONCOPYABLE, public SERVICES { public: SERVICES_IMPL(STORE * st); - virtual SERVICES_IMPL() {} + virtual ~SERVICES_IMPL() {} int Add(const SERVICE_CONF & service, const ADMIN * admin); int Del(const std::string & name, const ADMIN * admin); int Change(const SERVICE_CONF & service, const ADMIN * admin); - bool FindService(const std::string & name, SERVICE_CONF & service); - bool ServiceExists(const std::string & name) const; - const std::string & GetStrError() const; + bool Find(const std::string & name, SERVICE_CONF * service); + bool Exists(const std::string & name) const; + const std::string & GetStrError() const { return strError; } int OpenSearch() const; int SearchNext(int, SERVICE_CONF * service) const; @@ -55,7 +56,7 @@ private: typedef list::iterator srv_iter; typedef list::const_iterator const_srv_iter; - bool ReadServices(); + bool Read(); std::list data; STORE * store; @@ -65,3 +66,5 @@ private: mutable pthread_mutex_t mutex; std::string strError; }; + +#endif -- 2.43.2