]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Added local binding.
[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  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "stg/servconf.h"
23
24 #include "netunit.h"
25
26 #include "parsers/simple.h"
27 #include "parsers/get_container.h"
28
29 #include "parsers/server_info.h"
30
31 #include "parsers/get_admin.h"
32 #include "parsers/chg_admin.h"
33
34 #include "parsers/get_tariff.h"
35 #include "parsers/chg_tariff.h"
36
37 #include "parsers/auth_by.h"
38 #include "parsers/get_user.h"
39 #include "parsers/chg_user.h"
40
41 #include "parsers/get_service.h"
42 #include "parsers/chg_service.h"
43
44 #include "parsers/get_corp.h"
45 #include "parsers/chg_corp.h"
46
47 #include "parsers/base.h"
48
49 #include "stg/common.h"
50
51 #include <cstdio>
52 #include <cstring>
53
54 #include <expat.h>
55
56 using namespace STG;
57
58 class SERVCONF::IMPL
59 {
60 public:
61     IMPL(const std::string & server, uint16_t port,
62          const std::string & login, const std::string & password);
63     IMPL(const std::string & server, uint16_t port,
64          const std::string & localAddress, uint16_t localPort,
65          const std::string & login, const std::string & password);
66     ~IMPL() { XML_ParserFree(parser); }
67
68     const std::string & GetStrError() const;
69     static void Start(void * data, const char * el, const char ** attr);
70     static void End(void * data, const char * el);
71
72     int RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data);
73
74     template <class P, typename C>
75     int Exec(const std::string & request, C callback, void * data)
76     {
77         P cp(callback, data);
78         return ExecImpl(request, cp);
79     }
80
81     template <class P, typename C>
82     int Exec(const std::string & tag, const std::string & request, C callback, void * data)
83     {
84         P cp(tag, callback, data);
85         return ExecImpl(request, cp);
86     }
87
88 private:
89     NETTRANSACT nt;
90
91     std::string errorMsg;
92     XML_Parser parser;
93
94     static bool ParserRecv(const std::string & chunk, bool final, void * data);
95     static bool SimpleRecv(const std::string & chunk, bool final, void * data);
96     int ExecImpl(const std::string & request, PARSER & cp);
97 };
98
99 bool SERVCONF::IMPL::ParserRecv(const std::string & chunk, bool final, void * data)
100 {
101 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
102
103 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
104     {
105     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
106               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
107               XML_ErrorString(XML_GetErrorCode(sc->parser)));
108     printf("%s\n", sc->errorMsg.c_str());
109     return false;
110     }
111
112 return true;
113 }
114
115 bool SERVCONF::IMPL::SimpleRecv(const std::string & chunk, bool /*final*/, void * data)
116 {
117 *static_cast<std::string *>(data) += chunk;
118 return true;
119 }
120
121 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
122                    const std::string & login, const std::string & password)
123     : pImpl(new IMPL(server, port, login, password))
124 {
125 }
126
127 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
128                    const std::string & localAddress, uint16_t localPort,
129                    const std::string & login, const std::string & password)
130     : pImpl(new IMPL(server, port, localAddress, localPort, login, password))
131 {
132 }
133
134 SERVCONF::~SERVCONF()
135 {
136 delete pImpl;
137 }
138
139 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
140 {
141 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
142 }
143
144 int SERVCONF::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
145 {
146 return pImpl->RawXML(request, f, data);
147 }
148
149 // -- Admins --
150
151 int SERVCONF::GetAdmins(GET_CONTAINER::CALLBACK<GET_ADMIN::INFO>::TYPE f, void * data)
152 {
153 return pImpl->Exec<GET_CONTAINER::PARSER<GET_ADMIN::PARSER> >("admins", "<GetAdmins/>", f, data);
154 }
155
156 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
157 {
158 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
159 }
160
161 int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
162 {
163 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
164 }
165
166 int SERVCONF::AddAdmin(const std::string & login,
167                        const ADMIN_CONF_RES & conf,
168                        SIMPLE::CALLBACK f, void * data)
169 {
170 int res = pImpl->Exec<SIMPLE::PARSER>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
171 if (res != st_ok)
172     return res;
173 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
174 }
175
176 int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data)
177 {
178 return pImpl->Exec<SIMPLE::PARSER>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
179 }
180
181 // -- Tariffs --
182
183 int SERVCONF::GetTariffs(GET_CONTAINER::CALLBACK<GET_TARIFF::INFO>::TYPE f, void * data)
184 {
185 return pImpl->Exec<GET_CONTAINER::PARSER<GET_TARIFF::PARSER> >("tariffs", "<GetTariffs/>", f, data);
186 }
187
188 int SERVCONF::GetTariff(const std::string & name, GET_TARIFF::CALLBACK f, void * data)
189 {
190 return pImpl->Exec<GET_TARIFF::PARSER>("<GetTariff name=\"" + name + "\"/>", f, data);
191 }
192
193 int SERVCONF::ChgTariff(const TARIFF_DATA_RES & tariffData, SIMPLE::CALLBACK f, void * data)
194 {
195 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + tariffData.tariffConf.name.data() + "\">" + CHG_TARIFF::Serialize(tariffData) + "</SetTariff>", f, data);
196 }
197
198 int SERVCONF::AddTariff(const std::string & name,
199                        const TARIFF_DATA_RES & tariffData,
200                        SIMPLE::CALLBACK f, void * data)
201 {
202 int res = pImpl->Exec<SIMPLE::PARSER>("AddTariff", "<AddTariff name=\"" + name + "\"/>", f, data);
203 if (res != st_ok)
204     return res;
205 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + name + "\">" + CHG_TARIFF::Serialize(tariffData) + "</SetTariff>", f, data);
206 }
207
208 int SERVCONF::DelTariff(const std::string & name, SIMPLE::CALLBACK f, void * data)
209 {
210 return pImpl->Exec<SIMPLE::PARSER>("DelTariff", "<DelTariff name=\"" + name + "\"/>", f, data);
211 }
212
213 // -- Users --
214
215 int SERVCONF::GetUsers(GET_CONTAINER::CALLBACK<GET_USER::INFO>::TYPE f, void * data)
216 {
217 return pImpl->Exec<GET_CONTAINER::PARSER<GET_USER::PARSER> >("users", "<GetUsers/>", f, data);
218 }
219
220 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
221 {
222 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
223 }
224
225 int SERVCONF::ChgUser(const std::string & login,
226                       const USER_CONF_RES & conf,
227                       const USER_STAT_RES & stat,
228                       SIMPLE::CALLBACK f, void * data)
229 {
230 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat) + "</SetUser>", f, data);
231 }
232
233 int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
234 {
235 return pImpl->Exec<SIMPLE::PARSER>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
236 }
237
238 int SERVCONF::AddUser(const std::string & login,
239                       const USER_CONF_RES & conf,
240                       const USER_STAT_RES & stat,
241                       SIMPLE::CALLBACK f, void * data)
242 {
243 int res = pImpl->Exec<SIMPLE::PARSER>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
244 if (res != st_ok)
245     return res;
246 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat) + "</SetUser>", f, data);
247 }
248
249 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
250 {
251 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
252 }
253
254 int SERVCONF::SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data)
255 {
256 return pImpl->Exec<SIMPLE::PARSER>("SendMessageResult", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
257 }
258
259 int SERVCONF::CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data)
260 {
261 return pImpl->Exec<SIMPLE::PARSER>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
262 }
263
264 // -- Services --
265
266 int SERVCONF::GetServices(GET_CONTAINER::CALLBACK<GET_SERVICE::INFO>::TYPE f, void * data)
267 {
268 return pImpl->Exec<GET_CONTAINER::PARSER<GET_SERVICE::PARSER> >("services", "<GetServices/>", f, data);
269 }
270
271 int SERVCONF::GetService(const std::string & name, GET_SERVICE::CALLBACK f, void * data)
272 {
273 return pImpl->Exec<GET_SERVICE::PARSER>("<GetService name=\"" + name + "\"/>", f, data);
274 }
275
276 int SERVCONF::ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
277 {
278 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + conf.name.data() + "\">" + CHG_SERVICE::Serialize(conf) + "</SetService>", f, data);
279 }
280
281 int SERVCONF::AddService(const std::string & name,
282                          const SERVICE_CONF_RES & conf,
283                          SIMPLE::CALLBACK f, void * data)
284 {
285 int res = pImpl->Exec<SIMPLE::PARSER>("AddService", "<AddService name=\"" + name + "\"/>", f, data);
286 if (res != st_ok)
287     return res;
288 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + name + "\">" + CHG_SERVICE::Serialize(conf) + "</SetService>", f, data);
289 }
290
291 int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * data)
292 {
293 return pImpl->Exec<SIMPLE::PARSER>("DelService", "<DelService name=\"" + name + "\"/>", f, data);
294 }
295
296 // -- Corporations --
297
298 int SERVCONF::GetCorporations(GET_CONTAINER::CALLBACK<GET_CORP::INFO>::TYPE f, void * data)
299 {
300 return pImpl->Exec<GET_CONTAINER::PARSER<GET_CORP::PARSER> >("corporations", "<GetCorporations/>", f, data);
301 }
302
303 int SERVCONF::GetCorp(const std::string & name, GET_CORP::CALLBACK f, void * data)
304 {
305 return pImpl->Exec<GET_CORP::PARSER>("<GetCorp name=\"" + name + "\"/>", f, data);
306 }
307
308 int SERVCONF::ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
309 {
310 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + conf.name.data() + "\">" + CHG_CORP::Serialize(conf) + "</SetCorp>", f, data);
311 }
312
313 int SERVCONF::AddCorp(const std::string & name,
314                       const CORP_CONF_RES & conf,
315                       SIMPLE::CALLBACK f, void * data)
316 {
317 int res = pImpl->Exec<SIMPLE::PARSER>("AddCorp", "<AddCorp name=\"" + name + "\"/>", f, data);
318 if (res != st_ok)
319     return res;
320 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + name + "\">" + CHG_CORP::Serialize(conf) + "</SetCorp>", f, data);
321 }
322
323 int SERVCONF::DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data)
324 {
325 return pImpl->Exec<SIMPLE::PARSER>("DelCorp", "<DelCorp name=\"" + name + "\"/>", f, data);
326 }
327
328 const std::string & SERVCONF::GetStrError() const
329 {
330 return pImpl->GetStrError();
331 }
332
333 //-----------------------------------------------------------------------------
334 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
335                      const std::string & login, const std::string & password)
336     : nt(server, port, login, password)
337 {
338 parser = XML_ParserCreate(NULL);
339 }
340 //-----------------------------------------------------------------------------
341 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
342                      const std::string & localAddress, uint16_t localPort,
343                      const std::string & login, const std::string & password)
344     : nt(server, port, localAddress, localPort, login, password)
345 {
346 parser = XML_ParserCreate(NULL);
347 }
348 //-----------------------------------------------------------------------------
349 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
350 {
351 PARSER * currParser = static_cast<PARSER *>(data);
352 currParser->ParseStart(el, attr);
353 }
354 //-----------------------------------------------------------------------------
355 void SERVCONF::IMPL::End(void * data, const char * el)
356 {
357 PARSER * currParser = static_cast<PARSER *>(data);
358 currParser->ParseEnd(el);
359 }
360 //-----------------------------------------------------------------------------
361 const std::string & SERVCONF::IMPL::GetStrError() const
362 {
363 return errorMsg;
364 }
365 //-----------------------------------------------------------------------------
366 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
367 {
368 XML_ParserReset(parser, NULL);
369 XML_SetElementHandler(parser, Start, End);
370 XML_SetUserData(parser, &cp);
371
372 int ret = 0;
373 if ((ret = nt.Connect()) != st_ok)
374     {
375     errorMsg = nt.GetError();
376     cp.Failure(errorMsg);
377     return ret;
378     }
379 if ((ret = nt.Transact(request, ParserRecv, this)) != st_ok)
380     {
381     errorMsg = nt.GetError();
382     cp.Failure(errorMsg);
383     return ret;
384     }
385
386 nt.Disconnect();
387 return st_ok;
388 }
389
390 int SERVCONF::IMPL::RawXML(const std::string & request, RAW_XML::CALLBACK callback, void * data)
391 {
392 int ret = 0;
393 if ((ret = nt.Connect()) != st_ok)
394     {
395     errorMsg = nt.GetError();
396     callback(false, errorMsg, "", data);
397     return ret;
398     }
399 std::string response;
400 if ((ret = nt.Transact(request, SimpleRecv, &response)) != st_ok)
401     {
402     errorMsg = nt.GetError();
403     callback(false, errorMsg, "", data);
404     return ret;
405     }
406
407 nt.Disconnect();
408 callback(true, "", response, data);
409 return st_ok;
410 }