]> git.stg.codes - stg.git/commitdiff
[NY] Implemented SERVCONF using PIMPL.
authorMaxim Mamontov <faust.madf@gmail.com>
Sun, 15 Sep 2013 02:05:04 +0000 (05:05 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Sun, 15 Sep 2013 02:05:04 +0000 (05:05 +0300)
projects/sgconf/common_sg.h
stglibs/srvconf.lib/include/stg/netunit.h
stglibs/srvconf.lib/include/stg/servconf.h
stglibs/srvconf.lib/include/stg/servconf_types.h [new file with mode: 0644]
stglibs/srvconf.lib/netunit.cpp
stglibs/srvconf.lib/servconf.cpp

index 4bff0aa7ae2f74a5216faa9d03dcb2e0277147f1..87b8509b4e2e4ec4e962c29bfa76749fd86ab8cc 100644 (file)
@@ -29,6 +29,7 @@
 #define COMMON_SG_H
 
 #include "stg/servconf.h"
+#include "stg/servconf_types.h"
 #include "request.h"
 
 #include <string>
index 9e120855ea5e788cd89f9f896d3dc02cdaaedc1f..4d3bcfb09e72337e8c09471b7d1caca1b51b6d0b 100644 (file)
 #ifndef NetUnitH
 #define NetUnitH
 
-#include "stg/blowfish.h"
+#include "stg/os_int.h"
 
 #include <string>
 
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#define  STG_HEADER     "SG04"
-#define  OK_HEADER      "OKHD"
-#define  ERR_HEADER     "ERHD"
-#define  OK_LOGIN       "OKLG"
-#define  ERR_LOGIN      "ERLG"
-#define  OK_LOGINS      "OKLS"
-#define  ERR_LOGINS     "ERLS"
-
-// äÌÉÎÎÁ ÛÉÆÒÕÅÍÏÇÏ É ÐÅÒÅÄÁ×ÁÅÍÏÇ ÚÁ ÏÄÉΠÒÁÚ ÂÌÏËÁ ÉÎÆÏÒÍÁÃÉÉ
-#define  ENC_MSG_LEN    (8)
-
-#define MAX_ERR_STR_LEN (64)
-
 typedef bool (* RxCallback_t)(void *, const std::string &, bool);
 
-enum status
-{
-st_ok = 0,
-st_conn_fail,
-st_send_fail,
-st_recv_fail,
-st_header_err,
-st_login_err,
-st_logins_err,
-st_data_err,
-st_unknown_err,
-st_dns_err,
-st_xml_parse_error,
-st_data_error
-};
-
-enum CONF_STATE
-{
-confHdr = 0,
-confLogin,
-confLoginCipher,
-confData
-};
 //---------------------------------------------------------------------------
 class NETTRANSACT
 {
index 285136ea059f8a0659e46c109c5e8d6a0c34a2fc..32ee1cd4b9e6eb00e613f9eb2dfc38943cfb895f 100644 (file)
@@ -27,8 +27,6 @@
 #ifndef SERVCONF_H
 #define SERVCONF_H
 
-#include "netunit.h"
-
 #include "stg/parser_auth_by.h"
 #include "stg/parser_server_info.h"
 #include "stg/parser_check_user.h"
 
 #include <string>
 
-#include <expat.h>
-
 class SERVCONF
 {
 public:
     SERVCONF(const std::string & server, uint16_t port,
              const std::string & login, const std::string & password);
+    ~SERVCONF();
 
     int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data);
     int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data);
@@ -58,28 +55,10 @@ public:
     int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data);
 
     const std::string & GetStrError() const;
-    void Start(const char * el, const char ** attr);
-    void End(const char * el);
 
 private:
