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"
61 IMPL(const std::string & server, uint16_t port,
62 const std::string & login, const std::string & password);
63 ~IMPL() { XML_ParserFree(parser); }
65 const std::string & GetStrError() const;
66 static void Start(void * data, const char * el, const char ** attr);
67 static void End(void * data, const char * el);
69 int RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data);
71 template <class P, typename C>
72 int Exec(const std::string & request, C callback, void * data)
75 return ExecImpl(request, cp);
78 template <class P, typename C>
79 int Exec(const std::string & tag, const std::string & request, C callback, void * data)
81 P cp(tag, callback, data);
82 return ExecImpl(request, cp);
91 static bool ParserRecv(const std::string & chunk, bool final, void * data);
92 static bool SimpleRecv(const std::string & chunk, bool final, void * data);
93 int ExecImpl(const std::string & request, PARSER & cp);
96 bool SERVCONF::IMPL::ParserRecv(const std::string & chunk, bool final, void * data)
98 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
100 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
102 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
103 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
104 XML_ErrorString(XML_GetErrorCode(sc->parser)));
105 printf("%s\n", sc->errorMsg.c_str());
112 bool SERVCONF::IMPL::SimpleRecv(const std::string & chunk, bool /*final*/, void * data)
114 *static_cast<std::string *>(data) += chunk;
118 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
119 const std::string & login, const std::string & password)
120 : pImpl(new IMPL(server, port, login, password))
124 SERVCONF::~SERVCONF()
129 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
131 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
134 int SERVCONF::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
136 return pImpl->RawXML(request, f, data);
141 int SERVCONF::GetAdmins(GET_CONTAINER::CALLBACK<GET_ADMIN::INFO>::TYPE f, void * data)
143 return pImpl->Exec<GET_CONTAINER::PARSER<GET_ADMIN::PARSER> >("admins", "<GetAdmins/>", f, data);
146 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
148 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
151 int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
153 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
156 int SERVCONF::AddAdmin(const std::string & login,
157 const ADMIN_CONF_RES & conf,
158 SIMPLE::CALLBACK f, void * data)
160 int res = pImpl->Exec<SIMPLE::PARSER>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
163 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
166 int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data)
168 return pImpl->Exec<SIMPLE::PARSER>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
173 int SERVCONF::GetTariffs(GET_CONTAINER::CALLBACK<GET_TARIFF::INFO>::TYPE f, void * data)
175 return pImpl->Exec<GET_CONTAINER::PARSER<GET_TARIFF::PARSER> >("tariffs", "<GetTariffs/>", f, data);
178 int SERVCONF::GetTariff(const std::string & name, GET_TARIFF::CALLBACK f, void * data)
180 return pImpl->Exec<GET_TARIFF::PARSER>("<GetTariff name=\"" + name + "\"/>", f, data);
183 int SERVCONF::ChgTariff(const TARIFF_DATA_RES & tariffData, SIMPLE::CALLBACK f, void * data)
185 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + tariffData.tariffConf.name.data() + "\">" + CHG_TARIFF::Serialize(tariffData) + "</SetTariff>", f, data);
188 int SERVCONF::AddTariff(const std::string & name,
189 const TARIFF_DATA_RES & tariffData,
190 SIMPLE::CALLBACK f, void * data)
192 int res = pImpl->Exec<SIMPLE::PARSER>("AddTariff", "<AddTariff name=\"" + name + "\"/>", f, data);
195 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + name + "\">" + CHG_TARIFF::Serialize(tariffData) + "</SetTariff>", f, data);
198 int SERVCONF::DelTariff(const std::string & name, SIMPLE::CALLBACK f, void * data)
200 return pImpl->Exec<SIMPLE::PARSER>("DelTariff", "<DelTariff name=\"" + name + "\"/>", f, data);
205 int SERVCONF::GetUsers(GET_CONTAINER::CALLBACK<GET_USER::INFO>::TYPE f, void * data)
207 return pImpl->Exec<GET_CONTAINER::PARSER<GET_USER::PARSER> >("users", "<GetUsers/>", f, data);
210 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
212 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
215 int SERVCONF::ChgUser(const std::string & login,
216 const USER_CONF_RES & conf,
217 const USER_STAT_RES & stat,
218 SIMPLE::CALLBACK f, void * data)
220 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat) + "</SetUser>", f, data);
223 int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
225 return pImpl->Exec<SIMPLE::PARSER>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
228 int SERVCONF::AddUser(const std::string & login,
229 const USER_CONF_RES & conf,
230 const USER_STAT_RES & stat,
231 SIMPLE::CALLBACK f, void * data)
233 int res = pImpl->Exec<SIMPLE::PARSER>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
236 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat) + "</SetUser>", f, data);
239 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
241 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
244 int SERVCONF::SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data)
246 return pImpl->Exec<SIMPLE::PARSER>("SendMessageResult", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
249 int SERVCONF::CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data)
251 return pImpl->Exec<SIMPLE::PARSER>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
256 int SERVCONF::GetServices(GET_CONTAINER::CALLBACK<GET_SERVICE::INFO>::TYPE f, void * data)
258 return pImpl->Exec<GET_CONTAINER::PARSER<GET_SERVICE::PARSER> >("services", "<GetServices/>", f, data);
261 int SERVCONF::GetService(const std::string & name, GET_SERVICE::CALLBACK f, void * data)
263 return pImpl->Exec<GET_SERVICE::PARSER>("<GetService name=\"" + name + "\"/>", f, data);
266 int SERVCONF::ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
268 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + conf.name.data() + "\">" + CHG_SERVICE::Serialize(conf) + "</SetService>", f, data);
271 int SERVCONF::AddService(const std::string & name,
272 const SERVICE_CONF_RES & conf,
273 SIMPLE::CALLBACK f, void * data)
275 int res = pImpl->Exec<SIMPLE::PARSER>("AddService", "<AddService name=\"" + name + "\"/>", f, data);
278 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + name + "\">" + CHG_SERVICE::Serialize(conf) + "</SetService>", f, data);
281 int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * data)
283 return pImpl->Exec<SIMPLE::PARSER>("DelService", "<DelService name=\"" + name + "\"/>", f, data);
286 // -- Corporations --
288 int SERVCONF::GetCorporations(GET_CONTAINER::CALLBACK<GET_CORP::INFO>::TYPE f, void * data)
290 return pImpl->Exec<GET_CONTAINER::PARSER<GET_CORP::PARSER> >("corporations", "<GetCorporations/>", f, data);
293 int SERVCONF::GetCorp(const std::string & name, GET_CORP::CALLBACK f, void * data)
295 return pImpl->Exec<GET_CORP::PARSER>("<GetCorp name=\"" + name + "\"/>", f, data);
298 int SERVCONF::ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
300 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + conf.name.data() + "\">" + CHG_CORP::Serialize(conf) + "</SetCorp>", f, data);
303 int SERVCONF::AddCorp(const std::string & name,
304 const CORP_CONF_RES & conf,
305 SIMPLE::CALLBACK f, void * data)
307 int res = pImpl->Exec<SIMPLE::PARSER>("AddCorp", "<AddCorp name=\"" + name + "\"/>", f, data);
310 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + name + "\">" + CHG_CORP::Serialize(conf) + "</SetCorp>", f, data);
313 int SERVCONF::DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data)
315 return pImpl->Exec<SIMPLE::PARSER>("DelCorp", "<DelCorp name=\"" + name + "\"/>", f, data);
318 const std::string & SERVCONF::GetStrError() const
320 return pImpl->GetStrError();
323 //-----------------------------------------------------------------------------
324 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
325 const std::string & login, const std::string & password)
326 : nt( server, port, login, password )
328 parser = XML_ParserCreate(NULL);
330 //-----------------------------------------------------------------------------
331 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
333 PARSER * currParser = static_cast<PARSER *>(data);
334 currParser->ParseStart(el, attr);
336 //-----------------------------------------------------------------------------
337 void SERVCONF::IMPL::End(void * data, const char * el)
339 PARSER * currParser = static_cast<PARSER *>(data);
340 currParser->ParseEnd(el);
342 //-----------------------------------------------------------------------------
343 const std::string & SERVCONF::IMPL::GetStrError() const
347 //-----------------------------------------------------------------------------
348 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
350 XML_ParserReset(parser, NULL);
351 XML_SetElementHandler(parser, Start, End);
352 XML_SetUserData(parser, &cp);
355 if ((ret = nt.Connect()) != st_ok)
357 errorMsg = nt.GetError();
358 cp.Failure(errorMsg);
361 if ((ret = nt.Transact(request, ParserRecv, this)) != st_ok)
363 errorMsg = nt.GetError();
364 cp.Failure(errorMsg);
372 int SERVCONF::IMPL::RawXML(const std::string & request, RAW_XML::CALLBACK callback, void * data)
375 if ((ret = nt.Connect()) != st_ok)
377 errorMsg = nt.GetError();
378 callback(false, errorMsg, "", data);
381 std::string response;
382 if ((ret = nt.Transact(request, SimpleRecv, &response)) != st_ok)
384 errorMsg = nt.GetError();
385 callback(false, errorMsg, "", data);
390 callback(true, "", response, data);