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