From 0093be0eb4dbe201a7d63da1de47f98c68987b1d Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sun, 15 Sep 2013 04:27:32 +0300 Subject: [PATCH] [NY] Simplified SERVCONF interface. --- projects/sgconf/common_sg.cpp | 13 +-- stglibs/srvconf.lib/include/stg/servconf.h | 26 +++--- stglibs/srvconf.lib/servconf.cpp | 92 ++++++---------------- 3 files changed, 37 insertions(+), 94 deletions(-) diff --git a/projects/sgconf/common_sg.cpp b/projects/sgconf/common_sg.cpp index 48eaac35..06fd9823 100644 --- a/projects/sgconf/common_sg.cpp +++ b/projects/sgconf/common_sg.cpp @@ -447,8 +447,7 @@ bool ProcessSetUser(const std::string & server, SERVCONF sc(server, port, login, password); ResultData data; -sc.SetChgUserCallback(RecvSetUserAnswer, &data); -int res = sc.ChgUser(str.c_str()); +int res = sc.ChgUser(str.c_str(), RecvSetUserAnswer, &data); if (res == st_ok && data.result) { @@ -471,8 +470,7 @@ bool ProcessSendMessage(const std::string & server, uint16_t port, SERVCONF sc(server, port, login, password); ResultData data; -sc.SetSendMessageCallback(SendMessageCallback, &data); -int res = sc.SendMessage(requestString.c_str()); +int res = sc.SendMessage(requestString.c_str(), SendMessageCallback, &data); if (res == st_ok && data.result) { @@ -498,9 +496,7 @@ bool ProcessGetUser(const std::string &server, SERVCONF sc(server, port, admLogin, admPasswd); GetUserData data(request, false); - -sc.SetGetUserCallback(GetUserCallback, &data); -bool res = (sc.GetUser(login.c_str()) == st_ok); +bool res = (sc.GetUser(login.c_str(), GetUserCallback, &data) == st_ok); if (res && data.result) { @@ -525,8 +521,7 @@ bool ProcessAuthBy(const std::string &server, SERVCONF sc(server, port, admLogin, admPasswd); ResultData data; -sc.SetAuthByCallback(RecvAuthByData, &data); -bool res = (sc.AuthBy(login.c_str()) == st_ok); +bool res = (sc.AuthBy(login.c_str(), RecvAuthByData, &data) == st_ok); if (res && data.result) { diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index 23a73c7b..094524e9 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -60,24 +60,16 @@ public: SERVCONF(const std::string & server, uint16_t port, const std::string & login, const std::string & password); - void SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data); - void SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data); - void SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data); - 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 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); - int SendMessage(const char * request); - int ServerInfo(); - int CheckUser(const char * login, const char * 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; - int Start(const char * el, const char ** attr); + void Start(const char * el, const char ** attr); void End(const char * el); private: @@ -96,7 +88,7 @@ private: std::string errorMsg; XML_Parser parser; - int Exec(const char * request, PARSER & cp); + int Exec(const std::string & request, PARSER & cp); friend bool AnsRecv(void * data, const std::string & chunk, bool final); }; diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 621cced0..268749a3 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -30,14 +30,12 @@ namespace void ElementStart(void * data, const char * el, const char ** attr) { -SERVCONF * sc = static_cast(data); -sc->Start(el, attr); +static_cast(data)->Start(el, attr); } void ElementEnd(void * data, const char * el) { -SERVCONF * sc = static_cast(data); -sc->End(el); +static_cast(data)->End(el); } } // namespace anonymous @@ -68,58 +66,51 @@ parser = XML_ParserCreate(NULL); nt.SetRxCallback(this, AnsRecv); } //----------------------------------------------------------------------------- -int SERVCONF::GetUser(const char * l) +int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data) { -char request[255]; -snprintf(request, 255, "", l); - -return Exec(request, parserGetUser); +parserGetUser.SetCallback(f, data); +return Exec("", parserGetUser); } //----------------------------------------------------------------------------- -int SERVCONF::AuthBy(const char * l) +int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data) { -char request[255]; -snprintf(request, 255, "", l); - -return Exec(request, parserAuthBy); +parserAuthBy.SetCallback(f, data); +return Exec("", parserAuthBy); } //----------------------------------------------------------------------------- -int SERVCONF::GetUsers() +int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data) { -char request[] = ""; - -return Exec(request, parserGetUsers); +parserGetUsers.SetCallback(f, data); +return Exec("", parserGetUsers); } //----------------------------------------------------------------------------- -int SERVCONF::ServerInfo() +int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data) { -char request[] = ""; - -return Exec(request, parserServerInfo); +parserServerInfo.SetCallback(f, data); +return Exec("", parserServerInfo); } //----------------------------------------------------------------------------- -int SERVCONF::ChgUser(const char * request) +int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data) { +parserChgUser.SetCallback(f, data); return Exec(request, parserChgUser); } //----------------------------------------------------------------------------- -int SERVCONF::SendMessage(const char * request) +int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data) { +parserSendMessage.SetCallback(f, data); return Exec(request, parserSendMessage); } //----------------------------------------------------------------------------- -int SERVCONF::CheckUser(const char * login, const char * password) +int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data) { -char request[255]; -snprintf(request, 255, "", login, password); - -return Exec(request, parserCheckUser); +parserCheckUser.SetCallback(f, data); +return Exec("", parserCheckUser); } //----------------------------------------------------------------------------- -int SERVCONF::Start(const char * el, const char ** attr) +void SERVCONF::Start(const char * el, const char ** attr) { currParser->ParseStart(el, attr); -return 0; } //----------------------------------------------------------------------------- void SERVCONF::End(const char * el) @@ -127,47 +118,12 @@ void SERVCONF::End(const char * el) currParser->ParseEnd(el); } //----------------------------------------------------------------------------- -void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data) -{ -parserGetUsers.SetCallback(f, data); -} -//----------------------------------------------------------------------------- -void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data) -{ -parserGetUser.SetCallback(f, data); -} -//----------------------------------------------------------------------------- -void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data) -{ -parserAuthBy.SetCallback(f, data); -} -//----------------------------------------------------------------------------- -void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data) -{ -parserServerInfo.SetCallback(f, data); -} -//----------------------------------------------------------------------------- -void SERVCONF::SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data) -{ -parserChgUser.SetCallback(f, data); -} -//----------------------------------------------------------------------------- -void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data) -{ -parserCheckUser.SetCallback(f, data); -} -//----------------------------------------------------------------------------- -void SERVCONF::SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data) -{ -parserSendMessage.SetCallback(f, data); -} -//----------------------------------------------------------------------------- const std::string & SERVCONF::GetStrError() const { return errorMsg; } //----------------------------------------------------------------------------- -int SERVCONF::Exec(const char * request, PARSER & cp) +int SERVCONF::Exec(const std::string & request, PARSER & cp) { currParser = &cp; @@ -181,7 +137,7 @@ if ((ret = nt.Connect()) != st_ok) errorMsg = nt.GetError(); return ret; } -if ((ret = nt.Transact(request)) != st_ok) +if ((ret = nt.Transact(request.c_str())) != st_ok) { errorMsg = nt.GetError(); return ret; -- 2.44.2