-    PARSER * currParser;
-
-    PARSER_GET_USERS parserGetUsers;
-    PARSER_GET_USER parserGetUser;
-    PARSER_AUTH_BY parserAuthBy;
-    PARSER_SERVER_INFO  parserServerInfo;
-    PARSER_CHG_USER parserChgUser;
-    PARSER_CHECK_USER parserCheckUser;
-    PARSER_SEND_MESSAGE parserSendMessage;
-
-    NETTRANSACT nt;
-
-    std::string errorMsg;
-    XML_Parser parser;
-
-    int Exec(const std::string & request, PARSER & cp);
-
-    friend bool AnsRecv(void * data, const std::string & chunk, bool final);
+    class IMPL;
+    IMPL * pImpl;
 };
 //-----------------------------------------------------------------------------
 
diff --git a/stglibs/srvconf.lib/include/stg/servconf_types.h b/stglibs/srvconf.lib/include/stg/servconf_types.h
new file mode 100644 (file)
index 0000000..d7fb847
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ *    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_TYPES_H__
+#define __STG_STGLIBS_SRVCONF_TYPES_H__
+
+#define  STG_HEADER     "SG04"
+#define  OK_HEADER      "OKHD"
+#define  ERR_HEADER     "ERHD"
+#define  OK_LOGIN       "OKLG"
+#define  ERR_LOGIN      "ERLG"
+#define  OK_LOGINS      "OKLS"
+#define  ERR_LOGINS     "ERLS"
+
+#define  ENC_MSG_LEN    (8)
+
+enum status
+{
+st_ok = 0,
+st_conn_fail,
+st_send_fail,
+st_recv_fail,
+st_header_err,
+st_login_err,
+st_logins_err,
+st_data_err,
+st_unknown_err,
+st_dns_err,
+st_xml_parse_error,
+st_data_error
+};
+
+enum CONF_STATE
+{
+confHdr = 0,
+confLogin,
+confLoginCipher,
+confData
+};
+
+#endif
index 4c3cc92ed8bf7626b1f25e40b46e2d1c161317ab..68d64678e5b7d33528475cda68dd3afc89ef72ad 100644 (file)
@@ -27,6 +27,7 @@
 //---------------------------------------------------------------------------
 
 #include "stg/netunit.h"
+#include "stg/servconf_types.h"
 #include "stg/common.h"
 #include "stg/blowfish.h"
 
 #include <arpa/inet.h>
 #include <unistd.h>
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
 namespace
 {
 
index 268749a342427228161597038e25f6a33ed01425..45d60f666c9e520d0a135544ce1372de975e969a 100644 (file)
 
 #include "stg/servconf.h"
 
+#include "stg/netunit.h"
+#include "stg/servconf_types.h"
+
 #include "stg/common.h"
 
 #include <cstdio>
 #include <cstring>
 
-namespace
+#include <expat.h>
+
+class SERVCONF::IMPL
+{
+public:
+    IMPL(const std::string & server, uint16_t port,
+         const std::string & login, const std::string & password);
+
+    int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data);
+    int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data);
+    int ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data);
+    int AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data);
+    int SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data);
+    int ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data);
+    int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data);
+
+    const std::string & GetStrError() const;
+    static void Start(void * data, const char * el, const char ** attr);
+    static void End(void * data, const char * el);
+
+private:
+    PARSER_GET_USERS parserGetUsers;
+    PARSER_GET_USER parserGetUser;
+    PARSER_AUTH_BY parserAuthBy;
+    PARSER_SERVER_INFO  parserServerInfo;
+    PARSER_CHG_USER parserChgUser;
+    PARSER_CHECK_USER parserCheckUser;
+    PARSER_SEND_MESSAGE parserSendMessage;
+
+    NETTRANSACT nt;
+
+    std::string errorMsg;
+    XML_Parser parser;
+
+    int Exec(const std::string & request, PARSER & cp);
+
+    static bool AnsRecv(void * data, const std::string & chunk, bool final);
+};
+
+/*namespace
 {
 
 void ElementStart(void * data, const char * el, const char ** attr)
 {
-static_cast<SERVCONF *>(data)->Start(el, attr);
+static_cast<SERVCONF::IMPL *>(data)->Start(el, attr);
 }
 
 void ElementEnd(void * data, const char * el)
 {
-static_cast<SERVCONF *>(data)->End(el);
+static_cast<SERVCONF::IMPL *>(data)->End(el);
 }
 
 } // namespace anonymous
+*/
 
