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(m_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, m_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, m_encoding));
88 const std::string& Encoding() const { return m_encoding; }
93 std::string m_encoding;
94 std::string m_errorMsg;
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->m_parser, chunk.c_str(), chunk.length(), last) == XML_STATUS_ERROR)
108 strprintf(&sc->m_errorMsg, "XML parse error at line %d, %d: %s. Is last: %d",
109 static_cast<int>(XML_GetCurrentLineNumber(sc->m_parser)),
110 static_cast<int>(XML_GetCurrentColumnNumber(sc->m_parser)),
111 XML_ErrorString(XML_GetErrorCode(sc->m_parser)), static_cast<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 : m_impl(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 : m_impl(new Impl(server, port, localAddress, localPort, login, password))
137 ServConf::~ServConf()
142 int ServConf::ServerInfo(ServerInfo::Callback f, void* data)
144 return m_impl->Exec<ServerInfo::Parser>("<GetServerInfo/>", f, data);
147 int ServConf::RawXML(const std::string& request, RawXML::Callback f, void* data)
149 return m_impl->RawXML(request, f, data);
154 int ServConf::GetAdmins(GetContainer::Callback<GetAdmin::Info>::Type f, void* data)
156 return m_impl->Exec<GetContainer::Parser<GetAdmin::Parser> >("admins", "<GetAdmins/>", f, data);
159 int ServConf::GetAdmin(const std::string& login, GetAdmin::Callback f, void* data)
161 return m_impl->Exec<GetAdmin::Parser>("<GetAdmin login=\"" + login + "\"/>", f, data);
164 int ServConf::ChgAdmin(const AdminConfOpt& conf, Simple::Callback f, void* data)
166 return m_impl->Exec<Simple::Parser>("ChgAdmin", "<ChgAdmin" + ChgAdmin::serialize(conf, m_impl->Encoding()) + "/>", f, data);
169 int ServConf::AddAdmin(const std::string& login,
170 const AdminConfOpt& conf,
171 Simple::Callback f, void* data)
173 auto res = m_impl->Exec<Simple::Parser>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
176 return m_impl->Exec<Simple::Parser>("ChgAdmin", "<ChgAdmin" + ChgAdmin::serialize(conf, m_impl->Encoding()) + "/>", f, data);
179 int ServConf::DelAdmin(const std::string& login, Simple::Callback f, void* data)
181 return m_impl->Exec<Simple::Parser>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
186 int ServConf::GetTariffs(GetContainer::Callback<GetTariff::Info>::Type f, void* data)
188 return m_impl->Exec<GetContainer::Parser<GetTariff::Parser> >("tariffs", "<GetTariffs/>", f, data);
191 int ServConf::GetTariff(const std::string& name, GetTariff::Callback f, void* data)
193 return m_impl->Exec<GetTariff::Parser>("<GetTariff name=\"" + name + "\"/>", f, data);
196 int ServConf::ChgTariff(const TariffDataOpt& tariffData, Simple::Callback f, void* data)
198 return m_impl->Exec<Simple::Parser>("SetTariff", "<SetTariff name=\"" + tariffData.tariffConf.name.value() + "\">" + ChgTariff::serialize(tariffData, m_impl->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 = m_impl->Exec<Simple::Parser>("AddTariff", "<AddTariff name=\"" + name + "\"/>", f, data);
208 return m_impl->Exec<Simple::Parser>("SetTariff", "<SetTariff name=\"" + name + "\">" + ChgTariff::serialize(tariffData, m_impl->Encoding()) + "</SetTariff>", f, data);
211 int ServConf::DelTariff(const std::string& name, Simple::Callback f, void* data)
213 return m_impl->Exec<Simple::Parser>("DelTariff", "<DelTariff name=\"" + name + "\"/>", f, data);
218 int ServConf::GetUsers(GetContainer::Callback<GetUser::Info>::Type f, void* data)
220 return m_impl->Exec<GetContainer::Parser<GetUser::Parser> >("users", "<GetUsers/>", f, data);
223 int ServConf::GetUser(const std::string& login, GetUser::Callback f, void* data)
225 return m_impl->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 m_impl->Exec<ChgUser::Parser>("<SetUser><Login value=\"" + login + "\"/>" + ChgUser::serialize(conf, stat, m_impl->Encoding()) + "</SetUser>", f, data);
236 int ServConf::DelUser(const std::string& login, Simple::Callback f, void* data)
238 return m_impl->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 = m_impl->Exec<Simple::Parser>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
249 return m_impl->Exec<ChgUser::Parser>("<SetUser><Login value=\"" + login + "\"/>" + ChgUser::serialize(conf, stat, m_impl->Encoding()) + "</SetUser>", f, data);
252 int ServConf::AuthBy(const std::string& login, AuthBy::Callback f, void* data)
254 return m_impl->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 m_impl->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 m_impl->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 m_impl->Exec<GetContainer::Parser<GetService::Parser> >("services", "<GetServices/>", f, data);
274 int ServConf::GetService(const std::string& name, GetService::Callback f, void* data)
276 return m_impl->Exec<GetService::Parser>("<GetService name=\"" + name + "\"/>", f, data);
279 int ServConf::ChgService(const ServiceConfOpt& conf, Simple::Callback f, void* data)
281 return m_impl->Exec<Simple::Parser>("SetService", "<SetService " + ChgService::serialize(conf, m_impl->Encoding()) + "/>", f, data);
284 int ServConf::AddService(const std::string& name,
285 const ServiceConfOpt& conf,
286 Simple::Callback f, void* data)
288 auto res = m_impl->Exec<Simple::Parser>("AddService", "<AddService name=\"" + name + "\"/>", f, data);
291 return m_impl->Exec<Simple::Parser>("SetService", "<SetService " + ChgService::serialize(conf, m_impl->Encoding()) + "/>", f, data);
294 int ServConf::DelService(const std::string& name, Simple::Callback f, void* data)
296 return m_impl->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 m_impl->Exec<GetContainer::Parser<GetCorp::Parser> >("corporations", "<GetCorporations/>", f, data);
306 int ServConf::GetCorp(const std::string& name, GetCorp::Callback f, void* data)
308 return m_impl->Exec<GetCorp::Parser>("<GetCorp name=\"" + name + "\"/>", f, data);
311 int ServConf::ChgCorp(const CorpConfOpt & conf, Simple::Callback f, void* data)
313 return m_impl->Exec<Simple::Parser>("SetCorp", "<SetCorp name=\"" + conf.name.value() + "\">" + ChgCorp::serialize(conf, m_impl->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 = m_impl->Exec<Simple::Parser>("AddCorp", "<AddCorp name=\"" + name + "\"/>", f, data);
323 return m_impl->Exec<Simple::Parser>("SetCorp", "<SetCorp name=\"" + name + "\">" + ChgCorp::serialize(conf, m_impl->Encoding()) + "</SetCorp>", f, data);
326 int ServConf::DelCorp(const std::string& name, Simple::Callback f, void* data)
328 return m_impl->Exec<Simple::Parser>("DelCorp", "<DelCorp name=\"" + name + "\"/>", f, data);
331 const std::string& ServConf::GetStrError() const
333 return m_impl->GetStrError();
336 //-----------------------------------------------------------------------------
337 ServConf::Impl::Impl(const std::string& server, uint16_t port,
338 const std::string& login, const std::string& password)
339 : m_nt(server, port, login, password)
341 setlocale(LC_ALL, "");
342 setlocale(LC_NUMERIC, "C");
343 m_encoding = nl_langinfo(CODESET);
344 m_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 : m_nt(server, port, localAddress, localPort, login, password)
352 setlocale(LC_ALL, "");
353 setlocale(LC_NUMERIC, "C");
354 m_encoding = nl_langinfo(CODESET);
355 m_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(m_parser, NULL);
376 XML_SetElementHandler(m_parser, Start, End);
377 XML_SetUserData(m_parser, &cp);
380 if ((ret = m_nt.Connect()) != st_ok)
382 m_errorMsg = m_nt.GetError();
383 cp.Failure(m_errorMsg);
386 if ((ret = m_nt.Transact(request, ParserRecv, this)) != st_ok)
388 m_errorMsg = m_nt.GetError();
389 cp.Failure(m_errorMsg);
397 int ServConf::Impl::RawXML(const std::string& request, RawXML::Callback callback, void* data)
400 if ((ret = m_nt.Connect()) != st_ok)
402 m_errorMsg = m_nt.GetError();
403 callback(false, m_errorMsg, "", data);
406 std::string response;
407 if ((ret = m_nt.Transact(request, SimpleRecv, &response)) != st_ok)
409 m_errorMsg = m_nt.GetError();
410 callback(false, m_errorMsg, "", data);
415 callback(true, "", response, data);