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