]> git.stg.codes - stg.git/commitdiff
Configuration library refactoring (AUTH_BY)
authorMaxim Mamontov <faust.madf@gmail.com>
Mon, 5 Aug 2013 18:24:16 +0000 (21:24 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Mon, 5 Aug 2013 18:24:16 +0000 (21:24 +0300)
projects/sgconf/common_sg.cpp
stglibs/srvconf.lib/Makefile
stglibs/srvconf.lib/include/stg/parser.h [new file with mode: 0644]
stglibs/srvconf.lib/include/stg/parser_auth_by.h [new file with mode: 0644]
stglibs/srvconf.lib/include/stg/servconf.h
stglibs/srvconf.lib/parser.cpp
stglibs/srvconf.lib/parser_auth_by.cpp [new file with mode: 0644]
stglibs/srvconf.lib/servconf.cpp

index bf2ac6225afe53bc149f8f5f7771f4966cd2843f..b73a79c86a7ea2c121d66c075a23e32c29560797 100644 (file)
@@ -52,12 +52,6 @@ struct GetUserCbData
     void * data;
     bool * result;
 };
     void * data;
     bool * result;
 };
-//-----------------------------------------------------------------------------
-struct AuthByCbData
-{
-    void * data;
-    bool * result;
-};
 //---------------------------------------------------------------------------
 struct HelpParams
 {
 //---------------------------------------------------------------------------
 struct HelpParams
 {
@@ -418,20 +412,11 @@ for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
 *result = true;
 }
 //-----------------------------------------------------------------------------
 *result = true;
 }
 //-----------------------------------------------------------------------------