-bool AnsRecv(void * data, const std::string & chunk, bool final)
+bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
 {
-SERVCONF * sc = static_cast<SERVCONF *>(data);
+SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
 
 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
     {
@@ -56,80 +99,130 @@ if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ER
 return true;
 }
 
-//-----------------------------------------------------------------------------
 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
                    const std::string & login, const std::string & password)
-    : currParser(NULL),
-      nt( server, port, login, password )
+    : pImpl(new IMPL(server, port, login, password))
+{
+}
+
+SERVCONF::~SERVCONF()
+{
+    delete pImpl;
+}
+
+int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
+{
+    return pImpl->GetUsers( f, data );
+}
+
+int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
+{
+    return pImpl->GetUser(login, f, data);
+}
+
+int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
+{
+    return pImpl->ChgUser(request, f, data);
+}
+
+int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data)
+{
+    return pImpl->AuthBy(login, f, data);
+}
+
+int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
+{
+    return pImpl->SendMessage(request, f, data);
+}
+
+int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
+{
+    return pImpl->ServerInfo(f, data);
+}
+
+int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
+{
+    return pImpl->CheckUser(login, password, f, data);
+}
+
+const std::string & SERVCONF::GetStrError() const
+{
+    return pImpl->GetStrError();
+}
+
+//-----------------------------------------------------------------------------
+SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
+                     const std::string & login, const std::string & password)
+    : nt( server, port, login, password )
 {
 parser = XML_ParserCreate(NULL);
 nt.SetRxCallback(this, AnsRecv);
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
+int SERVCONF::IMPL::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
 {
 parserGetUser.SetCallback(f, data);
 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data)
+int SERVCONF::IMPL::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data)
 {
 parserAuthBy.SetCallback(f, data);
 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
+int SERVCONF::IMPL::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
 {
 parserGetUsers.SetCallback(f, data);
 return Exec("<GetUsers/>", parserGetUsers);
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
+int SERVCONF::IMPL::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
 {
 parserServerInfo.SetCallback(f, data);
 return Exec("<GetServerInfo/>", parserServerInfo);
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
+int SERVCONF::IMPL::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
 {
 parserChgUser.SetCallback(f, data);
 return Exec(request, parserChgUser);
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
+int SERVCONF::IMPL::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
 {
 parserSendMessage.SetCallback(f, data);
 return Exec(request, parserSendMessage);
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
+int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
 {
 parserCheckUser.SetCallback(f, data);
 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
 }
 //-----------------------------------------------------------------------------
-void SERVCONF::Start(const char * el, const char ** attr)
+void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
 {
+PARSER * currParser = static_cast<PARSER *>(data);
 currParser->ParseStart(el, attr);
 }
 //-----------------------------------------------------------------------------
-void SERVCONF::End(const char * el)
+void SERVCONF::IMPL::End(void * data, const char * el)
 {
+PARSER * currParser = static_cast<PARSER *>(data);
 currParser->ParseEnd(el);
 }
 //-----------------------------------------------------------------------------
-const std::string & SERVCONF::GetStrError() const
+const std::string & SERVCONF::IMPL::GetStrError() const
 {
 return errorMsg;
 }
 //-----------------------------------------------------------------------------
-int SERVCONF::Exec(const std::string & request, PARSER & cp)
+int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp)
 {
-currParser = &cp;
-
 XML_ParserReset(parser, NULL);
-XML_SetElementHandler(parser, ElementStart, ElementEnd);
-XML_SetUserData(parser, this);
+XML_SetElementHandler(parser, Start, End);
+XML_SetUserData(parser, &cp);
 
 int ret = 0;
 if ((ret = nt.Connect()) != st_ok)