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