From 56d1a7c8820bd8913b68eafb8566a4185dd188b1 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sun, 28 Sep 2014 18:35:56 +0300 Subject: [PATCH] Implemented services parser. --- include/stg/service_conf.h | 13 ++ include/stg/services.h | 2 +- .../plugins/configuration/sgconfig/Makefile | 1 + .../sgconfig/parser_services.cpp | 199 ++++++++++++++++++ .../configuration/sgconfig/parser_services.h | 175 +++++++++++++++ projects/stargazer/services_impl.cpp | 4 +- projects/stargazer/services_impl.h | 2 +- 7 files changed, 392 insertions(+), 4 deletions(-) create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_services.h diff --git a/include/stg/service_conf.h b/include/stg/service_conf.h index 473830e4..84f052a5 100644 --- a/include/stg/service_conf.h +++ b/include/stg/service_conf.h @@ -58,6 +58,11 @@ SERVICE_CONF_RES() cost(), payDay() {} +SERVICE_CONF_RES(const SERVICE_CONF & rhs) + : name(rhs.name), comment(rhs.comment), + cost(rhs.cost), payDay(rhs.payDay) +{} + SERVICE_CONF_RES & operator=(const SERVICE_CONF & conf) { name = conf.name; @@ -77,6 +82,14 @@ sc.payDay = payDay.data(); return sc; } +void Splice(const SERVICE_CONF_RES & rhs) +{ +name.splice(rhs.name); +comment.splice(rhs.comment); +cost.splice(rhs.cost); +payDay.splice(rhs.payDay); +} + RESETABLE name; RESETABLE comment; RESETABLE cost; diff --git a/include/stg/services.h b/include/stg/services.h index 8c49f781..1fe4dca1 100644 --- a/include/stg/services.h +++ b/include/stg/services.h @@ -33,7 +33,7 @@ public: virtual int Add(const SERVICE_CONF & service, const ADMIN * admin) = 0; virtual int Del(const std::string & name, const ADMIN * admin) = 0; virtual int Change(const SERVICE_CONF & service, const ADMIN * admin) = 0; - virtual bool Find(const std::string & name, SERVICE_CONF * service) = 0; + virtual bool Find(const std::string & name, SERVICE_CONF * service) const = 0; virtual bool Exists(const std::string & name) const = 0; virtual const std::string & GetStrError() const = 0; virtual size_t Count() const = 0; diff --git a/projects/stargazer/plugins/configuration/sgconfig/Makefile b/projects/stargazer/plugins/configuration/sgconfig/Makefile index 40a5c534..a83bb86c 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/Makefile +++ b/projects/stargazer/plugins/configuration/sgconfig/Makefile @@ -13,6 +13,7 @@ SRCS = ./stgconfig.cpp \ ./parser_tariffs.cpp \ ./parser_admins.cpp \ ./parser_users.cpp \ + ./parser_services.cpp \ ./parser_message.cpp \ ./parser_auth_by.cpp \ ./parser_user_info.cpp \ diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp new file mode 100644 index 00000000..59e7f03c --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp @@ -0,0 +1,199 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#include "parser_services.h" + +#include "stg/services.h" + +#include // strcasecmp + +using STG::PARSER::GET_SERVICES; +using STG::PARSER::GET_SERVICE; +using STG::PARSER::ADD_SERVICE; +using STG::PARSER::DEL_SERVICE; +using STG::PARSER::CHG_SERVICE; + +const char * GET_SERVICES::tag = "GetServices"; +const char * GET_SERVICE::tag = "AddService"; +const char * ADD_SERVICE::tag = "AddService"; +const char * DEL_SERVICE::tag = "DelService"; +const char * CHG_SERVICE::tag = "ChgService"; + +void GET_SERVICES::CreateAnswer() +{ + // TODO: no priviledges implemented yet + /*const PRIV * priv = m_currAdmin.GetPriv(); + if (!priv->serviceChg) + { + m_answer = ""; + return; + }*/ + + m_answer = ""; + SERVICE_CONF conf; + int h = m_services.OpenSearch(); + while (m_services.SearchNext(h, &conf) == 0) + { + m_answer += ""; + } + m_services.CloseSearch(h); + m_answer += ""; +} + +int GET_SERVICE::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, m_tag.c_str()) == 0) + { + m_name = attr[1]; + return 0; + } + return -1; +} + +void GET_SERVICE::CreateAnswer() +{ + // TODO: no priviledges implemented yet + /*const PRIV * priv = m_currAdmin.GetPriv(); + if (!priv->serviceChg) + { + m_answer = ""; + return; + }*/ + + SERVICE_CONF conf; + if (!m_services.Find(m_name, &conf)) + m_answer = ""; + else + m_answer += "<" + m_tag + " name=\"" + conf.name + + "\" comment=\"" + Encode12str(conf.comment) + + "\" cost=\"" + x2str(conf.cost) + + "\" payDay=\"" + x2str(conf.payDay) + "\"/>"; +} + +int ADD_SERVICE::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, m_tag.c_str()) == 0) + { + m_name = attr[1]; + return 0; + } + return -1; +} + +void ADD_SERVICE::CreateAnswer() +{ + SERVICE_CONF conf(m_name); + if (m_services.Add(conf, &m_currAdmin) == 0) + m_answer = "<" + m_tag + " result=\"Ok\"/>"; + else + m_answer = "<" + m_tag + " result=\"Error. " + m_services.GetStrError() + "\"/>"; +} + +int DEL_SERVICE::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, m_tag.c_str()) == 0) + { + m_name = attr[1]; + return 0; + } + return -1; +} + +void DEL_SERVICE::CreateAnswer() +{ + if (m_services.Del(m_name, &m_currAdmin) == 0) + m_answer = "<" + m_tag + " result=\"Ok\"/>"; + else + m_answer = "<" + m_tag + " result=\"Error. " + m_services.GetStrError() + "\"/>"; +} + +int CHG_SERVICE::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, m_tag.c_str()) == 0) + { + for (size_t i = 0; i < 8; i += 2) + { + printfd(__FILE__, "PARSER_CHG_SERVICE::attr[%d] = %s\n", i, attr[i]); + if (attr[i] == NULL) + break; + + if (strcasecmp(attr[i], "name") == 0) + { + m_service.name = attr[i + 1]; + continue; + } + + if (strcasecmp(attr[i], "comment") == 0) + { + m_service.comment = Decode21str(attr[i + 1]); + continue; + } + + if (strcasecmp(attr[i], "cost") == 0) + { + double cost = 0; + if (str2x(attr[i + 1], cost) == 0) + m_service.cost = cost; + // TODO: log it + continue; + } + + if (strcasecmp(attr[i], "payDay") == 0) + { + unsigned payDay; + if (str2x(attr[i + 1], payDay) == 0) + m_service.payDay = payDay; + // TODO: log it + continue; + } + } + + return 0; + } + return -1; +} + +void CHG_SERVICE::CreateAnswer() +{ + if (m_service.name.empty()) + { + m_answer = "<" + m_tag + " result=\"Empty service name.\"/>"; + return; + } + + if (!m_services.Exists(m_service.name.const_data())) + { + m_answer = "<" + m_tag + " result = \"Service '" + m_service.name.const_data() + "' does not exist.\"/>"; + return; + } + + SERVICE_CONF orig; + m_services.Find(m_service.name.const_data(), &orig); + + m_service.Splice(orig); + + if (m_services.Change(m_service.GetData(), &m_currAdmin) != 0) + m_answer = "<" + m_tag + " result = \"" + m_services.GetStrError() + "\"/>"; + else + m_answer = "<" + m_tag + " result = \"Ok\"/>"; +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_services.h b/projects/stargazer/plugins/configuration/sgconfig/parser_services.h new file mode 100644 index 00000000..f4d87fe9 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_services.h @@ -0,0 +1,175 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONFIG_PARSER_SERVICES_H__ +#define __STG_SGCONFIG_PARSER_SERVICES_H__ + +#include "parser.h" + +#include "stg/service_conf.h" + +#include "stg/common.h" + +#include + +class SERVICES; + +namespace STG +{ +namespace PARSER +{ + +class GET_SERVICES: public BASE_PARSER +{ + public: + class FACTORY : public BASE_PARSER::FACTORY + { + public: + FACTORY(const SERVICES & services) : m_services(services) {} + virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_SERVICES(admin, m_services); } + static void Register(REGISTRY & registry, const SERVICES & services) + { registry[ToLower(tag)] = new FACTORY(services); } + private: + const SERVICES & m_services; + }; + + static const char * tag; + + GET_SERVICES(const ADMIN & admin, const SERVICES & services) + : BASE_PARSER(admin, tag), m_services(services) {} + + private: + const SERVICES & m_services; + + void CreateAnswer(); +}; + +class GET_SERVICE: public BASE_PARSER +{ + public: + class FACTORY : public BASE_PARSER::FACTORY + { + public: + FACTORY(const SERVICES & services) : m_services(services) {} + virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_SERVICE(admin, m_services); } + static void Register(REGISTRY & registry, SERVICES & services) + { registry[ToLower(tag)] = new FACTORY(services); } + private: + const SERVICES & m_services; + }; + + static const char * tag; + + GET_SERVICE(const ADMIN & admin, const SERVICES & services) + : BASE_PARSER(admin, tag), m_services(services) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string m_name; + const SERVICES & m_services; + + void CreateAnswer(); +}; + +class ADD_SERVICE: public BASE_PARSER +{ + public: + class FACTORY : public BASE_PARSER::FACTORY + { + public: + FACTORY(SERVICES & services) : m_services(services) {} + virtual BASE_PARSER * create(const ADMIN & admin) { return new ADD_SERVICE(admin, m_services); } + static void Register(REGISTRY & registry, SERVICES & services) + { registry[ToLower(tag)] = new FACTORY(services); } + private: + SERVICES & m_services; + }; + + static const char * tag; + + ADD_SERVICE(const ADMIN & admin, SERVICES & services) + : BASE_PARSER(admin, tag), m_services(services) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string m_name; + SERVICES & m_services; + + void CreateAnswer(); +}; + +class DEL_SERVICE: public BASE_PARSER +{ + public: + class FACTORY : public BASE_PARSER::FACTORY + { + public: + FACTORY(SERVICES & services) : m_services(services) {} + virtual BASE_PARSER * create(const ADMIN & admin) { return new DEL_SERVICE(admin, m_services); } + static void Register(REGISTRY & registry, SERVICES & services) + { registry[ToLower(tag)] = new FACTORY(services); } + private: + SERVICES & m_services; + }; + + static const char * tag; + + DEL_SERVICE(const ADMIN & admin, SERVICES & services) + : BASE_PARSER(admin, tag), m_services(services) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string m_name; + SERVICES & m_services; + + void CreateAnswer(); +}; + +class CHG_SERVICE: public BASE_PARSER +{ + public: + class FACTORY : public BASE_PARSER::FACTORY + { + public: + FACTORY(SERVICES & services) : m_services(services) {} + virtual BASE_PARSER * create(const ADMIN & admin) { return new CHG_SERVICE(admin, m_services); } + static void Register(REGISTRY & registry, SERVICES & services) + { registry[ToLower(tag)] = new FACTORY(services); } + private: + SERVICES & m_services; + }; + + static const char * tag; + + CHG_SERVICE(const ADMIN & admin, SERVICES & services) + : BASE_PARSER(admin, tag), m_services(services) {} + int Start(void * data, const char * el, const char ** attr); + + private: + SERVICE_CONF_RES m_service; + SERVICES & m_services; + + void CreateAnswer(); +}; + +} // namespace PARSER +} // namespace STG + +#endif diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp index aebb98fb..ea231615 100644 --- a/projects/stargazer/services_impl.cpp +++ b/projects/stargazer/services_impl.cpp @@ -184,7 +184,7 @@ for (size_t i = 0; i < servicesList.size(); i++) return false; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::Find(const std::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"); @@ -192,7 +192,7 @@ STG_LOCKER lock(&mutex); if (data.empty()) return false; -srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); +const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); if (si != data.end()) { diff --git a/projects/stargazer/services_impl.h b/projects/stargazer/services_impl.h index 31aa72b2..254df8a1 100644 --- a/projects/stargazer/services_impl.h +++ b/projects/stargazer/services_impl.h @@ -44,7 +44,7 @@ public: 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 Find(const std::string & name, SERVICE_CONF * service); + bool Find(const std::string & name, SERVICE_CONF * service) const; bool Exists(const std::string & name) const; const std::string & GetStrError() const { return strError; } -- 2.44.2