From 8f1d13e63e83c5f590d59f6d09f2d1dae69ba526 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 5 Aug 2013 21:24:16 +0300 Subject: [PATCH 1/1] Configuration library refactoring (AUTH_BY) --- projects/sgconf/common_sg.cpp | 43 +++------------ stglibs/srvconf.lib/Makefile | 5 +- stglibs/srvconf.lib/include/stg/parser.h | 12 +++++ .../srvconf.lib/include/stg/parser_auth_by.h | 26 ++++++++++ stglibs/srvconf.lib/include/stg/servconf.h | 45 ++++------------ stglibs/srvconf.lib/parser.cpp | 52 ------------------- stglibs/srvconf.lib/parser_auth_by.cpp | 46 ++++++++++++++++ stglibs/srvconf.lib/servconf.cpp | 15 +++--- 8 files changed, 113 insertions(+), 131 deletions(-) create mode 100644 stglibs/srvconf.lib/include/stg/parser.h create mode 100644 stglibs/srvconf.lib/include/stg/parser_auth_by.h create mode 100644 stglibs/srvconf.lib/parser_auth_by.cpp diff --git a/projects/sgconf/common_sg.cpp b/projects/sgconf/common_sg.cpp index bf2ac622..b73a79c8 100644 --- a/projects/sgconf/common_sg.cpp +++ b/projects/sgconf/common_sg.cpp @@ -52,12 +52,6 @@ struct GetUserCbData void * data; bool * result; }; -//----------------------------------------------------------------------------- -struct AuthByCbData -{ - void * data; - bool * result; -}; //--------------------------------------------------------------------------- struct HelpParams { @@ -418,20 +412,11 @@ for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++) *result = true; } //----------------------------------------------------------------------------- -void RecvAuthByData(const std::vector & list, void * d) +void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *) { -AuthByCbData * abcbd; -abcbd = (AuthByCbData *)d; - -bool * result = abcbd->result; - -REQUEST * req = (REQUEST *)abcbd->data; - for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) cout << *it << "\n"; cout << endl; - -*result = true; } //----------------------------------------------------------------------------- int ProcessSetUser(const std::string &server, @@ -531,33 +516,21 @@ int ProcessAuthBy(const std::string &server, { SERVCONF sc; -bool result = false; - -sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ -sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ -sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ +sc.SetServer(server.c_str()); +sc.SetPort(port); +sc.SetAdmLogin(admLogin.c_str()); sc.SetAdmPassword(admPasswd.c_str()); -// TODO Good variable name :) -AuthByCbData abcbd; - -abcbd.data = data; -abcbd.result = &result; - -sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd); -sc.GetUserAuthBy(login.c_str()); +sc.SetAuthByCallback(RecvAuthByData, NULL); +sc.AuthBy(login.c_str()); -if (result) - { - printf("Ok\n"); - return 0; - } -else +if (sc.GetError()) { printf("Error\n"); return -1; } +printf("Ok\n"); return 0; } //----------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/Makefile b/stglibs/srvconf.lib/Makefile index e3bfa762..ab170663 100644 --- a/stglibs/srvconf.lib/Makefile +++ b/stglibs/srvconf.lib/Makefile @@ -8,14 +8,15 @@ STGLIBS = -lstgcommon \ -lstgcrypto LIBS = -lexpat -SRCS = netunit.cpp \ +SRCS = netunit.cpp \ parser.cpp \ + parser_auth_by.cpp \ servconf.cpp INCS = servconf.h \ netunit.h LIB_INCS = -I ../common.lib/include \ - -I ../crypto.lib/include + -I ../crypto.lib/include include ../Makefile.in diff --git a/stglibs/srvconf.lib/include/stg/parser.h b/stglibs/srvconf.lib/include/stg/parser.h new file mode 100644 index 00000000..66263692 --- /dev/null +++ b/stglibs/srvconf.lib/include/stg/parser.h @@ -0,0 +1,12 @@ +#ifndef __STG_STGLIBS_SRVCONF_PARSER_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_H__ + +class PARSER +{ +public: + virtual ~PARSER() {} + virtual int ParseStart(const char *el, const char **attr) = 0; + virtual void ParseEnd(const char *el) = 0; +}; + +#endif diff --git a/stglibs/srvconf.lib/include/stg/parser_auth_by.h b/stglibs/srvconf.lib/include/stg/parser_auth_by.h new file mode 100644 index 00000000..3ef1d00c --- /dev/null +++ b/stglibs/srvconf.lib/include/stg/parser_auth_by.h @@ -0,0 +1,26 @@ +#ifndef __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ + +#include "stg/parser.h" + +#include +#include + +class PARSER_AUTH_BY: public PARSER +{ +public: + typedef std::vector INFO; + typedef void (* CALLBACK)(const INFO & info, void * data); + + PARSER_AUTH_BY(); + int ParseStart(const char *el, const char **attr); + void ParseEnd(const char *el); + void SetCallback(CALLBACK f, void * data); +private: + CALLBACK callback; + void * data; + int depth; + INFO info; +}; + +#endif diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index dbbe2001..b9705851 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -27,15 +27,17 @@ #ifndef SERVCONF_H #define SERVCONF_H -#include +#include "netunit.h" + +#include "stg/parser_auth_by.h" +#include "stg/os_int.h" +#include "stg/const.h" #include #include #include -#include "stg/os_int.h" -#include "stg/const.h" -#include "netunit.h" +#include void Start(void *data, const char *el, const char **attr); void End(void *data, const char *el); @@ -102,22 +104,12 @@ typedef void(*RecvServerInfoDataCb_t)(SERVERINFO * si, void * data); typedef int(*RecvChgUserCb_t)(const char * asnwer, void * data); typedef int(*RecvCheckUserCb_t)(const char * answer, void * data); typedef int(*RecvSendMessageCb_t)(const char * answer, void * data); -typedef void(*RecvAuthByDataCb_t)(const std::vector & list, void * data); //----------------------------------------------------------------------------- struct ADMINDATA { char login[ADM_LOGIN_LEN]; }; //----------------------------------------------------------------------------- -class PARSER -{ -public: - PARSER() {} - virtual ~PARSER() {} - virtual int ParseStart(const char *el, const char **attr) = 0; - virtual void ParseEnd(const char *el) = 0; -}; -//----------------------------------------------------------------------------- class PARSER_CHG_USER: public PARSER { public: @@ -226,23 +218,6 @@ private: bool error; }; //----------------------------------------------------------------------------- -class PARSER_AUTH_BY: public PARSER -{ -public: - PARSER_AUTH_BY(); - int ParseStart(const char *el, const char **attr); - void ParseEnd(const char *el); - void ParseServerInfo(const char *el, const char **attr); - bool GetError(); - void SetRecvCb(RecvAuthByDataCb_t, void * data); -private: - RecvAuthByDataCb_t RecvAuthByDataCb; - void * authByDataCb; - int depth; - bool error; - std::vector list; -}; -//----------------------------------------------------------------------------- class SERVCONF { public: @@ -254,7 +229,7 @@ public: void SetAdmPassword(const char * password); void SetUserDataRecvCb(RecvUserDataCb_t, void * data); - void SetGetUserAuthByRecvCb(RecvAuthByDataCb_t, void * data); + void SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data); void SetServerInfoRecvCb(RecvServerInfoDataCb_t, void * data); void SetChgUserCb(RecvChgUserCb_t, void * data); void SetCheckUserCb(RecvCheckUserCb_t, void * data); @@ -264,7 +239,7 @@ public: int GetUsers(); int GetUser(const char * login); int ChgUser(const char * request); - int GetUserAuthBy(const char * login); + int AuthBy(const char * login); // TODO: Remove this shit! int MsgUser(const char * request); int SendMessage(const char * login, const char * message, int prio); @@ -296,14 +271,14 @@ private: RecvUserDataCb_t RecvUserDataCb; RecvUserDataCb_t RecvGetUserDataCb; - RecvAuthByDataCb_t RecvAuthByCb; + PARSER_AUTH_BY::CALLBACK authByCallback; RecvServerInfoDataCb_t RecvServerInfoDataCb; RecvChgUserCb_t RecvChgUserCb; RecvCheckUserCb_t RecvCheckUserCb; RecvSendMessageCb_t RecvSendMessageCb; void * getUserDataDataCb; - void * getUserAuthByDataCb; + void * authByData; void * getUsersDataDataCb; void * getServerInfoDataCb; void * chgUserDataCb; diff --git a/stglibs/srvconf.lib/parser.cpp b/stglibs/srvconf.lib/parser.cpp index 284e1127..a87266eb 100644 --- a/stglibs/srvconf.lib/parser.cpp +++ b/stglibs/srvconf.lib/parser.cpp @@ -822,58 +822,6 @@ if (strcmp(attr[0], "value") == 0) //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -PARSER_AUTH_BY::PARSER_AUTH_BY() - : RecvAuthByDataCb(NULL), - authByDataCb(NULL), - depth(0), - error(false), - list() -{ -} -//----------------------------------------------------------------------------- -int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr) -{ -depth++; -if (depth == 1) - { - if (strcasecmp(el, "AuthorizedBy") != 0) - { - list.erase(list.begin(), list.end()); - //printf("%s\n", el); - } - } -else - { - if (depth == 2) - { - if (strcasecmp(el, "Auth") == 0) - { - if (attr && attr[0] && attr[1]) - list.push_back(attr[1]); - return 0; - } - } - } -return 0; -} -//----------------------------------------------------------------------------- -void PARSER_AUTH_BY::ParseEnd(const char *) -{ -depth--; -if (depth == 0) - { - RecvAuthByDataCb(list, authByDataCb); - } -} -//----------------------------------------------------------------------------- -void PARSER_AUTH_BY::SetRecvCb(RecvAuthByDataCb_t f, void * data) -{ -RecvAuthByDataCb = f; -authByDataCb = data; -} -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- PARSER_CHG_USER::PARSER_CHG_USER() : RecvChgUserCb(NULL), chgUserCbData(NULL), diff --git a/stglibs/srvconf.lib/parser_auth_by.cpp b/stglibs/srvconf.lib/parser_auth_by.cpp new file mode 100644 index 00000000..63c23a09 --- /dev/null +++ b/stglibs/srvconf.lib/parser_auth_by.cpp @@ -0,0 +1,46 @@ +#include "stg/parser_auth_by.h" + +#include // strcasecmp + +PARSER_AUTH_BY::PARSER_AUTH_BY() + : callback(NULL), + data(NULL), + depth(0) +{ +} +//----------------------------------------------------------------------------- +int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr) +{ +depth++; +if (depth == 1) + { + if (strcasecmp(el, "AuthorizedBy") != 0) + info.clear(); + } +else + { + if (depth == 2) + { + if (strcasecmp(el, "Auth") == 0) + { + if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "name") == 0) + info.push_back(attr[1]); + return 0; + } + } + } +return 0; +} +//----------------------------------------------------------------------------- +void PARSER_AUTH_BY::ParseEnd(const char * /*el*/) +{ +depth--; +if (depth == 0) + callback(info, data); +} +//----------------------------------------------------------------------------- +void PARSER_AUTH_BY::SetCallback(CALLBACK f, void * data) +{ +callback = f; +data = data; +} diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 0ca3028c..d7df4704 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -96,12 +96,13 @@ SERVCONF::SERVCONF() error(0), RecvUserDataCb(NULL), RecvGetUserDataCb(NULL), + authByCallback(NULL), RecvServerInfoDataCb(NULL), RecvChgUserCb(NULL), RecvCheckUserCb(NULL), RecvSendMessageCb(NULL), getUserDataDataCb(NULL), - getUserAuthByDataCb(NULL), + authByData(NULL), getUsersDataDataCb(NULL), getServerInfoDataCb(NULL), chgUserDataCb(NULL), @@ -162,18 +163,18 @@ if ((ret = nt.Disconnect()) != st_ok) return st_ok; } //----------------------------------------------------------------------------- -int SERVCONF::GetUserAuthBy(const char * l) +int SERVCONF::AuthBy(const char * l) { char request[255]; snprintf(request, 255, "", l); -int ret; currParser = &parserAuthBy; -((PARSER_AUTH_BY*)currParser)->SetRecvCb(RecvAuthByCb, getUserAuthByDataCb); +((PARSER_AUTH_BY*)currParser)->SetCallback(authByCallback, authByData); nt.Reset(); nt.SetRxCallback(this, AnsRecv); +int ret; if ((ret = nt.Connect()) != st_ok) { errorMsg = nt.GetError(); @@ -400,10 +401,10 @@ RecvGetUserDataCb = f; //GET_USER getUserDataDataCb = data; } //----------------------------------------------------------------------------- -void SERVCONF::SetGetUserAuthByRecvCb(RecvAuthByDataCb_t f, void * data) +void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data) { -RecvAuthByCb = f; -getUserAuthByDataCb = data; +authByCallback = f; +authByData = data; } //----------------------------------------------------------------------------- void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data) -- 2.44.2