From 8b2d407ef1f52d42ca30d8f5e23c7a4277a2acd5 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sun, 18 Aug 2013 12:45:56 +0300 Subject: [PATCH 1/1] Send message parser was moved to a separate files. --- stglibs/srvconf.lib/Makefile | 1 + .../include/stg/parser_send_message.h | 44 ++++++++++++++ stglibs/srvconf.lib/include/stg/servconf.h | 29 ++------- stglibs/srvconf.lib/parser.cpp | 40 ------------- stglibs/srvconf.lib/parser_send_message.cpp | 60 +++++++++++++++++++ stglibs/srvconf.lib/servconf.cpp | 27 ++------- 6 files changed, 114 insertions(+), 87 deletions(-) create mode 100644 stglibs/srvconf.lib/include/stg/parser_send_message.h create mode 100644 stglibs/srvconf.lib/parser_send_message.cpp diff --git a/stglibs/srvconf.lib/Makefile b/stglibs/srvconf.lib/Makefile index 45f3899d..296ae0f3 100644 --- a/stglibs/srvconf.lib/Makefile +++ b/stglibs/srvconf.lib/Makefile @@ -16,6 +16,7 @@ SRCS = netunit.cpp \ parser_get_user.cpp \ parser_get_users.cpp \ parser_chg_user.cpp \ + parser_send_message.cpp \ servconf.cpp INCS = servconf.h \ diff --git a/stglibs/srvconf.lib/include/stg/parser_send_message.h b/stglibs/srvconf.lib/include/stg/parser_send_message.h new file mode 100644 index 00000000..d14153e0 --- /dev/null +++ b/stglibs/srvconf.lib/include/stg/parser_send_message.h @@ -0,0 +1,44 @@ +/* + * 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 : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#ifndef __STG_STGLIBS_SRVCONF_PARSER_SEND_MESSAGE_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_SEND_MESSAGE_H__ + +#include "parser.h" + +class PARSER_SEND_MESSAGE: public PARSER +{ +public: + typedef int (* CALLBACK)(const char * answer, void * data); + + PARSER_SEND_MESSAGE(); + 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; + + void ParseAnswer(const char * el, const char ** attr); +}; + +#endif diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index c62161be..b422f708 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -35,6 +35,7 @@ #include "stg/parser_get_user.h" #include "stg/parser_get_users.h" #include "stg/parser_chg_user.h" +#include "stg/parser_send_message.h" #include "stg/os_int.h" #include "stg/const.h" @@ -50,28 +51,12 @@ void End(void * data, const char * el); #define MAX_ERR_STR_LEN (64) #define IP_STRING_LEN (255) -//----------------------------------------------------------------------------- -typedef int (* RecvSendMessageCb_t)(const char * answer, void * data); -//----------------------------------------------------------------------------- + struct ADMINDATA { char login[ADM_LOGIN_LEN]; }; //----------------------------------------------------------------------------- -class PARSER_SEND_MESSAGE: public PARSER -{ -public: - PARSER_SEND_MESSAGE(); - int ParseStart(const char * el, const char ** attr); - void ParseEnd(const char * el); - void ParseAnswer(const char * el, const char ** attr); - void SetSendMessageRecvCb(RecvSendMessageCb_t, void * data); -private: - RecvSendMessageCb_t RecvSendMessageCb; - void * sendMessageCbData; - int depth; -}; -//----------------------------------------------------------------------------- class SERVCONF { public: @@ -88,15 +73,13 @@ public: void SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data); void SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data); void SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data); - void SetSendMessageCb(RecvSendMessageCb_t, void * data); + void SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data); int GetUsers(); int GetUser(const char * login); int ChgUser(const char * request); int AuthBy(const char * login); - // TODO: Remove this shit! - int MsgUser(const char * request); - int SendMessage(const char * login, const char * message, int prio); + int SendMessage(const char * request); int ServerInfo(); int CheckUser(const char * login, const char * password); @@ -120,10 +103,6 @@ private: std::string errorMsg; XML_Parser parser; - RecvSendMessageCb_t RecvSendMessageCb; - - void * sendMessageDataCb; - int Exec(const char * request); friend int AnsRecv(void * data, std::list * list); diff --git a/stglibs/srvconf.lib/parser.cpp b/stglibs/srvconf.lib/parser.cpp index 78247f48..2ebe9a91 100644 --- a/stglibs/srvconf.lib/parser.cpp +++ b/stglibs/srvconf.lib/parser.cpp @@ -39,43 +39,3 @@ using namespace std; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -PARSER_SEND_MESSAGE::PARSER_SEND_MESSAGE() - : RecvSendMessageCb(NULL), - sendMessageCbData(NULL), - depth(0) -{ -} -//----------------------------------------------------------------------------- -int PARSER_SEND_MESSAGE::ParseStart(const char *el, const char **attr) -{ -depth++; -if (depth == 1) - { - if (strcasecmp(el, "SendMessageResult") == 0) - { - ParseAnswer(el, attr); - } - } -return 0; -} -//----------------------------------------------------------------------------- -void PARSER_SEND_MESSAGE::ParseEnd(const char *) -{ -depth--; -} -//----------------------------------------------------------------------------- -void PARSER_SEND_MESSAGE::ParseAnswer(const char *, const char **attr) -{ -if (RecvSendMessageCb) - RecvSendMessageCb(attr[1], sendMessageCbData); -} -//----------------------------------------------------------------------------- -void PARSER_SEND_MESSAGE::SetSendMessageRecvCb(RecvSendMessageCb_t f, void * data) -{ -RecvSendMessageCb = f; -sendMessageCbData = data; -} -//----------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/parser_send_message.cpp b/stglibs/srvconf.lib/parser_send_message.cpp new file mode 100644 index 00000000..77aba248 --- /dev/null +++ b/stglibs/srvconf.lib/parser_send_message.cpp @@ -0,0 +1,60 @@ +/* + * 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 : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "stg/parser_send_message.h" + +#include + +#include + +PARSER_SEND_MESSAGE::PARSER_SEND_MESSAGE() + : callback(NULL), + data(NULL), + depth(0) +{ +} +//----------------------------------------------------------------------------- +int PARSER_SEND_MESSAGE::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1) + if (strcasecmp(el, "SendMessageResult") == 0) + ParseAnswer(el, attr); +return 0; +} +//----------------------------------------------------------------------------- +void PARSER_SEND_MESSAGE::ParseEnd(const char * /*el*/) +{ +depth--; +} +//----------------------------------------------------------------------------- +void PARSER_SEND_MESSAGE::ParseAnswer(const char * /*el*/, const char **attr) +{ +if (attr && attr[0] && attr[1]) + if (callback) + callback(attr[1], data); +} +//----------------------------------------------------------------------------- +void PARSER_SEND_MESSAGE::SetCallback(CALLBACK f, void * d) +{ +callback = f; +data = d; +} diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index b8507098..d48424e0 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -139,19 +139,6 @@ char request[] = ""; currParser = &parserGetUsers; -return Exec(request); -} -//----------------------------------------------------------------------------- -int SERVCONF::SendMessage(const char * login, const char * message, int prio) -{ -char request[1000]; -char msg[500]; -Encode12(msg, message, strlen(message)); -snprintf(request, 1000, "", login, prio, msg); - -currParser = &parserSendMessage; -parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb); - return Exec(request); } //----------------------------------------------------------------------------- @@ -171,12 +158,9 @@ currParser = &parserChgUser; return Exec(request); } //----------------------------------------------------------------------------- -// TODO: remove this shit! -//----------------------------------------------------------------------------- -int SERVCONF::MsgUser(const char * request) +int SERVCONF::SendMessage(const char * request) { currParser = &parserSendMessage; -parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb); return Exec(request); } @@ -191,13 +175,13 @@ currParser = &parserCheckUser; return Exec(request); } //----------------------------------------------------------------------------- -int SERVCONF::Start(const char *el, const char **attr) +int SERVCONF::Start(const char * el, const char ** attr) { currParser->ParseStart(el, attr); return 0; } //----------------------------------------------------------------------------- -void SERVCONF::End(const char *el) +void SERVCONF::End(const char * el) { currParser->ParseEnd(el); } @@ -232,10 +216,9 @@ void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data) parserCheckUser.SetCallback(f, data); } //----------------------------------------------------------------------------- -void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data) +void SERVCONF::SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data) { -RecvSendMessageCb = f; -sendMessageDataCb = data; +parserSendMessage.SetCallback(f, data); } //----------------------------------------------------------------------------- const std::string & SERVCONF::GetStrError() const -- 2.44.2