]> git.stg.codes - stg.git/commitdiff
Better handling of errors from server. Refactoring.
authorMaxim Mamontov <faust.madf@gmail.com>
Mon, 19 Aug 2013 05:57:38 +0000 (08:57 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Mon, 19 Aug 2013 05:57:38 +0000 (08:57 +0300)
17 files changed:
projects/stargazer/plugins/configuration/sgconfig/parser.cpp
projects/stargazer/plugins/configuration/sgconfig/parser.h
stglibs/srvconf.lib/Makefile
stglibs/srvconf.lib/include/stg/parser_check_user.h
stglibs/srvconf.lib/include/stg/parser_chg_user.h
stglibs/srvconf.lib/include/stg/parser_get_user.h
stglibs/srvconf.lib/include/stg/parser_get_users.h
stglibs/srvconf.lib/include/stg/parser_send_message.h
stglibs/srvconf.lib/include/stg/parser_server_info.h
stglibs/srvconf.lib/include/stg/property_parsers.h [new file with mode: 0644]
stglibs/srvconf.lib/parser_check_user.cpp
stglibs/srvconf.lib/parser_chg_user.cpp
stglibs/srvconf.lib/parser_get_user.cpp
stglibs/srvconf.lib/parser_get_users.cpp
stglibs/srvconf.lib/parser_send_message.cpp
stglibs/srvconf.lib/parser_server_info.cpp
stglibs/srvconf.lib/property_parsers.cpp [new file with mode: 0644]

index 431d17f31b6c20aaf33757c480748eee4c5024b7..601b34bc46d8bec14b6a56f41c5cc0b287416fcf 100644 (file)
@@ -1363,11 +1363,11 @@ switch (result)
         break;
     case res_params_error:
         printfd(__FILE__, "res_params_error\n");
-        answerList->push_back("<SendMessageResult value=\"Parameters error\"/>");
+        answerList->push_back("<SendMessageResult value=\"Parameters error.\"/>");
         break;
     case res_unknown:
         printfd(__FILE__, "res_unknown\n");
-        answerList->push_back("<SendMessageResult value=\"Unknown user\"/>");
+        answerList->push_back("<SendMessageResult value=\"Unknown user.\"/>");
         break;
     default:
         printfd(__FILE__, "res_default\n");
@@ -1421,30 +1421,17 @@ else
     answerList->push_back("<DelUser value=\"ok\"/>");
 }
 //-----------------------------------------------------------------------------
-/*void PARSERDELUSER::CreateAnswer(char * mes)
-{
-//answerList->clear();
-answerList->erase(answerList->begin(), answerList->end());
-
-char str[255];
-sprintf(str, "<DelUser value=\"%s\"/>", mes);
-answerList->push_back(str);
-}*/
-//-----------------------------------------------------------------------------
 //  CHECK USER
 // <checkuser login="vasya" password=\"123456\"/>
 //-----------------------------------------------------------------------------
 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("<CheckUser value=\"Ok\"/>");
+if (error)
+    answerList->push_back(std::string("<CheckUser value=\"Err\" reason=\"") + error + "\"/>");
 else
-    answerList->push_back("<CheckUser value=\"Err\"/>");
+    answerList->push_back("<CheckUser value=\"Ok\"/>");
 }
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
index df3d7155872b56f3a08aaad57e6d750ce6b0853c..636fa5421173bc47ad20cfb1f3b5c69de0079e90 100644 (file)
@@ -7,10 +7,6 @@
 #ifndef PARSER_H
 #define PARSER_H
 
-#include <list>
-#include <string>
-#include <vector>
-
 #include "stg/resetable.h"
 #include "stg/const.h"
 #include "stg/store.h"
 #include "stg/users.h"
 #include "stg/message.h"
 
+#include <list>
+#include <string>
+#include <vector>
+
 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 {
index 5cbc5d40d7d3fb11849ef6967e32b47c7cb3d4c8..aaf3e117999707230fb2c7a20cdfbe23022c2621 100644 (file)
@@ -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 \
index 8088a0c0718f1863d1caefac6b52682ac8150f9c..e0bea221236ecb921760050b87f59b6592f5ed2f 100644 (file)
 
 #include "parser.h"
 
+#include <string>
+
 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
index d8920cde7e051bc54643e7128d9b8fe8696ec26f..db54026a99ae5877e253cd06f061c7bcd78735e9 100644 (file)
 
 #include "parser.h"
 
+#include <string>
+
 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);
index 7b6032e094a10529ed10ba23d52f2414d4641416..a8700ad02653e5e1e6009f2db936cf2cb34f0482 100644 (file)
 
 #include "parser.h"
 
+#include "property_parsers.h"
+
 #include "stg/os_int.h"
 #include "stg/const.h"
 
 #include <string>
