From: Maxim Mamontov Date: Mon, 19 Aug 2013 05:57:38 +0000 (+0300) Subject: Better handling of errors from server. Refactoring. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/cd64751cac4c56591bee9b0a9c2a626ce40e67db?ds=sidebyside Better handling of errors from server. Refactoring. --- diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp index 431d17f3..601b34bc 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp @@ -1363,11 +1363,11 @@ switch (result) break; case res_params_error: printfd(__FILE__, "res_params_error\n"); - answerList->push_back(""); + answerList->push_back(""); break; case res_unknown: printfd(__FILE__, "res_unknown\n"); - answerList->push_back(""); + answerList->push_back(""); break; default: printfd(__FILE__, "res_default\n"); @@ -1421,30 +1421,17 @@ else answerList->push_back(""); } //----------------------------------------------------------------------------- -/*void PARSERDELUSER::CreateAnswer(char * mes) -{ -//answerList->clear(); -answerList->erase(answerList->begin(), answerList->end()); - -char str[255]; -sprintf(str, "", mes); -answerList->push_back(str); -}*/ -//----------------------------------------------------------------------------- // CHECK USER // //----------------------------------------------------------------------------- int PARSER_CHECK_USER::ParseStart(void *, const char *el, const char **attr) { -result = false; - if (strcasecmp(el, "CheckUser") == 0) { if (attr[0] == NULL || attr[1] == NULL || attr[2] == NULL || attr[3] == NULL) { - result = false; - CreateAnswer(); + CreateAnswer("Invalid parameters."); printfd(__FILE__, "PARSER_CHECK_USER - attr err\n"); return 0; } @@ -1452,22 +1439,19 @@ if (strcasecmp(el, "CheckUser") == 0) USER_PTR user; if (users->FindByName(attr[1], &user)) { - result = false; - CreateAnswer(); + CreateAnswer("User not found."); printfd(__FILE__, "PARSER_CHECK_USER - login err\n"); return 0; } if (strcmp(user->GetProperty().password.Get().c_str(), attr[3])) { - result = false; - CreateAnswer(); + CreateAnswer("Wrong password."); printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n"); return 0; } - result = true; - CreateAnswer(); + CreateAnswer(NULL); return 0; } return -1; @@ -1482,12 +1466,12 @@ if (strcasecmp(el, "CheckUser") == 0) return -1; } //----------------------------------------------------------------------------- -void PARSER_CHECK_USER::CreateAnswer() +void PARSER_CHECK_USER::CreateAnswer(const char * error) { -if (result) - answerList->push_back(""); +if (error) + answerList->push_back(std::string(""); else - answerList->push_back(""); + answerList->push_back(""); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser.h b/projects/stargazer/plugins/configuration/sgconfig/parser.h index df3d7155..636fa542 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser.h @@ -7,10 +7,6 @@ #ifndef PARSER_H #define PARSER_H -#include -#include -#include - #include "stg/resetable.h" #include "stg/const.h" #include "stg/store.h" @@ -19,6 +15,10 @@ #include "stg/users.h" #include "stg/message.h" +#include +#include +#include + class TARIFFS; class SETTINGS; @@ -236,9 +236,7 @@ public: PARSER_CHECK_USER() : BASE_PARSER(), result(false) {} int ParseStart(void *data, const char *el, const char **attr); int ParseEnd(void *data, const char *el); - void CreateAnswer(); -private: - bool result; + void CreateAnswer(const char * error); }; //----------------------------------------------------------------------------- class PARSER_SEND_MESSAGE: public BASE_PARSER { diff --git a/stglibs/srvconf.lib/Makefile b/stglibs/srvconf.lib/Makefile index 5cbc5d40..aaf3e117 100644 --- a/stglibs/srvconf.lib/Makefile +++ b/stglibs/srvconf.lib/Makefile @@ -9,6 +9,7 @@ STGLIBS = -lstgcommon \ LIBS = -lexpat SRCS = netunit.cpp \ + property_parsers.cpp \ parser_auth_by.cpp \ parser_server_info.cpp \ parser_check_user.cpp \ diff --git a/stglibs/srvconf.lib/include/stg/parser_check_user.h b/stglibs/srvconf.lib/include/stg/parser_check_user.h index 8088a0c0..e0bea221 100644 --- a/stglibs/srvconf.lib/include/stg/parser_check_user.h +++ b/stglibs/srvconf.lib/include/stg/parser_check_user.h @@ -24,21 +24,23 @@ #include "parser.h" +#include + class PARSER_CHECK_USER: public PARSER { public: - typedef int (* CALLBACK)(const char * answer, void * data); + typedef int (* CALLBACK)(bool result, const std::string & reason, void * data); PARSER_CHECK_USER(); - int ParseStart(const char *el, const char **attr); - void ParseEnd(const char *el); + 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); + void ParseAnswer(const char * el, const char ** attr); }; #endif diff --git a/stglibs/srvconf.lib/include/stg/parser_chg_user.h b/stglibs/srvconf.lib/include/stg/parser_chg_user.h index d8920cde..db54026a 100644 --- a/stglibs/srvconf.lib/include/stg/parser_chg_user.h +++ b/stglibs/srvconf.lib/include/stg/parser_chg_user.h @@ -24,10 +24,12 @@ #include "parser.h" +#include + class PARSER_CHG_USER: public PARSER { public: - typedef int (* CALLBACK)(const char * asnwer, void * data); + typedef int (* 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 7b6032e0..a8700ad0 100644 --- a/stglibs/srvconf.lib/include/stg/parser_get_user.h +++ b/stglibs/srvconf.lib/include/stg/parser_get_user.h @@ -24,35 +24,15 @@ #include "parser.h" +#include "property_parsers.h" + #include "stg/os_int.h" #include "stg/const.h" #include -#include #include -class BASE_PROPERTY_PARSER -{ - public: - virtual ~BASE_PROPERTY_PARSER() {} - virtual void Parse(const char ** attr) = 0; -}; - -template -class PROPERTY_PARSER : public BASE_PROPERTY_PARSER -{ - public: - typedef T (* FUNC)(const char **); - PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {} - virtual void Parse(const char ** attr) { value = func(attr); } - private: - T & value; - FUNC func; -}; - -typedef std::map PROPERTY_PARSERS; - class PARSER_GET_USER: public PARSER { public: @@ -92,7 +72,7 @@ public: std::string userData[USERDATA_NUM]; }; - typedef void (* CALLBACK)(const INFO & info, void * data); + typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); PARSER_GET_USER(); virtual ~PARSER_GET_USER(); @@ -105,6 +85,7 @@ private: void * data; INFO info; int depth; + std::string error; void ParseUser(const char *el, const char **attr); void ParseUserParams(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 5338a913..57907026 100644 --- a/stglibs/srvconf.lib/include/stg/parser_get_users.h +++ b/stglibs/srvconf.lib/include/stg/parser_get_users.h @@ -33,7 +33,7 @@ class PARSER_GET_USERS: 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_GET_USERS(); int ParseStart(const char * el, const char ** attr); @@ -45,8 +45,10 @@ private: PARSER_GET_USER userParser; INFO info; int depth; + 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); diff --git a/stglibs/srvconf.lib/include/stg/parser_send_message.h b/stglibs/srvconf.lib/include/stg/parser_send_message.h index d14153e0..9d1f33dc 100644 --- a/stglibs/srvconf.lib/include/stg/parser_send_message.h +++ b/stglibs/srvconf.lib/include/stg/parser_send_message.h @@ -24,10 +24,12 @@ #include "parser.h" +#include + class PARSER_SEND_MESSAGE: public PARSER { public: - typedef int (* CALLBACK)(const char * answer, void * data); + typedef int (* 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 5adb2abb..33ff5b66 100644 --- a/stglibs/srvconf.lib/include/stg/parser_server_info.h +++ b/stglibs/srvconf.lib/include/stg/parser_server_info.h @@ -24,6 +24,7 @@ #include "parser.h" +#include "property_parsers.h" #include "stg/const.h" #include @@ -41,19 +42,19 @@ public: int dirNum; std::string dirName[DIR_NUM]; }; - typedef void (* CALLBACK)(const INFO & info, void * data); + typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); PARSER_SERVER_INFO(); - int ParseStart(const char *el, const char **attr); - void ParseEnd(const char *el); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); void SetCallback(CALLBACK f, void * data); private: - void ParseDirName(const char **attr, int d); - + PROPERTY_PARSERS propertyParsers; CALLBACK callback; void * data; int depth; INFO info; + std::string error; }; #endif diff --git a/stglibs/srvconf.lib/include/stg/property_parsers.h b/stglibs/srvconf.lib/include/stg/property_parsers.h new file mode 100644 index 00000000..cb3943db --- /dev/null +++ b/stglibs/srvconf.lib/include/stg/property_parsers.h @@ -0,0 +1,94 @@ +/* + * 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_PROPERTY_PARSERS_H__ +#define __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__ + +#include +#include + +#include "stg/common.h" + +class BASE_PROPERTY_PARSER +{ + public: + virtual ~BASE_PROPERTY_PARSER() {} + virtual bool Parse(const char ** attr) = 0; +}; + +template +class PROPERTY_PARSER : public BASE_PROPERTY_PARSER +{ + public: + typedef bool (* FUNC)(const char **, T &); + PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {} + virtual bool Parse(const char ** attr) { return func(attr, value); } + private: + T & value; + FUNC func; +}; + +typedef std::map PROPERTY_PARSERS; + +bool CheckValue(const char ** attr); + +template +bool GetValue(const char ** attr, T & value) +{ +if (CheckValue(attr)) + if (str2x(attr[1], value) < 0) + return false; +return true; +} + +template <> +bool GetValue(const char ** attr, std::string & value) +{ +if (!CheckValue(attr)) + return false; +value = attr[1]; +return true; +} + +template <> +bool GetValue(const char ** attr, double & value) +{ +if (CheckValue(attr)) + if (strtodouble2(attr[1], value)) + return false; +return true; +} + +bool GetEncodedValue(const char ** attr, std::string & value); + +bool GetIPValue(const char ** attr, uint32_t& value); + +template +void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue); + +template +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))); +} + +bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr); + +#endif diff --git a/stglibs/srvconf.lib/parser_check_user.cpp b/stglibs/srvconf.lib/parser_check_user.cpp index 0de1cedd..b99a4390 100644 --- a/stglibs/srvconf.lib/parser_check_user.cpp +++ b/stglibs/srvconf.lib/parser_check_user.cpp @@ -48,9 +48,12 @@ depth--; //----------------------------------------------------------------------------- void PARSER_CHECK_USER::ParseAnswer(const char *, const char **attr) { +if (!callback) + return; if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0) - if (callback) - callback(attr[1], data); + callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data); +else + callback(false, "Invalid response.", data); } //----------------------------------------------------------------------------- void PARSER_CHECK_USER::SetCallback(CALLBACK f, void * d) diff --git a/stglibs/srvconf.lib/parser_chg_user.cpp b/stglibs/srvconf.lib/parser_chg_user.cpp index 90d78f6c..53ce941d 100644 --- a/stglibs/srvconf.lib/parser_chg_user.cpp +++ b/stglibs/srvconf.lib/parser_chg_user.cpp @@ -54,9 +54,12 @@ depth--; //----------------------------------------------------------------------------- void PARSER_CHG_USER::ParseAnswer(const char * /*el*/, const char ** attr) { +if (!callback) + return; if (attr && attr[0] && attr[1]) - if (callback) - callback(attr[1], data); + callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data); +else + callback(false, "Invalid response.", data); } //----------------------------------------------------------------------------- void PARSER_CHG_USER::SetCallback(CALLBACK f, void * d) diff --git a/stglibs/srvconf.lib/parser_get_user.cpp b/stglibs/srvconf.lib/parser_get_user.cpp index 1039cb8d..258d531d 100644 --- a/stglibs/srvconf.lib/parser_get_user.cpp +++ b/stglibs/srvconf.lib/parser_get_user.cpp @@ -23,53 +23,17 @@ #include "stg/common.h" +#include #include #include #include -namespace -{ - -bool checkValue(const char ** attr) -{ -return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; -} - -template -T getValue(const char ** attr) -{ -T value = 0; -if (checkValue(attr)) - if (str2x(attr[1], value) < 0) - return 0; -return value; -} - template <> -std::string getValue(const char ** attr) +bool GetValue(const char ** attr, PARSER_GET_USER::STAT & value) { -if (checkValue(attr)) - return attr[1]; -return ""; -} - -template <> -double getValue(const char ** attr) -{ -double value = 0; -if (checkValue(attr)) - if (strtodouble2(attr[1], value)) - return 0; -return value; -} - -template <> -PARSER_GET_USER::STAT getValue(const char ** attr) -{ -PARSER_GET_USER::STAT value; if (!attr) - return value; + return false; std::map props; for (size_t i = 0; i < DIR_NUM; ++i) { @@ -84,74 +48,42 @@ while (attr[pos]) std::string name(ToLower(attr[pos++])); std::map::iterator it(props.find(name)); if (it != props.end()) - str2x(attr[pos++], *it->second); + if (str2x(attr[pos++], *it->second) < 0) + return false; } -return value; +return true; } -std::string getEncodedValue(const char ** attr) -{ -std::string value; -if (checkValue(attr)) - Decode21str(value, attr[1]); -return value; -} - -uint32_t getIPValue(const char ** attr) -{ -if (checkValue(attr)) - return inet_strington(attr[1]); -return 0; -} - -template -void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = getValue); - -template -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))); -} - -void tryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) -{ - PROPERTY_PARSERS::iterator it(parsers.find(name)); - if (it != parsers.end()) - it->second->Parse(attr); -} - -} // namespace anonymous - PARSER_GET_USER::PARSER_GET_USER() : callback(NULL), data(NULL), depth(0) { - addParser(propertyParsers, "login", info.login); - addParser(propertyParsers, "password", info.password); - addParser(propertyParsers, "cash", info.cash); - addParser(propertyParsers, "credit", info.credit); - addParser(propertyParsers, "creditExpire", info.creditExpire); - addParser(propertyParsers, "lastCash", info.lastCash); - addParser(propertyParsers, "prepaidTraff", info.prepaidTraff); - addParser(propertyParsers, "down", info.down); - addParser(propertyParsers, "passive", info.passive); - addParser(propertyParsers, "disableDetailStat", info.disableDetailStat); - addParser(propertyParsers, "connected", info.connected); - addParser(propertyParsers, "alwaysOnline", info.alwaysOnline); - addParser(propertyParsers, "currIP", info.ip, getIPValue); - addParser(propertyParsers, "ip", info.ips); - addParser(propertyParsers, "tariff", info.tariff); - addParser(propertyParsers, "group", info.group, getEncodedValue); - addParser(propertyParsers, "note", info.note, getEncodedValue); - addParser(propertyParsers, "email", info.email, getEncodedValue); - addParser(propertyParsers, "name", info.name, getEncodedValue); - addParser(propertyParsers, "address", info.address, getEncodedValue); - addParser(propertyParsers, "phone", info.phone, getEncodedValue); - addParser(propertyParsers, "traff", info.stat); + AddParser(propertyParsers, "login", info.login); + AddParser(propertyParsers, "password", info.password); + AddParser(propertyParsers, "cash", info.cash); + AddParser(propertyParsers, "credit", info.credit); + AddParser(propertyParsers, "creditExpire", info.creditExpire); + AddParser(propertyParsers, "lastCash", info.lastCash); + AddParser(propertyParsers, "prepaidTraff", info.prepaidTraff); + AddParser(propertyParsers, "down", info.down); + AddParser(propertyParsers, "passive", info.passive); + AddParser(propertyParsers, "disableDetailStat", info.disableDetailStat); + AddParser(propertyParsers, "connected", info.connected); + AddParser(propertyParsers, "alwaysOnline", info.alwaysOnline); + AddParser(propertyParsers, "currIP", info.ip, GetIPValue); + AddParser(propertyParsers, "ip", info.ips); + AddParser(propertyParsers, "tariff", info.tariff); + AddParser(propertyParsers, "group", info.group, GetEncodedValue); + AddParser(propertyParsers, "note", info.note, GetEncodedValue); + AddParser(propertyParsers, "email", info.email, GetEncodedValue); + AddParser(propertyParsers, "name", info.name, GetEncodedValue); + AddParser(propertyParsers, "address", info.address, GetEncodedValue); + AddParser(propertyParsers, "phone", info.phone, GetEncodedValue); + AddParser(propertyParsers, "traff", info.stat); for (size_t i = 0; i < USERDATA_NUM; ++i) - addParser(propertyParsers, "userData" + x2str(i), info.userData[i], getEncodedValue); + AddParser(propertyParsers, "userData" + x2str(i), info.userData[i], GetEncodedValue); } //----------------------------------------------------------------------------- PARSER_GET_USER::~PARSER_GET_USER() @@ -161,7 +93,7 @@ PARSER_GET_USER::~PARSER_GET_USER() delete (it++)->second; } //----------------------------------------------------------------------------- -int PARSER_GET_USER::ParseStart(const char *el, const char **attr) +int PARSER_GET_USER::ParseStart(const char * el, const char ** attr) { depth++; if (depth == 1) @@ -173,24 +105,28 @@ if (depth == 2) return 0; } //----------------------------------------------------------------------------- -void PARSER_GET_USER::ParseEnd(const char *) +void PARSER_GET_USER::ParseEnd(const char * /*el*/) { depth--; if (depth == 0) + { if (callback) - callback(info, data); + callback(error.empty(), error, info, data); + error.clear(); + } } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseUser(const char * el, const char ** attr) { if (strcasecmp(el, "user") == 0) if (strcasecmp(attr[1], "error") == 0) - info.login = ""; + error = "User not found."; } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr) { -tryParse(propertyParsers, ToLower(el), attr); +if (!TryParse(propertyParsers, ToLower(el), attr)) + error = "Invalid parameter."; } //----------------------------------------------------------------------------- void PARSER_GET_USER::SetCallback(CALLBACK f, void * d) diff --git a/stglibs/srvconf.lib/parser_get_users.cpp b/stglibs/srvconf.lib/parser_get_users.cpp index d4e44d36..26ffc157 100644 --- a/stglibs/srvconf.lib/parser_get_users.cpp +++ b/stglibs/srvconf.lib/parser_get_users.cpp @@ -52,8 +52,11 @@ if (depth > 0) userParser.ParseEnd(el); if (depth == 0) + { if (callback) - callback(info, data); + callback(error.empty(), error, info, data); + error.clear(); + } } //----------------------------------------------------------------------------- void PARSER_GET_USERS::ParseUsers(const char * el, const char ** /*attr*/) @@ -73,8 +76,11 @@ callback = f; data = d; } //----------------------------------------------------------------------------- -void PARSER_GET_USERS::UserCallback(const PARSER_GET_USER::INFO & info, void * data) +void PARSER_GET_USERS::UserCallback(bool result, const std::string & error, const PARSER_GET_USER::INFO & info, void * data) { PARSER_GET_USERS * parser = static_cast(data); - parser->AddUser(info); + if (!result) + parser->SetError(error); + else + parser->AddUser(info); } diff --git a/stglibs/srvconf.lib/parser_send_message.cpp b/stglibs/srvconf.lib/parser_send_message.cpp index 77aba248..2e2f785a 100644 --- a/stglibs/srvconf.lib/parser_send_message.cpp +++ b/stglibs/srvconf.lib/parser_send_message.cpp @@ -48,9 +48,12 @@ depth--; //----------------------------------------------------------------------------- void PARSER_SEND_MESSAGE::ParseAnswer(const char * /*el*/, const char **attr) { +if (!callback) + return; if (attr && attr[0] && attr[1]) - if (callback) - callback(attr[1], data); + callback(strcasecmp(attr[1], "ok") == 0, attr[1], data); +else + callback(false, "Invalid response.", data); } //----------------------------------------------------------------------------- void PARSER_SEND_MESSAGE::SetCallback(CALLBACK f, void * d) diff --git a/stglibs/srvconf.lib/parser_server_info.cpp b/stglibs/srvconf.lib/parser_server_info.cpp index 057d99a5..c8b1abfc 100644 --- a/stglibs/srvconf.lib/parser_server_info.cpp +++ b/stglibs/srvconf.lib/parser_server_info.cpp @@ -35,27 +35,6 @@ const size_t UNAME_LEN = 256; const size_t SERV_VER_LEN = 64; const size_t DIRNAME_LEN = 16; -bool checkValue(const char ** attr) -{ -return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; -} - -int getIntValue(const char ** attr) -{ -int value = -1; -if (checkValue(attr)) - if (str2x(attr[1], value) < 0) - return -1; -return value; -} - -std::string getStringValue(const char ** attr) -{ -if (checkValue(attr)) - return attr[1]; -return ""; -} - } PARSER_SERVER_INFO::PARSER_SERVER_INFO() @@ -63,71 +42,39 @@ PARSER_SERVER_INFO::PARSER_SERVER_INFO() data(NULL), depth(0) { + AddParser(propertyParsers, "uname", info.uname); + AddParser(propertyParsers, "version", info.version); + AddParser(propertyParsers, "tariff", info.tariffType); + AddParser(propertyParsers, "dir_num", info.dirNum); + AddParser(propertyParsers, "users_num", info.usersNum); + AddParser(propertyParsers, "tariff_num", info.tariffNum); + + for (size_t i = 0; i < DIR_NUM; i++) + AddParser(propertyParsers, "dir_name_" + x2str(i), info.dirName[i], GetEncodedValue); } //----------------------------------------------------------------------------- int PARSER_SERVER_INFO::ParseStart(const char *el, const char **attr) { depth++; if (depth == 1) - { if (strcasecmp(el, "ServerInfo") != 0) - { - //printf("%s\n", el); - } - } + error = "Invalid response."; else - { if (depth == 2) - { - if (strcasecmp(el, "uname") == 0) - { - info.uname = getStringValue(attr); - return 0; - } - if (strcasecmp(el, "version") == 0) - { - info.version = getStringValue(attr); - return 0; - } - if (strcasecmp(el, "tariff") == 0) - { - info.tariffType = getIntValue(attr); - return 0; - } - if (strcasecmp(el, "dir_num") == 0) - { - info.dirNum = getIntValue(attr); - return 0; - } - if (strcasecmp(el, "users_num") == 0) - { - info.usersNum = getIntValue(attr); - return 0; - } - if (strcasecmp(el, "tariff_num") == 0) - { - info.tariffNum = getIntValue(attr); - return 0; - } - - for (int j = 0; j < DIR_NUM; j++) - { - char str[16]; - sprintf(str, "dir_name_%d", j); - if (strcasecmp(el, str) == 0) - ParseDirName(attr, j); - } - - } - } + if (!TryParse(propertyParsers, ToLower(el), attr)) + error = "Invalid parameter."; return 0; } //----------------------------------------------------------------------------- void PARSER_SERVER_INFO::ParseEnd(const char * /*el*/) { depth--; -if (depth == 0 && callback) - callback(info, data); +if (depth == 0) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + } } //----------------------------------------------------------------------------- void PARSER_SERVER_INFO::SetCallback(CALLBACK f, void * d) @@ -135,13 +82,3 @@ void PARSER_SERVER_INFO::SetCallback(CALLBACK f, void * d) callback = f; data = d; } -//----------------------------------------------------------------------------- -void PARSER_SERVER_INFO::ParseDirName(const char **attr, int d) -{ -if (checkValue(attr)) - { - char str[2 * DIRNAME_LEN + 1]; - Decode21(str, attr[1]); - info.dirName[d] = str; - } -} diff --git a/stglibs/srvconf.lib/property_parsers.cpp b/stglibs/srvconf.lib/property_parsers.cpp new file mode 100644 index 00000000..4c5fcd6d --- /dev/null +++ b/stglibs/srvconf.lib/property_parsers.cpp @@ -0,0 +1,55 @@ +/* + * 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 + */ + +#include "stg/property_parsers.h" + +#include + +bool CheckValue(const char ** attr) +{ +return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; +} + +bool GetEncodedValue(const char ** attr, std::string & value) +{ +if (!CheckValue(attr)) + return false; +Decode21str(value, attr[1]); +return true; +} + +bool GetIPValue(const char ** attr, uint32_t value) +{ +if (!CheckValue(attr)) + return false; +std::string ip(attr[1]); +value = inet_strington(attr[1]); +if (value == 0 && ip != "0.0.0.0") + return false; +return true; +} + +bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) +{ + PROPERTY_PARSERS::iterator it(parsers.find(name)); + if (it != parsers.end()) + return it->second->Parse(attr); + return true; // Assume that non-existing params are ok. +}