-void RecvAuthByData(const std::vector<std::string> & list, void * d)
+void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *)
 {
 {
-AuthByCbData * abcbd;
-abcbd = (AuthByCbData *)d;
-
-bool * result = abcbd->result;
-
-REQUEST * req = (REQUEST *)abcbd->data;
-
 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
     cout << *it << "\n";
 cout << endl;
 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
     cout << *it << "\n";
 cout << endl;
-
-*result = true;
 }
 //-----------------------------------------------------------------------------
 int ProcessSetUser(const std::string &server,
 }
 //-----------------------------------------------------------------------------
 int ProcessSetUser(const std::string &server,
@@ -531,33 +516,21 @@ int ProcessAuthBy(const std::string &server,
 {
 SERVCONF sc;
 
 {
 SERVCONF sc;
 
-bool result = false;
-
-sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
-sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
-sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
+sc.SetServer(server.c_str());
+sc.SetPort(port);
+sc.SetAdmLogin(admLogin.c_str());
 sc.SetAdmPassword(admPasswd.c_str());
 
 sc.SetAdmPassword(admPasswd.c_str());
 
-// TODO Good variable name :)
-AuthByCbData abcbd;
-
-abcbd.data = data;
-abcbd.result = &result;
-
-sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd);
-sc.GetUserAuthBy(login.c_str());
+sc.SetAuthByCallback(RecvAuthByData, NULL);
+sc.AuthBy(login.c_str());
 
 
-if (result)
-    {
-    printf("Ok\n");
-    return 0;
-    }
-else
+if (sc.GetError())
     {
     printf("Error\n");
     return -1;
     }
 
     {
     printf("Error\n");
     return -1;
     }
 
+printf("Ok\n");
 return 0;
 }
 //-----------------------------------------------------------------------------
 return 0;
 }
 //-----------------------------------------------------------------------------
index e3bfa76219fdab686afac6203e6a76186be74e83..ab170663e8a64bd1fa6bd0e5d4a5d0ffd8a23a03 100644 (file)
@@ -8,14 +8,15 @@ STGLIBS = -lstgcommon \
           -lstgcrypto
 LIBS = -lexpat
 
           -lstgcrypto
 LIBS = -lexpat
 
-SRCS =         netunit.cpp \
+SRCS =  netunit.cpp \
         parser.cpp \
         parser.cpp \
+        parser_auth_by.cpp \
         servconf.cpp
 
 INCS = servconf.h \
        netunit.h
 
 LIB_INCS = -I ../common.lib/include \
         servconf.cpp
 
 INCS = servconf.h \
        netunit.h
 
 LIB_INCS = -I ../common.lib/include \
-          -I ../crypto.lib/include
+           -I ../crypto.lib/include
 
 include ../Makefile.in
 
 include ../Makefile.in
diff --git a/stglibs/srvconf.lib/include/stg/parser.h b/stglibs/srvconf.lib/include/stg/parser.h
new file mode 100644 (file)
index 0000000..6626369
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef __STG_STGLIBS_SRVCONF_PARSER_H__
+#define __STG_STGLIBS_SRVCONF_PARSER_H__
+
+class PARSER
+{
+public:
+    virtual ~PARSER() {}
+    virtual int ParseStart(const char *el, const char **attr) = 0;
+    virtual void ParseEnd(const char *el) = 0;
+};
+
+#endif
diff --git a/stglibs/srvconf.lib/include/stg/parser_auth_by.h b/stglibs/srvconf.lib/include/stg/parser_auth_by.h
new file mode 100644 (file)
index 0000000..3ef1d00
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__
+#define __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__
+
+#include "stg/parser.h"
+
+#include <vector>
+#include <string>
+
+class PARSER_AUTH_BY: public PARSER
+{
+public:
+    typedef std::vector<std::string> INFO;
+    typedef void (* CALLBACK)(const INFO & info, void * data);
+
+    PARSER_AUTH_BY();
+    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;
+    INFO info;
+};
+
+#endif
index dbbe20013daa4087224221ea5c94e4c8c500d0d8..b97058512e9baba7a5ec5dd587b8642a9ee03461 100644 (file)
 #ifndef SERVCONF_H
 #define SERVCONF_H
 
 #ifndef SERVCONF_H
 #define SERVCONF_H
 
-#include <expat.h>
+#include "netunit.h"
+
+#include "stg/parser_auth_by.h"
+#include "stg/os_int.h"
+#include "stg/const.h"
 
 #include <list>
 #include <vector>
 #include <string>
 
 
 #include <list>
 #include <vector>
 #include <string>
 
-#include "stg/os_int.h"
-#include "stg/const.h"
-#include "netunit.h"
+#include <expat.h>
 
 void Start(void *data, const char *el, const char **attr);
 void End(void *data, const char *el);
 
 void Start(void *data, const char *el, const char **attr);
 void End(void *data, const char *el);
@@ -102,22 +104,12 @@ typedef void(*RecvServerInfoDataCb_t)(SERVERINFO * si, void * data);
 typedef int(*RecvChgUserCb_t)(const char * asnwer, void * data);
 typedef int(*RecvCheckUserCb_t)(const char * answer, void * data);
 typedef int(*RecvSendMessageCb_t)(const char * answer, void * data);
 typedef int(*RecvChgUserCb_t)(const char * asnwer, void * data);
 typedef int(*RecvCheckUserCb_t)(const char * answer, void * data);
 typedef int(*RecvSendMessageCb_t)(const char * answer, void * data);
-typedef void(*RecvAuthByDataCb_t)(const std::vector<std::string> & list, void * data);
 //-----------------------------------------------------------------------------
 struct ADMINDATA
 {
     char login[ADM_LOGIN_LEN];
 };
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 struct ADMINDATA
 {
     char login[ADM_LOGIN_LEN];
 };
 //-----------------------------------------------------------------------------
-class PARSER
-{
-public:
-    PARSER() {}
-    virtual ~PARSER() {}
-    virtual int ParseStart(const char *el, const char **attr) = 0;
-    virtual void ParseEnd(const char *el) = 0;
-};
-//-----------------------------------------------------------------------------
 class PARSER_CHG_USER: public PARSER
 {
 public:
 class PARSER_CHG_USER: public PARSER
 {
 public:
@@ -226,23 +218,6 @@ private:
     bool error;
 };
 //-----------------------------------------------------------------------------
     bool error;
 };
 //-----------------------------------------------------------------------------
-class PARSER_AUTH_BY: public PARSER
-{
-public:
-    PARSER_AUTH_BY();
-    int  ParseStart(const char *el, const char **attr);
-    void ParseEnd(const char *el);
-    void ParseServerInfo(const char *el, const char **attr);
-    bool GetError();
-    void SetRecvCb(RecvAuthByDataCb_t, void * data);
-private:
-    RecvAuthByDataCb_t RecvAuthByDataCb;
-    void * authByDataCb;
-    int depth;
-    bool error;
-    std::vector<std::string> list;
-};
-//-----------------------------------------------------------------------------
 class SERVCONF
 {
 public:
 class SERVCONF
 {
 public:
@@ -254,7 +229,7 @@ public:
     void SetAdmPassword(const char * password);
 
     void SetUserDataRecvCb(RecvUserDataCb_t, void * data);
     void SetAdmPassword(const char * password);
 
     void SetUserDataRecvCb(RecvUserDataCb_t, void * data);
-    void SetGetUserAuthByRecvCb(RecvAuthByDataCb_t, void * data);
+    void SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data);
     void SetServerInfoRecvCb(RecvServerInfoDataCb_t, void * data);
     void SetChgUserCb(RecvChgUserCb_t, void * data);
     void SetCheckUserCb(RecvCheckUserCb_t, void * data);
     void SetServerInfoRecvCb(RecvServerInfoDataCb_t, void * data);
     void SetChgUserCb(RecvChgUserCb_t, void * data);
     void SetCheckUserCb(RecvCheckUserCb_t, void * data);
@@ -264,7 +239,7 @@ public:
     int GetUsers();
     int GetUser(const char * login);
     int ChgUser(const char * request);
     int GetUsers();
     int GetUser(const char * login);
     int ChgUser(const char * request);
-    int GetUserAuthBy(const char * login);
+    int AuthBy(const char * login);
     // TODO: Remove this shit!
     int MsgUser(const char * request);
     int SendMessage(const char * login, const char * message, int prio);
     // TODO: Remove this shit!
     int MsgUser(const char * request);
     int SendMessage(const char * login, const char * message, int prio);
@@ -296,14 +271,14 @@ private:
 
     RecvUserDataCb_t RecvUserDataCb;
     RecvUserDataCb_t RecvGetUserDataCb;
 
     RecvUserDataCb_t RecvUserDataCb;
     RecvUserDataCb_t RecvGetUserDataCb;
-    RecvAuthByDataCb_t RecvAuthByCb;
+    PARSER_AUTH_BY::CALLBACK authByCallback;
     RecvServerInfoDataCb_t RecvServerInfoDataCb;
     RecvChgUserCb_t RecvChgUserCb;
     RecvCheckUserCb_t RecvCheckUserCb;
     RecvSendMessageCb_t RecvSendMessageCb;
 
     void * getUserDataDataCb;
     RecvServerInfoDataCb_t RecvServerInfoDataCb;
     RecvChgUserCb_t RecvChgUserCb;
     RecvCheckUserCb_t RecvCheckUserCb;
     RecvSendMessageCb_t RecvSendMessageCb;
 
     void * getUserDataDataCb;
-    void * getUserAuthByDataCb;
+    void * authByData;
     void * getUsersDataDataCb;
     void * getServerInfoDataCb;
     void * chgUserDataCb;
     void * getUsersDataDataCb;
     void * getServerInfoDataCb;
     void * chgUserDataCb;
index 284e1127e5f83da67bb6f04de36f92bd6a6f34f3..a87266eb5cb57d37fd6cb81f002134cbdde1e547 100644 (file)
@@ -822,58 +822,6 @@ if (strcmp(attr[0], "value") == 0)
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-PARSER_AUTH_BY::PARSER_AUTH_BY()
-    : RecvAuthByDataCb(NULL),
-      authByDataCb(NULL),
-      depth(0),
-      error(false),
-      list()
-{
-}
-//-----------------------------------------------------------------------------
-int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr)
-{
-depth++;
-if (depth == 1)
-    {
-    if (strcasecmp(el, "AuthorizedBy") != 0)
-        {
-        list.erase(list.begin(), list.end());
-        //printf("%s\n", el);
-        }
-    }
-else
-    {
-    if (depth == 2)
-        {
-        if (strcasecmp(el, "Auth") == 0)
-            {
-            if (attr && attr[0] && attr[1])
-                list.push_back(attr[1]);
-            return 0;
-            }
-        }
-    }
-return 0;
-}
-//-----------------------------------------------------------------------------
-void PARSER_AUTH_BY::ParseEnd(const char *)
-{
-depth--;
-if (depth == 0)
-    {
-    RecvAuthByDataCb(list, authByDataCb);
-    }
-}
-//-----------------------------------------------------------------------------
-void PARSER_AUTH_BY::SetRecvCb(RecvAuthByDataCb_t f, void * data)
-{
-RecvAuthByDataCb = f;
-authByDataCb = data;
-}
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
 PARSER_CHG_USER::PARSER_CHG_USER()
     : RecvChgUserCb(NULL),
       chgUserCbData(NULL),
 PARSER_CHG_USER::PARSER_CHG_USER()
     : RecvChgUserCb(NULL),
       chgUserCbData(NULL),
diff --git a/stglibs/srvconf.lib/parser_auth_by.cpp b/stglibs/srvconf.lib/parser_auth_by.cpp
new file mode 100644 (file)
index 0000000..63c23a0
--- /dev/null
@@ -0,0 +1,46 @@
+#include "stg/parser_auth_by.h"
+
+#include <strings.h> // strcasecmp
+
+PARSER_AUTH_BY::PARSER_AUTH_BY()
+    : callback(NULL),
+      data(NULL),
+      depth(0)
+{
+}
+//-----------------------------------------------------------------------------
+int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr)
+{
+depth++;
+if (depth == 1)
+    {
+    if (strcasecmp(el, "AuthorizedBy") != 0)
+        info.clear();
+    }
+else
+    {
+    if (depth == 2)
+        {
+        if (strcasecmp(el, "Auth") == 0)
+            {
+            if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "name") == 0)
+                info.push_back(attr[1]);
+            return 0;
+            }
+        }
+    }
+return 0;
+}
+//-----------------------------------------------------------------------------
+void PARSER_AUTH_BY::ParseEnd(const char * /*el*/)
+{
+depth--;
+if (depth == 0)
+    callback(info, data);
+}
+//-----------------------------------------------------------------------------
+void PARSER_AUTH_BY::SetCallback(CALLBACK f, void * data)
+{
+callback = f;
+data = data;
+}
index 0ca3028c9e881209cf4d881696c6aedc0ae9a41e..d7df47040c1ba99a91f1653e7a687e78610fcba7 100644 (file)
@@ -96,12 +96,13 @@ SERVCONF::SERVCONF()
       error(0),
       RecvUserDataCb(NULL),
       RecvGetUserDataCb(NULL),
       error(0),
       RecvUserDataCb(NULL),
       RecvGetUserDataCb(NULL),