-#include <map>
 
 #include <ctime>
 
-class BASE_PROPERTY_PARSER
-{
-    public:
-        virtual ~BASE_PROPERTY_PARSER() {}
-        virtual void Parse(const char ** attr) = 0;
-};
-
-template <typename T>
-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<std::string, BASE_PROPERTY_PARSER *> 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);
index 5338a91357e8978ae4ee3a52e3fd1cec2ff0c012..57907026376133807c04f855663814a5b1ad7b03 100644 (file)
@@ -33,7 +33,7 @@ class PARSER_GET_USERS: public PARSER
 {
 public:
     typedef std::vector<PARSER_GET_USER::INFO> 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);
index d14153e0d2377f951c62cb186dfb35ea9aa5e375..9d1f33dc92364016a8ae5fb173afd4e6adadf4f8 100644 (file)
 
 #include "parser.h"
 
+#include <string>
+
 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);
index 5adb2abb8729e474679b02d04f25fd1337db6313..33ff5b66c955dcae955242c285db72d582fc052b 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "parser.h"
 
+#include "property_parsers.h"
 #include "stg/const.h"
 
 #include <string>
@@ -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 (file)
index 0000000..cb3943d
--- /dev/null
@@ -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 <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__
+#define __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__
+
+#include <map>
+#include <string>
+
+#include "stg/common.h"
+
+class BASE_PROPERTY_PARSER
+{
+    public:
+        virtual ~BASE_PROPERTY_PARSER() {}
+        virtual bool Parse(const char ** attr) = 0;
+};
+
+template <typename T>
+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<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
+
+bool CheckValue(const char ** attr);
+
+template <typename T>
+bool GetValue(const char ** attr, T & value)
+{
+if (CheckValue(attr))
+    if (str2x(attr[1], value) < 0)
+        return false;
+return true;
+}
+
+template <>
+bool GetValue<std::string>(const char ** attr, std::string & value)
+{
+if (!CheckValue(attr))
+    return false;
+value = attr[1];
+return true;
+}
+
+template <>
+bool GetValue<double>(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 <typename T>
+void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>);
+
+template <typename T>
+void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
+{
+    parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
+}
+
+bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr);
+
+#endif
index 0de1cedd95d30a61bc46093f466b3a15d11ea81c..b99a43904368d6be96d1afe215fb89771b48a1d5 100644 (file)
@@ -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)
index 90d78f6cff17a5dad70612e016036aa5a3edba4c..53ce941d66f4ee411749f71d99c57b2fd4f40732 100644 (file)
@@ -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)
index 1039cb8d842ab08174b6d371ae054950bef44b15..258d531d3bde32c220d4ffc338bb6aca0de1f365 100644 (file)
 
 #include "stg/common.h"
 
+#include <map>
 #include <utility>
 #include <cstddef>
 
 #include <strings.h>
 
-namespace
-{
-
-bool checkValue(const char ** attr)
-{
-return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0;
-}
-
-template <typename T>
-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<std::string>(const char ** attr)
+bool GetValue<PARSER_GET_USER::STAT>(const char ** attr, PARSER_GET_USER::STAT & value)
 {
-if (checkValue(attr))
-    return attr[1];
-return "";
-}
-
-template <>
-double getValue<double>(const char ** attr)
-{
-double value = 0;
-if (checkValue(attr))
-    if (strtodouble2(attr[1], value))
-        return 0;
-return value;
-}
-
-template <>
-PARSER_GET_USER::STAT getValue<PARSER_GET_USER::STAT>(const char ** attr)
-{
-PARSER_GET_USER::STAT value;
 if (!attr)
-    return value;
+    return false;
 std::map<std::string, long long *> 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<std::string, long long *>::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 <typename T>
-void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = getValue<T>);
-
-template <typename T>
-void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
-{
-    parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(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)
index d4e44d36458185e83cadb2f608f81b7718ca6e92..26ffc15705b8a6df0362d07606746cf7270a99fe 100644 (file)
@@ -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<PARSER_GET_USERS *>(data);
-    parser->AddUser(info);
+    if (!result)
+        parser->SetError(error);
+    else
+        parser->AddUser(info);
 }
index 77aba248dd14c2ffd6d2dd9a99fc1e4c1a2c859f..2e2f785a77d690d00edab0ae96e5928dd194e466 100644 (file)
@@ -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)
index 057d99a569e8524622090362d7e8b8e039247d7e..c8b1abfcfeea13f90fcc9639d6a1cfb4b25f48d3 100644 (file)
@@ -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 (file)
index 0000000..4c5fcd6
--- /dev/null
@@ -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 <faust@stargazer.dp.ua>
+ */
+
+#include "stg/property_parsers.h"
+
+#include <strings.h>
+
+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.
+}