From 99090ca5351c98fd9ce9773456a8a3c3936e8c60 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sun, 20 Oct 2013 12:37:13 +0300 Subject: [PATCH] Added services management. --- stglibs/srvconf.lib/Makefile | 3 + stglibs/srvconf.lib/include/stg/servconf.h | 9 ++ .../srvconf.lib/include/stg/servconf_types.h | 17 ++++ stglibs/srvconf.lib/parsers/chg_service.cpp | 52 ++++++++++ stglibs/srvconf.lib/parsers/chg_service.h | 42 ++++++++ stglibs/srvconf.lib/parsers/get_service.cpp | 99 +++++++++++++++++++ stglibs/srvconf.lib/parsers/get_service.h | 61 ++++++++++++ stglibs/srvconf.lib/parsers/get_services.cpp | 78 +++++++++++++++ stglibs/srvconf.lib/parsers/get_services.h | 62 ++++++++++++ stglibs/srvconf.lib/servconf.cpp | 36 +++++++ 10 files changed, 459 insertions(+) create mode 100644 stglibs/srvconf.lib/parsers/chg_service.cpp create mode 100644 stglibs/srvconf.lib/parsers/chg_service.h create mode 100644 stglibs/srvconf.lib/parsers/get_service.cpp create mode 100644 stglibs/srvconf.lib/parsers/get_service.h create mode 100644 stglibs/srvconf.lib/parsers/get_services.cpp create mode 100644 stglibs/srvconf.lib/parsers/get_services.h diff --git a/stglibs/srvconf.lib/Makefile b/stglibs/srvconf.lib/Makefile index 94f33741..ff0da14c 100644 --- a/stglibs/srvconf.lib/Makefile +++ b/stglibs/srvconf.lib/Makefile @@ -21,6 +21,9 @@ SRCS = parsers/property.cpp \ parsers/get_user.cpp \ parsers/get_users.cpp \ parsers/chg_user.cpp \ + parsers/get_services.cpp \ + parsers/get_service.cpp \ + parsers/chg_service.cpp \ netunit.cpp \ servconf.cpp diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index c9908640..b7884964 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -37,6 +37,7 @@ struct USER_CONF_RES; struct USER_STAT_RES; struct TARIFF_DATA_RES; +struct SERVICE_CONF_RES; namespace STG { @@ -80,6 +81,14 @@ public: int SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data); int CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data); + int GetServices(GET_SERVICES::CALLBACK f, void * data); + int GetService(const std::string & name, GET_SERVICE::CALLBACK f, void * data); + int ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data); + int AddService(const std::string & name, + const SERVICE_CONF_RES & conf, + SIMPLE::CALLBACK f, void * data); + int DelService(const std::string & name, SIMPLE::CALLBACK f, void * data); + const std::string & GetStrError() const; private: diff --git a/stglibs/srvconf.lib/include/stg/servconf_types.h b/stglibs/srvconf.lib/include/stg/servconf_types.h index 4c0e5927..6f454bc3 100644 --- a/stglibs/srvconf.lib/include/stg/servconf_types.h +++ b/stglibs/srvconf.lib/include/stg/servconf_types.h @@ -40,6 +40,7 @@ struct ADMIN_CONF; struct TARIFF_DATA; +struct SERVICE_CONF; namespace STG { @@ -190,6 +191,22 @@ typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & } +namespace GET_SERVICE +{ + +typedef SERVICE_CONF INFO; +typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); + +} + +namespace GET_SERVICES +{ + +typedef std::vector INFO; +typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); + +} + } // namespace STG #endif diff --git a/stglibs/srvconf.lib/parsers/chg_service.cpp b/stglibs/srvconf.lib/parsers/chg_service.cpp new file mode 100644 index 00000000..2eed494f --- /dev/null +++ b/stglibs/srvconf.lib/parsers/chg_service.cpp @@ -0,0 +1,52 @@ +/* + * 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 "chg_service.h" + +#include "stg/service_conf.h" +#include "stg/common.h" + +#include + +using namespace STG; + +namespace +{ + +template +void appendResetable(std::ostream & stream, const std::string & name, const T & value) +{ +if (!value.empty()) + stream << "<" << name << " value=\"" << value.data() << "\"/>"; +} + +} // namespace anonymous + +std::string CHG_SERVICE::Serialize(const SERVICE_CONF_RES & conf) +{ +std::ostringstream stream; + +appendResetable(stream, "name", conf.name); +appendResetable(stream, "comment", conf.comment); +appendResetable(stream, "cost", conf.cost); +appendResetable(stream, "payDay", conf.payDay); + +return stream.str(); +} diff --git a/stglibs/srvconf.lib/parsers/chg_service.h b/stglibs/srvconf.lib/parsers/chg_service.h new file mode 100644 index 00000000..10fbf6fb --- /dev/null +++ b/stglibs/srvconf.lib/parsers/chg_service.h @@ -0,0 +1,42 @@ +/* + * 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_STGLIBS_SRVCONF_PARSER_CHG_SERVICE_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_CHG_SERVICE_H__ + +#include "base.h" + +#include "stg/servconf_types.h" + +#include + +struct SERVICE_CONF_RES; + +namespace STG +{ +namespace CHG_SERVICE +{ + +std::string Serialize(const SERVICE_CONF_RES & conf); + +} // namespace CHG_SERVICE +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/get_service.cpp b/stglibs/srvconf.lib/parsers/get_service.cpp new file mode 100644 index 00000000..105b7395 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_service.cpp @@ -0,0 +1,99 @@ +/* + * 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 "get_service.h" + +#include "parsers/property.h" + +#include "stg/common.h" + +#include + +using namespace STG; + +GET_SERVICE::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), + depth(0), + parsingAnswer(false) +{ + AddParser(propertyParsers, "name", info.name); + AddParser(propertyParsers, "comment", info.comment); + AddParser(propertyParsers, "cost", info.cost); + AddParser(propertyParsers, "payDay", info.payDay); +} +//----------------------------------------------------------------------------- +GET_SERVICE::PARSER::~PARSER() +{ + PROPERTY_PARSERS::iterator it(propertyParsers.begin()); + while (it != propertyParsers.end()) + delete (it++)->second; +} +//----------------------------------------------------------------------------- +int GET_SERVICE::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1) + ParseService(el, attr); + +if (depth == 2 && parsingAnswer) + ParseServiceParams(el, attr); + +return 0; +} +//----------------------------------------------------------------------------- +void GET_SERVICE::PARSER::ParseEnd(const char * /*el*/) +{ +depth--; +if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + parsingAnswer = false; + } +} +//----------------------------------------------------------------------------- +void GET_SERVICE::PARSER::ParseService(const char * el, const char ** attr) +{ +if (strcasecmp(el, "service") == 0) + { + if (attr && attr[0] && attr[1]) + { + if (strcasecmp(attr[1], "error") == 0) + { + if (attr[2] && attr[3]) + error = attr[3]; + else + error = "Service not found."; + } + else + parsingAnswer = true; + } + else + parsingAnswer = true; + } +} +//----------------------------------------------------------------------------- +void GET_SERVICE::PARSER::ParseServiceParams(const char * el, const char ** attr) +{ +if (!TryParse(propertyParsers, ToLower(el), attr)) + error = "Invalid parameter."; +} diff --git a/stglibs/srvconf.lib/parsers/get_service.h b/stglibs/srvconf.lib/parsers/get_service.h new file mode 100644 index 00000000..24c04849 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_service.h @@ -0,0 +1,61 @@ +/* + * 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_STGLIBS_SRVCONF_PARSER_GET_SERVICE_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_GET_SERVICE_H__ + +#include "base.h" +#include "property.h" + +#include "stg/service_conf.h" +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace GET_SERVICE +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(CALLBACK f, void * data); + virtual ~PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + +private: + PROPERTY_PARSERS propertyParsers; + CALLBACK callback; + void * data; + INFO info; + int depth; + bool parsingAnswer; + std::string error; + + void ParseService(const char * el, const char ** attr); + void ParseServiceParams(const char * el, const char ** attr); +}; + +} // namespace GET_SERVICE +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/get_services.cpp b/stglibs/srvconf.lib/parsers/get_services.cpp new file mode 100644 index 00000000..ec26f126 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_services.cpp @@ -0,0 +1,78 @@ +/* + * 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 "get_services.h" + +#include "stg/service_conf.h" + +#include + +using namespace STG; + +GET_SERVICES::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), + serviceParser(&GET_SERVICES::PARSER::ServiceCallback, this), + depth(0), + parsingAnswer(false) +{ +} +//----------------------------------------------------------------------------- +int GET_SERVICES::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1 && strcasecmp(el, "services") == 0) + parsingAnswer = true; + +if (depth > 1 && parsingAnswer) + serviceParser.ParseStart(el, attr); + +return 0; +} +//----------------------------------------------------------------------------- +void GET_SERVICES::PARSER::ParseEnd(const char * el) +{ +depth--; +if (depth > 0 && parsingAnswer) + serviceParser.ParseEnd(el); + +if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + info.clear(); + parsingAnswer = false; + } +} +//----------------------------------------------------------------------------- +void GET_SERVICES::PARSER::AddService(const GET_SERVICE::INFO & serviceInfo) +{ +info.push_back(serviceInfo); +} +//----------------------------------------------------------------------------- +void GET_SERVICES::PARSER::ServiceCallback(bool result, const std::string & error, const GET_SERVICE::INFO & info, void * data) +{ + GET_SERVICES::PARSER * parser = static_cast(data); + if (!result) + parser->SetError(error); + else + parser->AddService(info); +} diff --git a/stglibs/srvconf.lib/parsers/get_services.h b/stglibs/srvconf.lib/parsers/get_services.h new file mode 100644 index 00000000..1e02c938 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_services.h @@ -0,0 +1,62 @@ +/* + * 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_STGLIBS_SRVCONF_PARSER_GET_SERVICES_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_GET_SERVICES_H__ + +#include "base.h" +#include "get_service.h" + +#include "stg/service_conf.h" +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace GET_SERVICES +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(CALLBACK f, void * data); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + +private: + CALLBACK callback; + void * data; + GET_SERVICE::PARSER serviceParser; + INFO info; + int depth; + bool parsingAnswer; + std::string error; + + void AddService(const GET_SERVICE::INFO & serviceInfo); + void SetError(const std::string & e) { error = e; } + + static void ServiceCallback(bool result, const std::string& reason, const GET_SERVICE::INFO & info, void * data); +}; + +} // namespace GET_SERVICES +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 92773d7f..11e755ba 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -39,6 +39,10 @@ #include "parsers/get_user.h" #include "parsers/chg_user.h" +#include "parsers/get_services.h" +#include "parsers/get_service.h" +#include "parsers/chg_service.h" + #include "parsers/base.h" #include "stg/common.h" @@ -239,6 +243,38 @@ int SERVCONF::CheckUser(const std::string & login, const std::string & password, return pImpl->Exec("CheckUser", "", f, data); } +// -- Services -- + +int SERVCONF::GetServices(GET_SERVICES::CALLBACK f, void * data) +{ +return pImpl->Exec("", f, data); +} + +int SERVCONF::GetService(const std::string & name, GET_SERVICE::CALLBACK f, void * data) +{ +return pImpl->Exec("", f, data); +} + +int SERVCONF::ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data) +{ +return pImpl->Exec("SetService", "" + CHG_SERVICE::Serialize(conf) + "", f, data); +} + +int SERVCONF::AddService(const std::string & name, + const SERVICE_CONF_RES & conf, + SIMPLE::CALLBACK f, void * data) +{ +int res = pImpl->Exec("AddService", "", f, data); +if (res != st_ok) + return res; +return pImpl->Exec("SetService", "" + CHG_SERVICE::Serialize(conf) + "", f, data); +} + +int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * data) +{ +return pImpl->Exec("DelService", "", f, data); +} + const std::string & SERVCONF::GetStrError() const { return pImpl->GetStrError(); -- 2.44.2