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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "stg/servconf.h"
26 #include "parsers/simple.h"
27 #include "parsers/get_container.h"
29 #include "parsers/server_info.h"
31 #include "parsers/get_admin.h"
32 #include "parsers/chg_admin.h"
34 #include "parsers/get_tariff.h"
35 #include "parsers/chg_tariff.h"
37 #include "parsers/auth_by.h"
38 #include "parsers/get_user.h"
39 #include "parsers/chg_user.h"
41 #include "parsers/get_service.h"
42 #include "parsers/chg_service.h"
44 #include "parsers/get_corp.h"
45 #include "parsers/chg_corp.h"
47 #include "parsers/base.h"
49 #include "stg/common.h"
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); }
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);
74 int RawXML(const std::string& request, RawXML::Callback f, void* data);
76 template <class P, typename C>
77 int Exec(const std::string& request, C callback, void* data)
79 return ExecImpl(request, P(callback, data, encoding));
82 template <class P, typename C>
83 int Exec(const std::string& tag, const std::string& request, C callback, void* data)
85 return ExecImpl(request, P(tag, callback, data, encoding));
88 const std::string& Encoding() const { return encoding; }
97 static bool ParserRecv(const std::string& chunk, bool last, void* data);
98 static bool SimpleRecv(const std::string& chunk, bool last, void* data);
99 int ExecImpl(const std::string& request, Parser&& cp);
102 bool ServConf::Impl::ParserRecv(const std::string& chunk, bool last, void* data)
104 auto sc = static_cast<ServConf::Impl*>(data);
106 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), last) == XML_STATUS_ERROR)
108 strprintf(&sc->errorMsg, "XML parse error at line %d, %d: %s. Is last: %d",
109 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
110 static_cast<int>(XML_GetCurrentColumnNumber(sc->parser)),
111 XML_ErrorString(XML_GetErrorCode(sc->parser)), (int)last);
118 bool ServConf::Impl::SimpleRecv(const std::string& chunk, bool /*last*/, void* data)
120 *static_cast<std::string*>(data) += chunk;
124 ServConf::ServConf(const std::string& server, uint16_t port,
125 const std::string& login, const std::string& password)
126 : pImpl(new Impl(server, port, login, password))
130 ServConf::ServConf(const std::string& server, uint16_t port,
131 const std::string& localAddress, uint16_t localPort,
132 const std::string& login, const std::string& password)
133 : pImpl(new Impl(server, port, localAddress, localPort, login, password))
137 ServConf::~ServConf()
142 int ServConf::ServerInfo(ServerInfo::Callback f, void* data)
144 return pImpl->Exec<ServerInfo::Parser>("<GetServerInfo/>", f, data);
147 int ServConf::RawXML(const std::string& request, RawXML::Callback f, void* data)
149 return pImpl->RawXML(request, f, data);
154 int ServConf::GetAdmins(GetContainer::Callback<GetAdmin::Info>::Type f, void* data)
156 return pImpl->Exec<GetContainer::Parser<GetAdmin::Parser> >("admins", "<GetAdmins/>", f, data);
159 int ServConf::GetAdmin(const std::string& login, GetAdmin::Callback f, void* data)
161 return pImpl->Exec<GetAdmin::Parser>("<GetAdmin login=\"" + login + "\"/>", f, data);
164 int ServConf::ChgAdmin(const AdminConfOpt& conf, Simple::Callback f, void* data)
166 return pImpl->Exec<Simple::Parser>("ChgAdmin", "<ChgAdmin" + ChgAdmin::serialize(conf, pImpl->Encoding()) + "/>", f, data);
169 int ServConf::AddAdmin(const std::string& login,
170 const AdminConfOpt& conf,
171 Simple::Callback f, void* data)
173 auto res = pImpl->Exec<Simple::Parser>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
176 return pImpl->Exec<Simple::Parser>("ChgAdmin", "<ChgAdmin" + ChgAdmin::serialize(conf, pImpl->Encoding()) + "/>", f, data);
179 int ServConf::DelAdmin(const std::string& login, Simple::Callback f, void* data)
181 return pImpl->Exec<Simple::Parser>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
186 int ServConf::GetTariffs(GetContainer::Callback<GetTariff::Info>::Type f, void* data)
188 return pImpl->Exec<GetContainer::Parser<GetTariff::Parser> >("tariffs", "<GetTariffs/>", f, data);
191 int ServConf::GetTariff(const std::string& name, GetTariff::Callback f, void* data)
193 return pImpl->Exec<GetTariff::Parser>("<GetTariff name=\"" + name + "\"/>", f, data);
196 int ServConf::ChgTariff(const TariffDataOpt& tariffData, Simple::Callback f, void* data)
198 return pImpl->Exec<Simple::Parser>("SetTariff", "<SetTariff name=\"" + tariffData.tariffConf.name.data() + "\">" + ChgTariff::serialize(tariffData, pImpl->Encoding()) + "</SetTariff>", f, data);
201 int ServConf::AddTariff(const std::string& name,
202 const TariffDataOpt& tariffData,
203 Simple::Callback f, void* data)
205 auto res = pImpl->Exec<Simple::Parser>("AddTariff", "<AddTariff name=\"" + name + "\"/>", f, data);
208 return pImpl->Exec<Simple::Parser>("SetTariff", "<SetTariff name=\"" + name + "\">" + ChgTariff::serialize(tariffData, pImpl->Encoding()) + "</SetTariff>", f, data);
211 int ServConf::DelTariff(const std::string& name, Simple::Callback f, void* data)
213 return pImpl->Exec<Simple::Parser>("DelTariff", "<DelTariff name=\"" + name + "\"/>", f, data);
218 int ServConf::GetUsers(GetContainer::Callback<GetUser::Info>::Type f, void* data)
220 return pImpl->Exec<GetContainer::Parser<GetUser::Parser> >("users", "<GetUsers/>", f, data);
223 int ServConf::GetUser(const std::string& login, GetUser::Callback f, void* data)
225 return pImpl->Exec<GetUser::Parser>("<GetUser login=\"" + login + "\"/>", f, data);
228 int ServConf::ChgUser(const std::string& login,
229 const UserConfOpt& conf,
230 const UserStatOpt& stat,
231 Simple::Callback f, void* data)
233 return pImpl->Exec<ChgUser::Parser>("<SetUser><Login value=\"" + login + "\"/>" + ChgUser::serialize(conf, stat, pImpl->Encoding()) + "</SetUser>", f, data);
236 int ServConf::DelUser(const std::string& login, Simple::Callback f, void* data)
238 return pImpl->Exec<Simple::Parser>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
241 int ServConf::AddUser(const std::string& login,
242 const UserConfOpt& conf,
243 const UserStatOpt& stat,
244 Simple::Callback f, void* data)
246 auto res = pImpl->Exec<Simple::Parser>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
249 return pImpl->Exec<ChgUser::Parser>("<SetUser><Login value=\"" + login + "\"/>" + ChgUser::serialize(conf, stat, pImpl->Encoding()) + "</SetUser>", f, data);
252 int ServConf::AuthBy(const std::string& login, AuthBy::Callback f, void* data)
254 return pImpl->Exec<AuthBy::Parser>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
257 int ServConf::SendMessage(const std::string& login, const std::string& text, Simple::Callback f, void* data)
259 return pImpl->Exec<Simple::Parser>("SendMessageResult", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
262 int ServConf::CheckUser(const std::string& login, const std::string& password, Simple::Callback f, void* data)
264 return pImpl->Exec<Simple::Parser>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
269 int ServConf::GetServices(GetContainer::Callback<GetService::Info>::Type f, void* data)
271 return pImpl->Exec<GetContainer::Parser<GetService::Parser> >("services", "<GetServices/>", f, data);
274 int ServConf::GetService(const std::string& name, GetService::Callback f, void* data)
276 return pImpl->Exec<GetService::Parser>("<GetService name=\"" + name + "\"/>", f, data);
279 int ServConf::ChgService(const ServiceConfOpt& conf, Simple::Callback f, void* data)
281 return pImpl->Exec<Simple::Parser>("SetService", "<SetService " + ChgService::serialize(conf, pImpl->Encoding()) + "/>", f, data);
284 int ServConf::AddService(const std::string& name,
285 const ServiceConfOpt& conf,
286 Simple::Callback f, void* data)
288 auto res = pImpl->Exec<Simple::Parser>("AddService", "<AddService name=\"" + name + "\"/>", f, data);
291 return pImpl->Exec<Simple::Parser>("SetService", "<SetService " + ChgService::serialize(conf, pImpl->Encoding()) + "/>", f, data);
294 int ServConf::DelService(const std::string& name, Simple::Callback f, void* data)
296 return pImpl->Exec<Simple::Parser>("DelService", "<DelService name=\"" + name + "\"/>", f, data);
299 // -- Corporations --
301 int ServConf::GetCorporations(GetContainer::Callback<GetCorp::Info>::Type f, void* data)
303 return pImpl->Exec<GetContainer::Parser<GetCorp::Parser> >("corporations", "<GetCorporations/>", f, data);
306 int ServConf::GetCorp(const std::string& name, GetCorp::Callback f, void* data)
308 return pImpl->Exec<GetCorp::Parser>("<GetCorp name=\"" + name + "\"/>", f, data);
311 int ServConf::ChgCorp(const CorpConfOpt & conf, Simple::Callback f, void* data)
313 return pImpl->Exec<Simple::Parser>("SetCorp", "<SetCorp name=\"" + conf.name.data() + "\">" + ChgCorp::serialize(conf, pImpl->Encoding()) + "</SetCorp>", f, data);
316 int ServConf::AddCorp(const std::string& name,
317 const CorpConfOpt& conf,
318 Simple::Callback f, void* data)
320 auto res = pImpl->Exec<Simple::Parser>("AddCorp", "<AddCorp name=\"" + name + "\"/>", f, data);
323 return pImpl->Exec<Simple::Parser>("SetCorp", "<SetCorp name=\"" + name + "\">" + ChgCorp::serialize(conf, pImpl->Encoding()) + "</SetCorp>", f, data);
326 int ServConf::DelCorp(const std::string& name, Simple::Callback f, void* data)
328 return pImpl->Exec<Simple::Parser>("DelCorp", "<DelCorp name=\"" + name + "\"/>", f, data);
331 const std::string& ServConf::GetStrError() const
333 return pImpl->GetStrError();
336 //-----------------------------------------------------------------------------
337 ServConf::Impl::Impl(const std::string& server, uint16_t port,
338 const std::string& login, const std::string& password)
339 : nt(server, port, login, password)
341 setlocale(LC_ALL, "");
342 setlocale(LC_NUMERIC, "C");
343 encoding = nl_langinfo(CODESET);
344 parser = XML_ParserCreate(NULL);
346 //-----------------------------------------------------------------------------
347 ServConf::Impl::Impl(const std::string& server, uint16_t port,
348 const std::string& localAddress, uint16_t localPort,
349 const std::string& login, const std::string& password)
350 : nt(server, port, localAddress, localPort, login, password)
352 setlocale(LC_ALL, "");
353 setlocale(LC_NUMERIC, "C");
354 encoding = nl_langinfo(CODESET);
355 parser = XML_ParserCreate(NULL);
357 //-----------------------------------------------------------------------------
358 void ServConf::Impl::Start(void* data, const char* el, const char** attr)
360 static_cast<Parser*>(data)->ParseStart(el, attr);
362 //-----------------------------------------------------------------------------
363 void ServConf::Impl::End(void* data, const char* el)
365 static_cast<Parser*>(data)->ParseEnd(el);
367 //-----------------------------------------------------------------------------
368 const std::string & ServConf::Impl::GetStrError() const
372 //-----------------------------------------------------------------------------
373 int ServConf::Impl::ExecImpl(const std::string& request, Parser&& cp)
375 XML_ParserReset(parser, NULL);
376 XML_SetElementHandler(parser, Start, End);
377 XML_SetUserData(parser, &cp);
380 if ((ret = nt.Connect()) != st_ok)
382 errorMsg = nt.GetError();
383 cp.Failure(errorMsg);
386 if ((ret = nt.Transact(request, ParserRecv, this)) != st_ok)
388 errorMsg = nt.GetError();
389 cp.Failure(errorMsg);
397 int ServConf::Impl::RawXML(const std::string& request, RawXML::Callback callback, void* data)
400 if ((ret = nt.Connect()) != st_ok)
402 errorMsg = nt.GetError();
403 callback(false, errorMsg, "", data);
406 std::string response;
407 if ((ret = nt.Transact(request, SimpleRecv, &response)) != st_ok)
409 errorMsg = nt.GetError();
410 callback(false, errorMsg, "", data);
415 callback(true, "", response, data);