From: Maxim Mamontov Date: Fri, 23 Aug 2013 15:59:53 +0000 (+0300) Subject: Improved handling errors got from server. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/85f8f0b545228f3cffec62d8e2251e756a2fac15 Improved handling errors got from server. --- diff --git a/projects/sgconf/common_sg.cpp b/projects/sgconf/common_sg.cpp index 898c334f..a54826df 100644 --- a/projects/sgconf/common_sg.cpp +++ b/projects/sgconf/common_sg.cpp @@ -49,11 +49,18 @@ const int usageInfo = 1; const int TO_KOI8 = 0; const int FROM_KOI8 = 1; //----------------------------------------------------------------------------- +struct ResultData +{ + bool result; + std::string reason; +}; +//----------------------------------------------------------------------------- struct GetUserData { GetUserData(REQUEST & req, bool res) : request(req), result(res) {} REQUEST & request; bool result; + std::string reason; }; //--------------------------------------------------------------------------- struct HelpParams @@ -287,15 +294,34 @@ void ConvertFromKOI8(const std::string & src, std::string * dst) ConvertKOI8(src, dst, FROM_KOI8); } //----------------------------------------------------------------------------- -int RecvSetUserAnswer(const char * ans, void * d) +void SendMessageCallback(bool result, const std::string & reason, void * d) { -GetUserData * data = static_cast(d); +ResultData * data = static_cast(d); +data->result = result; +data->reason = reason; +} +//----------------------------------------------------------------------------- +void RecvSetUserAnswer(bool result, const std::string & reason, void * d) +{ +ResultData * data = static_cast(d); +data->result = result; +data->reason = reason; +} +//----------------------------------------------------------------------------- +void RecvAuthByData(bool result, const std::string & reason, + const PARSER_AUTH_BY::INFO & list, void * d) +{ +ResultData * data = static_cast(d); +data->result = result; +data->reason = reason; -std::cout << ans << std::endl; +if (!result) + return; -data->result = (strcasecmp("Ok", ans) == 0); +for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + std::cout << *it << "\n"; -return 0; +std::cout << std::endl; } //----------------------------------------------------------------------------- struct StringReqParams @@ -305,13 +331,19 @@ struct StringReqParams const std::string * value; }; //----------------------------------------------------------------------------- -void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d) +void GetUserCallback(bool result, const std::string& reason, const PARSER_GET_USER::INFO & info, void * d) { GetUserData * data = static_cast(d); +data->result = false; +data->reason = reason; + +if (!result) + return; if (info.login == "") { data->result = false; + data->reason = "Invalid login."; return; } @@ -406,19 +438,11 @@ for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++) data->result = true; } //----------------------------------------------------------------------------- -void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *) -{ -for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) - cout << *it << "\n"; -cout << endl; -} -//----------------------------------------------------------------------------- -int ProcessSetUser(const std::string &server, - int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &str, - bool isMessage) +bool ProcessSetUser(const std::string & server, + int port, + const std::string & admLogin, + const std::string & admPasswd, + const std::string & str) { SERVCONF sc; @@ -427,43 +451,59 @@ sc.SetPort(port); sc.SetAdmLogin(admLogin.c_str()); sc.SetAdmPassword(admPasswd.c_str()); -REQUEST request; -GetUserData cbdata(request, false); +ResultData data; +sc.SetChgUserCallback(RecvSetUserAnswer, &data); +int res = sc.ChgUser(str.c_str()); -int res = 0; -if (isMessage) +if (res == st_ok && data.result) { - sc.SetSendMessageCallback(RecvSetUserAnswer, &cbdata); - res = sc.SendMessage(str.c_str()); + printf("Ok\n"); + return false; } + +printf("Error\n"); +if (res != st_ok) + printf("%s\n", sc.GetStrError().c_str()); else - { - sc.SetChgUserCallback(RecvSetUserAnswer, &cbdata); - res = sc.ChgUser(str.c_str()); - } + printf("%s\n", data.reason.c_str()); +return true; +} +//----------------------------------------------------------------------------- +bool ProcessSendMessage(const std::string & server, uint16_t port, + const std::string & login, const std::string & password, + const std::string & requestString) +{ +SERVCONF sc; + +sc.SetServer(server.c_str()); +sc.SetPort(port); +sc.SetAdmLogin(login.c_str()); +sc.SetAdmPassword(password.c_str()); + +ResultData data; +sc.SetSendMessageCallback(SendMessageCallback, &data); +int res = sc.SendMessage(requestString.c_str()); -if (res == st_ok && cbdata.result) +if (res == st_ok && data.result) { printf("Ok\n"); - return 0; - } -else - { - printf("Error\n"); - if (res != st_ok) - printf("%s\n", sc.GetStrError().c_str()); - return -1; + return true; } -return 0; +printf("Error\n"); +if (res != st_ok) + printf("%s\n", sc.GetStrError().c_str()); +else + printf("%s\n", data.reason.c_str()); +return false; } //----------------------------------------------------------------------------- -int ProcessGetUser(const std::string &server, - int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &login, - REQUEST & request) +bool ProcessGetUser(const std::string &server, + int port, + const std::string &admLogin, + const std::string &admPasswd, + const std::string &login, + REQUEST & request) { SERVCONF sc; @@ -480,22 +520,22 @@ bool res = (sc.GetUser(login.c_str()) == st_ok); if (res && data.result) { printf("Ok\n"); - return 0; - } -else - { - printf("Error\n"); - return -1; + return true; } -return 0; +printf("Error\n"); +if (!res) + printf("%s\n", sc.GetStrError().c_str()); +else + printf("%s\n", data.reason.c_str()); +return false; } //----------------------------------------------------------------------------- -int ProcessAuthBy(const std::string &server, - int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &login) +bool ProcessAuthBy(const std::string &server, + int port, + const std::string &admLogin, + const std::string &admPasswd, + const std::string &login) { SERVCONF sc; @@ -504,16 +544,21 @@ sc.SetPort(port); sc.SetAdmLogin(admLogin.c_str()); sc.SetAdmPassword(admPasswd.c_str()); -sc.SetAuthByCallback(RecvAuthByData, NULL); +ResultData data; +sc.SetAuthByCallback(RecvAuthByData, &data); bool res = (sc.AuthBy(login.c_str()) == st_ok); -if (!res) +if (res && data.result) { - printf("Error\n"); - return -1; + printf("Ok\n"); + return true; } -printf("Ok\n"); -return 0; +printf("Error\n"); +if (!res) + printf("%s\n", sc.GetStrError().c_str()); +else + printf("%s\n", data.reason.c_str()); +return false; } //----------------------------------------------------------------------------- diff --git a/projects/sgconf/common_sg.h b/projects/sgconf/common_sg.h index 03090a76..4bff0aa7 100644 --- a/projects/sgconf/common_sg.h +++ b/projects/sgconf/common_sg.h @@ -45,24 +45,27 @@ int CheckLogin(const char * login); void ConvertFromKOI8(const std::string & src, std::string * dst); void ConvertToKOI8(const std::string & src, std::string * dst); -int ProcessGetUser(const std::string &server, +bool ProcessGetUser(const std::string & server, + int port, + const std::string & admLogin, + const std::string & admPasswd, + const std::string & login, + REQUEST & request); + +bool ProcessAuthBy(const std::string & server, int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &login, - REQUEST & request); + const std::string & admLogin, + const std::string & admPasswd, + const std::string & login); -int ProcessAuthBy(const std::string &server, - int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &login); +bool ProcessSetUser(const std::string & server, + int port, + const std::string & admLogin, + const std::string & admPasswd, + const std::string & str); -int ProcessSetUser(const std::string &server, - int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &str, - bool isMessage = false); +bool ProcessSendMessage(const std::string & server, uint16_t port, + const std::string & login, const std::string & password, + const std::string & requestString); #endif diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index f8a394d4..66f580f6 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -724,7 +724,7 @@ int CheckParametersSet(REQUEST * req) return CheckParameters(req); } //----------------------------------------------------------------------------- -int mainGet(int argc, char **argv) +bool mainGet(int argc, char **argv) { int c; REQUEST req; @@ -890,7 +890,7 @@ else return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, req); } //----------------------------------------------------------------------------- -int mainSet(int argc, char **argv) +bool mainSet(int argc, char **argv) { string str; @@ -1087,7 +1087,10 @@ char rstr[rLen]; memset(rstr, 0, rLen); CreateRequestSet(&req, rstr); -return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr, isMessage); +if (isMessage) + return ProcessSendMessage(req.server, req.port, req.admLogin, req.admPasswd, rstr); + +return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr); } //----------------------------------------------------------------------------- int main(int argc, char **argv) @@ -1106,7 +1109,9 @@ if (strcmp(argv[1], "get") == 0) else if (strcmp(argv[1], "set") == 0) { //printf("set\n"); - return mainSet(argc - 1, argv + 1); + if (mainSet(argc - 1, argv + 1) ) + return 0; + return -1; } else { diff --git a/stglibs/srvconf.lib/include/stg/parser_auth_by.h b/stglibs/srvconf.lib/include/stg/parser_auth_by.h index aa3ea7bc..00c997d5 100644 --- a/stglibs/srvconf.lib/include/stg/parser_auth_by.h +++ b/stglibs/srvconf.lib/include/stg/parser_auth_by.h @@ -30,7 +30,7 @@ class PARSER_AUTH_BY: public PARSER { public: typedef std::vector INFO; - typedef void (* CALLBACK)(const INFO & info, void * data); + typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); PARSER_AUTH_BY(); int ParseStart(const char *el, const char **attr); @@ -40,7 +40,9 @@ private: CALLBACK callback; void * data; int depth; + bool parsingAnswer; INFO info; + std::string error; }; #endif diff --git a/stglibs/srvconf.lib/include/stg/parser_chg_user.h b/stglibs/srvconf.lib/include/stg/parser_chg_user.h index db54026a..cc6e9f18 100644 --- a/stglibs/srvconf.lib/include/stg/parser_chg_user.h +++ b/stglibs/srvconf.lib/include/stg/parser_chg_user.h @@ -29,7 +29,7 @@ class PARSER_CHG_USER: public PARSER { public: - typedef int (* CALLBACK)(bool result, const std::string& reason, void * data); + typedef void (* CALLBACK)(bool result, const std::string& reason, void * data); PARSER_CHG_USER(); int ParseStart(const char * el, const char ** attr); diff --git a/stglibs/srvconf.lib/include/stg/parser_get_user.h b/stglibs/srvconf.lib/include/stg/parser_get_user.h index a8700ad0..7f6faaad 100644 --- a/stglibs/srvconf.lib/include/stg/parser_get_user.h +++ b/stglibs/srvconf.lib/include/stg/parser_get_user.h @@ -85,6 +85,7 @@ private: void * data; INFO info; int depth; + bool parsingAnswer; std::string error; void ParseUser(const char *el, const char **attr); diff --git a/stglibs/srvconf.lib/include/stg/parser_get_users.h b/stglibs/srvconf.lib/include/stg/parser_get_users.h index 57907026..35fcd126 100644 --- a/stglibs/srvconf.lib/include/stg/parser_get_users.h +++ b/stglibs/srvconf.lib/include/stg/parser_get_users.h @@ -45,13 +45,13 @@ private: PARSER_GET_USER userParser; INFO info; int depth; + bool parsingAnswer; std::string error; void AddUser(const PARSER_GET_USER::INFO & userInfo); void SetError(const std::string & e) { error = e; } - void ParseUsers(const char * el, const char ** attr); - static void UserCallback(const PARSER_GET_USER::INFO & info, void * data); + static void UserCallback(bool result, const std::string& reason, const PARSER_GET_USER::INFO & info, void * data); }; #endif diff --git a/stglibs/srvconf.lib/include/stg/parser_send_message.h b/stglibs/srvconf.lib/include/stg/parser_send_message.h index 9d1f33dc..9b3ca5ef 100644 --- a/stglibs/srvconf.lib/include/stg/parser_send_message.h +++ b/stglibs/srvconf.lib/include/stg/parser_send_message.h @@ -29,7 +29,7 @@ class PARSER_SEND_MESSAGE: public PARSER { public: - typedef int (* CALLBACK)(bool result, const std::string& reason, void * data); + typedef void (* CALLBACK)(bool result, const std::string& reason, void * data); PARSER_SEND_MESSAGE(); int ParseStart(const char * el, const char ** attr); diff --git a/stglibs/srvconf.lib/include/stg/parser_server_info.h b/stglibs/srvconf.lib/include/stg/parser_server_info.h index 33ff5b66..ff31ae71 100644 --- a/stglibs/srvconf.lib/include/stg/parser_server_info.h +++ b/stglibs/srvconf.lib/include/stg/parser_server_info.h @@ -53,6 +53,7 @@ private: CALLBACK callback; void * data; int depth; + bool parsingAnswer; INFO info; std::string error; }; diff --git a/stglibs/srvconf.lib/include/stg/property_parsers.h b/stglibs/srvconf.lib/include/stg/property_parsers.h index cb3943db..d1e6a893 100644 --- a/stglibs/srvconf.lib/include/stg/property_parsers.h +++ b/stglibs/srvconf.lib/include/stg/property_parsers.h @@ -50,6 +50,7 @@ typedef std::map PROPERTY_PARSERS; bool CheckValue(const char ** attr); template +inline bool GetValue(const char ** attr, T & value) { if (CheckValue(attr)) @@ -59,6 +60,7 @@ return true; } template <> +inline bool GetValue(const char ** attr, std::string & value) { if (!CheckValue(attr)) @@ -68,6 +70,7 @@ return true; } template <> +inline bool GetValue(const char ** attr, double & value) { if (CheckValue(attr)) @@ -84,6 +87,7 @@ template void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue); template +inline void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func) { parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); diff --git a/stglibs/srvconf.lib/parser_auth_by.cpp b/stglibs/srvconf.lib/parser_auth_by.cpp index 877ca3e4..06981aae 100644 --- a/stglibs/srvconf.lib/parser_auth_by.cpp +++ b/stglibs/srvconf.lib/parser_auth_by.cpp @@ -27,7 +27,8 @@ PARSER_AUTH_BY::PARSER_AUTH_BY() : callback(NULL), data(NULL), - depth(0) + depth(0), + parsingAnswer(false) { } //----------------------------------------------------------------------------- @@ -36,14 +37,25 @@ int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr) depth++; if (depth == 1) { - if (strcasecmp(el, "AuthorizedBy") != 0) - info.clear(); + if (strcasecmp(el, "AuthorizedBy") == 0) + if (attr && attr[0] && attr[1]) + { + if (strcasecmp(attr[1], "error") == 0) + { + if (attr[2] && attr[3]) + error = attr[3]; + else + error = "User not found."; + } + else + parsingAnswer = true; + } } else { if (depth == 2) { - if (strcasecmp(el, "Auth") == 0) + if (parsingAnswer && strcasecmp(el, "Auth") == 0) { if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "name") == 0) info.push_back(attr[1]); @@ -57,8 +69,13 @@ return 0; void PARSER_AUTH_BY::ParseEnd(const char * /*el*/) { depth--; -if (depth == 0 && callback) - callback(info, data); +if (depth == 0) + { + if (callback) + callback(error.empty(), error, info, data); + info.clear(); + error.clear(); + } } //----------------------------------------------------------------------------- void PARSER_AUTH_BY::SetCallback(CALLBACK f, void * d) diff --git a/stglibs/srvconf.lib/parser_check_user.cpp b/stglibs/srvconf.lib/parser_check_user.cpp index b99a4390..eed03f30 100644 --- a/stglibs/srvconf.lib/parser_check_user.cpp +++ b/stglibs/srvconf.lib/parser_check_user.cpp @@ -32,7 +32,7 @@ PARSER_CHECK_USER::PARSER_CHECK_USER() { } //----------------------------------------------------------------------------- -int PARSER_CHECK_USER::ParseStart(const char *el, const char **attr) +int PARSER_CHECK_USER::ParseStart(const char * el, const char ** attr) { depth++; if (depth == 1) diff --git a/stglibs/srvconf.lib/parser_get_user.cpp b/stglibs/srvconf.lib/parser_get_user.cpp index 258d531d..a684a3f7 100644 --- a/stglibs/srvconf.lib/parser_get_user.cpp +++ b/stglibs/srvconf.lib/parser_get_user.cpp @@ -57,7 +57,8 @@ return true; PARSER_GET_USER::PARSER_GET_USER() : callback(NULL), data(NULL), - depth(0) + depth(0), + parsingAnswer(false) { AddParser(propertyParsers, "login", info.login); AddParser(propertyParsers, "password", info.password); @@ -99,7 +100,7 @@ depth++; if (depth == 1) ParseUser(el, attr); -if (depth == 2) +if (depth == 2 && parsingAnswer) ParseUserParams(el, attr); return 0; @@ -108,19 +109,32 @@ return 0; void PARSER_GET_USER::ParseEnd(const char * /*el*/) { depth--; -if (depth == 0) +if (depth == 0 && parsingAnswer) { if (callback) callback(error.empty(), error, info, data); error.clear(); + parsingAnswer = false; } } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseUser(const char * el, const char ** attr) { if (strcasecmp(el, "user") == 0) - if (strcasecmp(attr[1], "error") == 0) - error = "User not found."; + if (attr && attr[0] && attr[1]) + { + if (strcasecmp(attr[1], "error") == 0) + { + if (attr[2] && attr[3]) + error = attr[3]; + else + error = "User not found."; + } + else + parsingAnswer = true; + } + else + parsingAnswer = true; } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr) diff --git a/stglibs/srvconf.lib/parser_get_users.cpp b/stglibs/srvconf.lib/parser_get_users.cpp index 26ffc157..2f380c82 100644 --- a/stglibs/srvconf.lib/parser_get_users.cpp +++ b/stglibs/srvconf.lib/parser_get_users.cpp @@ -28,7 +28,8 @@ PARSER_GET_USERS::PARSER_GET_USERS() : callback(NULL), data(NULL), - depth(0) + depth(0), + parsingAnswer(false) { userParser.SetCallback(&PARSER_GET_USERS::UserCallback, this); } @@ -36,10 +37,10 @@ PARSER_GET_USERS::PARSER_GET_USERS() int PARSER_GET_USERS::ParseStart(const char * el, const char ** attr) { depth++; -if (depth == 1) - ParseUsers(el, attr); +if (depth == 1 && strcasecmp(el, "users") == 0) + parsingAnswer = true; -if (depth > 1) +if (depth > 1 && parsingAnswer) userParser.ParseStart(el, attr); return 0; @@ -48,21 +49,17 @@ return 0; void PARSER_GET_USERS::ParseEnd(const char * el) { depth--; -if (depth > 0) +if (depth > 0 && parsingAnswer) userParser.ParseEnd(el); -if (depth == 0) +if (depth == 0 && parsingAnswer) { if (callback) callback(error.empty(), error, info, data); error.clear(); - } -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::ParseUsers(const char * el, const char ** /*attr*/) -{ -if (strcasecmp(el, "users") == 0) info.clear(); + parsingAnswer = false; + } } //----------------------------------------------------------------------------- void PARSER_GET_USERS::AddUser(const PARSER_GET_USER::INFO & userInfo) diff --git a/stglibs/srvconf.lib/parser_send_message.cpp b/stglibs/srvconf.lib/parser_send_message.cpp index 2e2f785a..c1955fce 100644 --- a/stglibs/srvconf.lib/parser_send_message.cpp +++ b/stglibs/srvconf.lib/parser_send_message.cpp @@ -32,7 +32,7 @@ PARSER_SEND_MESSAGE::PARSER_SEND_MESSAGE() { } //----------------------------------------------------------------------------- -int PARSER_SEND_MESSAGE::ParseStart(const char * el, const char ** attr) +int PARSER_SEND_MESSAGE::ParseStart(const char * el, const char ** attr) { depth++; if (depth == 1) diff --git a/stglibs/srvconf.lib/parser_server_info.cpp b/stglibs/srvconf.lib/parser_server_info.cpp index c8b1abfc..645f6099 100644 --- a/stglibs/srvconf.lib/parser_server_info.cpp +++ b/stglibs/srvconf.lib/parser_server_info.cpp @@ -40,7 +40,8 @@ const size_t DIRNAME_LEN = 16; PARSER_SERVER_INFO::PARSER_SERVER_INFO() : callback(NULL), data(NULL), - depth(0) + depth(0), + parsingAnswer(false) { AddParser(propertyParsers, "uname", info.uname); AddParser(propertyParsers, "version", info.version); @@ -57,10 +58,10 @@ int PARSER_SERVER_INFO::ParseStart(const char *el, const char **attr) { depth++; if (depth == 1) - if (strcasecmp(el, "ServerInfo") != 0) - error = "Invalid response."; + if (strcasecmp(el, "ServerInfo") == 0) + parsingAnswer = true; else - if (depth == 2) + if (depth == 2 && parsingAnswer) if (!TryParse(propertyParsers, ToLower(el), attr)) error = "Invalid parameter."; return 0; @@ -69,11 +70,12 @@ return 0; void PARSER_SERVER_INFO::ParseEnd(const char * /*el*/) { depth--; -if (depth == 0) +if (depth == 0 && parsingAnswer) { if (callback) callback(error.empty(), error, info, data); error.clear(); + parsingAnswer = false; } } //----------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/property_parsers.cpp b/stglibs/srvconf.lib/property_parsers.cpp index 4c5fcd6d..fcb9020a 100644 --- a/stglibs/srvconf.lib/property_parsers.cpp +++ b/stglibs/srvconf.lib/property_parsers.cpp @@ -35,7 +35,7 @@ Decode21str(value, attr[1]); return true; } -bool GetIPValue(const char ** attr, uint32_t value) +bool GetIPValue(const char ** attr, uint32_t & value) { if (!CheckValue(attr)) return false;