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