From: Maxim Mamontov Date: Sun, 15 Sep 2013 02:05:04 +0000 (+0300) Subject: [NY] Implemented SERVCONF using PIMPL. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/359e4b28458d352dc55ac39db7e4b5bb308eb361 [NY] Implemented SERVCONF using PIMPL. --- diff --git a/projects/sgconf/common_sg.h b/projects/sgconf/common_sg.h index 4bff0aa7..87b8509b 100644 --- a/projects/sgconf/common_sg.h +++ b/projects/sgconf/common_sg.h @@ -29,6 +29,7 @@ #define COMMON_SG_H #include "stg/servconf.h" +#include "stg/servconf_types.h" #include "request.h" #include diff --git a/stglibs/srvconf.lib/include/stg/netunit.h b/stglibs/srvconf.lib/include/stg/netunit.h index 9e120855..4d3bcfb0 100644 --- a/stglibs/srvconf.lib/include/stg/netunit.h +++ b/stglibs/srvconf.lib/include/stg/netunit.h @@ -27,52 +27,12 @@ #ifndef NetUnitH #define NetUnitH -#include "stg/blowfish.h" +#include "stg/os_int.h" #include -#include -#include -#include - -#define STG_HEADER "SG04" -#define OK_HEADER "OKHD" -#define ERR_HEADER "ERHD" -#define OK_LOGIN "OKLG" -#define ERR_LOGIN "ERLG" -#define OK_LOGINS "OKLS" -#define ERR_LOGINS "ERLS" - -// äÌÉÎÎÁ ÛÉÆÒÕÅÍÏÇÏ É ÐÅÒÅÄÁ×ÁÅÍÏÇ ÚÁ ÏÄÉÎ ÒÁÚ ÂÌÏËÁ ÉÎÆÏÒÍÁÃÉÉ -#define ENC_MSG_LEN (8) - -#define MAX_ERR_STR_LEN (64) - typedef bool (* RxCallback_t)(void *, const std::string &, bool); -enum status -{ -st_ok = 0, -st_conn_fail, -st_send_fail, -st_recv_fail, -st_header_err, -st_login_err, -st_logins_err, -st_data_err, -st_unknown_err, -st_dns_err, -st_xml_parse_error, -st_data_error -}; - -enum CONF_STATE -{ -confHdr = 0, -confLogin, -confLoginCipher, -confData -}; //--------------------------------------------------------------------------- class NETTRANSACT { diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index 285136ea..32ee1cd4 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -27,8 +27,6 @@ #ifndef SERVCONF_H #define SERVCONF_H -#include "netunit.h" - #include "stg/parser_auth_by.h" #include "stg/parser_server_info.h" #include "stg/parser_check_user.h" @@ -41,13 +39,12 @@ #include -#include - class SERVCONF { public: SERVCONF(const std::string & server, uint16_t port, const std::string & login, const std::string & password); + ~SERVCONF(); int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data); int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data); @@ -58,28 +55,10 @@ public: int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data); const std::string & GetStrError() const; - void Start(const char * el, const char ** attr); - void End(const char * el); private: - PARSER * currParser; - - PARSER_GET_USERS parserGetUsers; - PARSER_GET_USER parserGetUser; - PARSER_AUTH_BY parserAuthBy; - PARSER_SERVER_INFO parserServerInfo; - PARSER_CHG_USER parserChgUser; - PARSER_CHECK_USER parserCheckUser; - PARSER_SEND_MESSAGE parserSendMessage; - - NETTRANSACT nt; - - std::string errorMsg; - XML_Parser parser; - - int Exec(const std::string & request, PARSER & cp); - - friend bool AnsRecv(void * data, const std::string & chunk, bool final); + class IMPL; + IMPL * pImpl; }; //----------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/include/stg/servconf_types.h b/stglibs/srvconf.lib/include/stg/servconf_types.h new file mode 100644 index 00000000..d7fb8478 --- /dev/null +++ b/stglibs/srvconf.lib/include/stg/servconf_types.h @@ -0,0 +1,58 @@ +/* + * 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_TYPES_H__ +#define __STG_STGLIBS_SRVCONF_TYPES_H__ + +#define STG_HEADER "SG04" +#define OK_HEADER "OKHD" +#define ERR_HEADER "ERHD" +#define OK_LOGIN "OKLG" +#define ERR_LOGIN "ERLG" +#define OK_LOGINS "OKLS" +#define ERR_LOGINS "ERLS" + +#define ENC_MSG_LEN (8) + +enum status +{ +st_ok = 0, +st_conn_fail, +st_send_fail, +st_recv_fail, +st_header_err, +st_login_err, +st_logins_err, +st_data_err, +st_unknown_err, +st_dns_err, +st_xml_parse_error, +st_data_error +}; + +enum CONF_STATE +{ +confHdr = 0, +confLogin, +confLoginCipher, +confData +}; + +#endif diff --git a/stglibs/srvconf.lib/netunit.cpp b/stglibs/srvconf.lib/netunit.cpp index 4c3cc92e..68d64678 100644 --- a/stglibs/srvconf.lib/netunit.cpp +++ b/stglibs/srvconf.lib/netunit.cpp @@ -27,6 +27,7 @@ //--------------------------------------------------------------------------- #include "stg/netunit.h" +#include "stg/servconf_types.h" #include "stg/common.h" #include "stg/blowfish.h" @@ -38,6 +39,10 @@ #include #include +#include +#include +#include + namespace { diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 268749a3..45d60f66 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -20,29 +20,72 @@ #include "stg/servconf.h" +#include "stg/netunit.h" +#include "stg/servconf_types.h" + #include "stg/common.h" #include #include -namespace +#include + +class SERVCONF::IMPL +{ +public: + IMPL(const std::string & server, uint16_t port, + const std::string & login, const std::string & password); + + int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data); + int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data); + int ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data); + int AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data); + int SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data); + int ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data); + int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data); + + const std::string & GetStrError() const; + static void Start(void * data, const char * el, const char ** attr); + static void End(void * data, const char * el); + +private: + PARSER_GET_USERS parserGetUsers; + PARSER_GET_USER parserGetUser; + PARSER_AUTH_BY parserAuthBy; + PARSER_SERVER_INFO parserServerInfo; + PARSER_CHG_USER parserChgUser; + PARSER_CHECK_USER parserCheckUser; + PARSER_SEND_MESSAGE parserSendMessage; + + NETTRANSACT nt; + + std::string errorMsg; + XML_Parser parser; + + int Exec(const std::string & request, PARSER & cp); + + static bool AnsRecv(void * data, const std::string & chunk, bool final); +}; + +/*namespace { void ElementStart(void * data, const char * el, const char ** attr) { -static_cast(data)->Start(el, attr); +static_cast(data)->Start(el, attr); } void ElementEnd(void * data, const char * el) { -static_cast(data)->End(el); +static_cast(data)->End(el); } } // namespace anonymous +*/ -bool AnsRecv(void * data, const std::string & chunk, bool final) +bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final) { -SERVCONF * sc = static_cast(data); +SERVCONF::IMPL * sc = static_cast(data); if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR) { @@ -56,80 +99,130 @@ if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ER return true; } -//----------------------------------------------------------------------------- SERVCONF::SERVCONF(const std::string & server, uint16_t port, const std::string & login, const std::string & password) - : currParser(NULL), - nt( server, port, login, password ) + : pImpl(new IMPL(server, port, login, password)) +{ +} + +SERVCONF::~SERVCONF() +{ + delete pImpl; +} + +int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data) +{ + return pImpl->GetUsers( f, data ); +} + +int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data) +{ + return pImpl->GetUser(login, f, data); +} + +int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data) +{ + return pImpl->ChgUser(request, f, data); +} + +int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data) +{ + return pImpl->AuthBy(login, f, data); +} + +int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data) +{ + return pImpl->SendMessage(request, f, data); +} + +int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data) +{ + return pImpl->ServerInfo(f, data); +} + +int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data) +{ + return pImpl->CheckUser(login, password, f, data); +} + +const std::string & SERVCONF::GetStrError() const +{ + return pImpl->GetStrError(); +} + +//----------------------------------------------------------------------------- +SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port, + const std::string & login, const std::string & password) + : nt( server, port, login, password ) { parser = XML_ParserCreate(NULL); nt.SetRxCallback(this, AnsRecv); } //----------------------------------------------------------------------------- -int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data) +int SERVCONF::IMPL::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data) { parserGetUser.SetCallback(f, data); return Exec("", parserGetUser); } //----------------------------------------------------------------------------- -int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data) +int SERVCONF::IMPL::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data) { parserAuthBy.SetCallback(f, data); return Exec("", parserAuthBy); } //----------------------------------------------------------------------------- -int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data) +int SERVCONF::IMPL::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data) { parserGetUsers.SetCallback(f, data); return Exec("", parserGetUsers); } //----------------------------------------------------------------------------- -int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data) +int SERVCONF::IMPL::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data) { parserServerInfo.SetCallback(f, data); return Exec("", parserServerInfo); } //----------------------------------------------------------------------------- -int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data) +int SERVCONF::IMPL::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data) { parserChgUser.SetCallback(f, data); return Exec(request, parserChgUser); } //----------------------------------------------------------------------------- -int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data) +int SERVCONF::IMPL::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data) { parserSendMessage.SetCallback(f, data); return Exec(request, parserSendMessage); } //----------------------------------------------------------------------------- -int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data) +int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data) { parserCheckUser.SetCallback(f, data); return Exec("", parserCheckUser); } //----------------------------------------------------------------------------- -void SERVCONF::Start(const char * el, const char ** attr) +void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr) { +PARSER * currParser = static_cast(data); currParser->ParseStart(el, attr); } //----------------------------------------------------------------------------- -void SERVCONF::End(const char * el) +void SERVCONF::IMPL::End(void * data, const char * el) { +PARSER * currParser = static_cast(data); currParser->ParseEnd(el); } //----------------------------------------------------------------------------- -const std::string & SERVCONF::GetStrError() const +const std::string & SERVCONF::IMPL::GetStrError() const { return errorMsg; } //----------------------------------------------------------------------------- -int SERVCONF::Exec(const std::string & request, PARSER & cp) +int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp) { -currParser = &cp; - XML_ParserReset(parser, NULL); -XML_SetElementHandler(parser, ElementStart, ElementEnd); -XML_SetUserData(parser, this); +XML_SetElementHandler(parser, Start, End); +XML_SetUserData(parser, &cp); int ret = 0; if ((ret = nt.Connect()) != st_ok)