]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Removed redundant methods from SERVCONF implementation.
[stg.git] / stglibs / srvconf.lib / servconf.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #include "stg/servconf.h"
22
23 #include "netunit.h"
24 #include "parsers/auth_by.h"
25 #include "parsers/server_info.h"
26 #include "parsers/check_user.h"
27 #include "parsers/get_users.h"
28 #include "parsers/get_user.h"
29 #include "parsers/chg_user.h"
30 #include "parsers/send_message.h"
31 #include "parsers/base.h"
32
33 #include "stg/common.h"
34
35 #include <cstdio>
36 #include <cstring>
37
38 #include <expat.h>
39
40 using namespace STG;
41
42 class SERVCONF::IMPL
43 {
44 public:
45     IMPL(const std::string & server, uint16_t port,
46          const std::string & login, const std::string & password);
47
48     int ServerInfo(SERVER_INFO::CALLBACK f, void * data);
49
50     const std::string & GetStrError() const;
51     static void Start(void * data, const char * el, const char ** attr);
52     static void End(void * data, const char * el);
53
54     template <class P, typename C>
55     int Exec(const std::string & request, C callback, void * data);
56
57 private:
58     NETTRANSACT nt;
59
60     std::string errorMsg;
61     XML_Parser parser;
62
63     static bool AnsRecv(void * data, const std::string & chunk, bool final);
64 };
65
66 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
67 {
68 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
69
70 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
71     {
72     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
73               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
74               XML_ErrorString(XML_GetErrorCode(sc->parser)));
75     printf("%s\n", sc->errorMsg.c_str());
76     return false;
77     }
78
79 return true;
80 }
81
82 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
83                    const std::string & login, const std::string & password)
84     : pImpl(new IMPL(server, port, login, password))
85 {
86 }
87
88 SERVCONF::~SERVCONF()
89 {
90 delete pImpl;
91 }
92
93 int SERVCONF::GetUsers(GET_USERS::CALLBACK f, void * data)
94 {
95 return pImpl->Exec<GET_USERS::PARSER>("<GetUsers/>", f, data);
96 }
97
98 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
99 {
100 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
101 }
102
103 int SERVCONF::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data)
104 {
105 return pImpl->Exec<CHG_USER::PARSER>(request, f, data);
106 }
107
108 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
109 {
110 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
111 }
112
113 int SERVCONF::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data)
114 {
115 return pImpl->Exec<SEND_MESSAGE::PARSER>(request, f, data);
116 }
117
118 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
119 {
120     return pImpl->ServerInfo(f, data);
121 }
122
123 int SERVCONF::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data)
124 {
125 return pImpl->Exec<CHECK_USER::PARSER>("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
126 }
127
128 const std::string & SERVCONF::GetStrError() const
129 {
130     return pImpl->GetStrError();
131 }
132
133 //-----------------------------------------------------------------------------
134 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
135                      const std::string & login, const std::string & password)
136     : nt( server, port, login, password )
137 {
138 parser = XML_ParserCreate(NULL);
139 nt.SetRxCallback(this, AnsRecv);
140 }
141 //-----------------------------------------------------------------------------
142 int SERVCONF::IMPL::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
143 {
144 return Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
145 }
146 //-----------------------------------------------------------------------------
147 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
148 {
149 PARSER * currParser = static_cast<PARSER *>(data);
150 currParser->ParseStart(el, attr);
151 }
152 //-----------------------------------------------------------------------------
153 void SERVCONF::IMPL::End(void * data, const char * el)
154 {
155 PARSER * currParser = static_cast<PARSER *>(data);
156 currParser->ParseEnd(el);
157 }
158 //-----------------------------------------------------------------------------
159 const std::string & SERVCONF::IMPL::GetStrError() const
160 {
161 return errorMsg;
162 }
163 //-----------------------------------------------------------------------------
164 template <class P, typename C>
165 int SERVCONF::IMPL::Exec(const std::string & request, C callback, void * data)
166 {
167 P cp(callback, data);
168 XML_ParserReset(parser, NULL);
169 XML_SetElementHandler(parser, Start, End);
170 XML_SetUserData(parser, &cp);
171
172 int ret = 0;
173 if ((ret = nt.Connect()) != st_ok)
174     {
175     errorMsg = nt.GetError();
176     return ret;
177     }
178 if ((ret = nt.Transact(request.c_str())) != st_ok)
179     {
180     errorMsg = nt.GetError();
181     return ret;
182     }
183 if ((ret = nt.Disconnect()) != st_ok)
184     {
185     errorMsg = nt.GetError();
186     return ret;
187     }
188
189 return st_ok;
190 }