+      authByCallback(NULL),
       RecvServerInfoDataCb(NULL),
       RecvChgUserCb(NULL),
       RecvCheckUserCb(NULL),
       RecvSendMessageCb(NULL),
       getUserDataDataCb(NULL),
       RecvServerInfoDataCb(NULL),
       RecvChgUserCb(NULL),
       RecvCheckUserCb(NULL),
       RecvSendMessageCb(NULL),
       getUserDataDataCb(NULL),
-      getUserAuthByDataCb(NULL),
+      authByData(NULL),
       getUsersDataDataCb(NULL),
       getServerInfoDataCb(NULL),
       chgUserDataCb(NULL),
       getUsersDataDataCb(NULL),
       getServerInfoDataCb(NULL),
       chgUserDataCb(NULL),
@@ -162,18 +163,18 @@ if ((ret = nt.Disconnect()) != st_ok)
 return st_ok;
 }
 //-----------------------------------------------------------------------------
 return st_ok;
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::GetUserAuthBy(const char * l)
+int SERVCONF::AuthBy(const char * l)
 {
 char request[255];
 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
 {
 char request[255];
 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
-int ret;
 
 currParser = &parserAuthBy;
 
 currParser = &parserAuthBy;
-((PARSER_AUTH_BY*)currParser)->SetRecvCb(RecvAuthByCb, getUserAuthByDataCb);
+((PARSER_AUTH_BY*)currParser)->SetCallback(authByCallback, authByData);
 
 nt.Reset();
 nt.SetRxCallback(this, AnsRecv);
 
 
 nt.Reset();
 nt.SetRxCallback(this, AnsRecv);
 
+int ret;
 if ((ret = nt.Connect()) != st_ok)
     {
     errorMsg = nt.GetError();
 if ((ret = nt.Connect()) != st_ok)
     {
     errorMsg = nt.GetError();
@@ -400,10 +401,10 @@ RecvGetUserDataCb = f;            //GET_USER
 getUserDataDataCb = data;
 }
 //-----------------------------------------------------------------------------
 getUserDataDataCb = data;
 }
 //-----------------------------------------------------------------------------
-void SERVCONF::SetGetUserAuthByRecvCb(RecvAuthByDataCb_t f, void * data)
+void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
 {
 {
-RecvAuthByCb = f;
-getUserAuthByDataCb = data;
+authByCallback = f;
+authByData = data;
 }
 //-----------------------------------------------------------------------------
 void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
 }
 //-----------------------------------------------------------------------------
 void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)