]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Moved SERVER_INFO parser from global scope.
[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 "parser_auth_by.h"
25 #include "parser_server_info.h"
26
27 #include "stg/common.h"
28
29 #include <cstdio>
30 #include <cstring>
31
32 #include <expat.h>
33
34 using namespace STG;
35
36 class SERVCONF::IMPL
37 {
38 public:
39     IMPL(const std::string & server, uint16_t port,
40          const std::string & login, const std::string & password);
41
42     int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data);
43     int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data);
44     int ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data);
45     int AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data);
46     int SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data);
47     int ServerInfo(SERVER_INFO::CALLBACK f, void * data);
48     int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::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 private:
55     PARSER_GET_USERS parserGetUsers;
56     PARSER_GET_USER parserGetUser;
57     AUTH_BY::PARSER parserAuthBy;
58     SERVER_INFO::PARSER  parserServerInfo;
59     PARSER_CHG_USER parserChgUser;
60     PARSER_CHECK_USER parserCheckUser;
61     PARSER_SEND_MESSAGE parserSendMessage;
62
63     NETTRANSACT nt;
64
65     std::string errorMsg;
66     XML_Parser parser;
67
68     int Exec(const std::string & request, PARSER & cp);
69
70     static bool AnsRecv(void * data, const std::string & chunk, bool final);
71 };
72
73 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
74 {
75 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
76
77 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
78     {
79     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
80               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
81               XML_ErrorString(XML_GetErrorCode(sc->parser)));
82     printf("%s\n", sc->errorMsg.c_str());
83     return false;
84     }
85
86 return true;
87 }
88
89 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
90                    const std::string & login, const std::string & password)
91     : pImpl(new IMPL(server, port, login, password))
92 {
93 }
94
95 SERVCONF::~SERVCONF()
96 {
97     delete pImpl;
98 }
99
100 int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
101 {
102     return pImpl->GetUsers( f, data );
103 }
104
105 int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
106 {
107     return pImpl->GetUser(login, f, data);
108 }
109
110 int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
111 {
112     return pImpl->ChgUser(request, f, data);
113 }
114
115 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
116 {
117     return pImpl->AuthBy(login, f, data);
118 }
119
120 int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
121 {
122     return pImpl->SendMessage(request, f, data);
123 }
124
125 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
126 {
127     return pImpl->ServerInfo(f, data);
128 }
129
130 int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
131 {
132     return pImpl->CheckUser(login, password, f, data);
133 }
134
135 const std::string & SERVCONF::GetStrError() const
136 {
137     return pImpl->GetStrError();
138 }
139
140 //-----------------------------------------------------------------------------
141 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
142                      const std::string & login, const std::string & password)
143     : nt( server, port, login, password )
144 {
145 parser = XML_ParserCreate(NULL);
146 nt.SetRxCallback(this, AnsRecv);
147 }
148 //-----------------------------------------------------------------------------
149 int SERVCONF::IMPL::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
150 {
151 parserGetUser.SetCallback(f, data);
152 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
153 }
154 //-----------------------------------------------------------------------------
155 int SERVCONF::IMPL::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
156 {
157 parserAuthBy.SetCallback(f, data);
158 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
159 }
160 //-----------------------------------------------------------------------------
161 int SERVCONF::IMPL::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
162 {
163 parserGetUsers.SetCallback(f, data);
164 return Exec("<GetUsers/>", parserGetUsers);
165 }
166 //-----------------------------------------------------------------------------
167 int SERVCONF::IMPL::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
168 {
169 parserServerInfo.SetCallback(f, data);
170 return Exec("<GetServerInfo/>", parserServerInfo);
171 }
172 //-----------------------------------------------------------------------------
173 int SERVCONF::IMPL::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
174 {
175 parserChgUser.SetCallback(f, data);
176 return Exec(request, parserChgUser);
177 }
178 //-----------------------------------------------------------------------------
179 int SERVCONF::IMPL::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
180 {
181 parserSendMessage.SetCallback(f, data);
182 return Exec(request, parserSendMessage);
183 }
184 //-----------------------------------------------------------------------------
185 int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
186 {
187 parserCheckUser.SetCallback(f, data);
188 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
189 }
190 //-----------------------------------------------------------------------------
191 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
192 {
193 PARSER * currParser = static_cast<PARSER *>(data);
194 currParser->ParseStart(el, attr);
195 }
196 //-----------------------------------------------------------------------------
197 void SERVCONF::IMPL::End(void * data, const char * el)
198 {
199 PARSER * currParser = static_cast<PARSER *>(data);
200 currParser->ParseEnd(el);
201 }
202 //-----------------------------------------------------------------------------
203 const std::string & SERVCONF::IMPL::GetStrError() const
204 {
205 return errorMsg;
206 }
207 //-----------------------------------------------------------------------------
208 int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp)
209 {
210 XML_ParserReset(parser, NULL);
211 XML_SetElementHandler(parser, Start, End);
212 XML_SetUserData(parser, &cp);
213
214 int ret = 0;
215 if ((ret = nt.Connect()) != st_ok)
216     {
217     errorMsg = nt.GetError();
218     return ret;
219     }
220 if ((ret = nt.Transact(request.c_str())) != st_ok)
221     {
222     errorMsg = nt.GetError();
223     return ret;
224     }
225 if ((ret = nt.Disconnect()) != st_ok)
226     {
227     errorMsg = nt.GetError();
228     return ret;
229     }
230
231 return st_ok;
232 }