From: Maxim Mamontov Date: Tue, 27 May 2014 14:55:56 +0000 (+0300) Subject: Implemented some service and corporation management functions. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/70c7a7e4463c27aa6592225fd3ea5e2bc49f048c Implemented some service and corporation management functions. --- diff --git a/projects/sgconf/Makefile b/projects/sgconf/Makefile index 8117bea6..fd012fc4 100644 --- a/projects/sgconf/Makefile +++ b/projects/sgconf/Makefile @@ -12,6 +12,8 @@ SRCS = ./main.cpp \ ./admins.cpp \ ./tariffs.cpp \ ./users.cpp \ + ./services.cpp \ + ./corps.cpp \ ./xml.cpp STGLIBS = srvconf \ @@ -61,11 +63,7 @@ $(PROG): $(OBJS) $(CXX) $^ $(LDFLAGS) $(LIBS) -o $(PROG) clean: - rm -f deps $(PROG) *.o tags *.*~ .OS - rm -f .OS - rm -f .store - rm -f .db.sql - rm -f core* + rm -f deps $(PROG) *.o $(MAKE) -C $(DIR_LIBSRC) clean distclean: clean diff --git a/projects/sgconf/corps.cpp b/projects/sgconf/corps.cpp new file mode 100644 index 00000000..56293efd --- /dev/null +++ b/projects/sgconf/corps.cpp @@ -0,0 +1,119 @@ +#include "corps.h" + +#include "config.h" + +#include "stg/servconf.h" +#include "stg/servconf_types.h" +#include "stg/corp_conf.h" +#include "stg/common.h" + +#include + +namespace +{ + +std::string Indent(size_t level, bool dash = false) +{ +if (level == 0) + return ""; +return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' '); +} + +void PrintCorp(const STG::GET_CORP::INFO & info, size_t level = 0) +{ +std::cout << Indent(level, true) << "name: " << info.name << "\n" + << Indent(level) << "cash: " << info.cash << "\n"; +} + +void SimpleCallback(bool result, + const std::string & reason, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl; + return; + } +std::cout << "Success.\n"; +} + +void GetCorpsCallback(bool result, + const std::string & reason, + const std::vector & info, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl; + return; + } +std::cout << "Corps:\n"; +for (size_t i = 0; i < info.size(); ++i) + PrintCorp(info[i], 1); +} + +void GetCorpCallback(bool result, + const std::string & reason, + const STG::GET_CORP::INFO & info, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl; + return; + } +PrintCorp(info); +} + +} // namespace anonymous + +bool SGCONF::GetCorpsFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok; +} + +bool SGCONF::GetCorpFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok; +} + +bool SGCONF::DelCorpFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok; +} + +bool SGCONF::AddCorpFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} + +bool SGCONF::ChgCorpFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} diff --git a/projects/sgconf/corps.h b/projects/sgconf/corps.h new file mode 100644 index 00000000..58a65161 --- /dev/null +++ b/projects/sgconf/corps.h @@ -0,0 +1,34 @@ +#ifndef __STG_SGCONF_CORPS_H__ +#define __STG_SGCONF_CORPS_H__ + +#include +#include + +namespace SGCONF +{ + +struct CONFIG; + +bool GetCorpsFunction(const CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/); + +bool GetCorpFunction(const CONFIG & config, + const std::string & arg, + const std::map & /*options*/); + +bool DelCorpFunction(const CONFIG & config, + const std::string & arg, + const std::map & /*options*/); + +bool AddCorpFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +bool ChgCorpFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +} // namespace SGCONF + +#endif diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 135cd3c0..08199cfc 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -23,6 +23,9 @@ #include "admins.h" #include "tariffs.h" #include "users.h" +#include "services.h" +#include "corps.h" + #include "options.h" #include "actions.h" #include "config.h" @@ -433,10 +436,24 @@ blocks.Add("Tariff management options") .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgTariffFunction), "\tchange tariff"); blocks.Add("User management options") .Add("get-users", SGCONF::MakeAPIAction(commands, SGCONF::GetUsersFunction), "\tget user list") - .Add("get-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetUserFunction), "\tget user") - .Add("add-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddUserFunction), "\tadd user") - .Add("del-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelUserFunction), "\tdel user") - .Add("chg-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgUserFunction), "\tchange user"); + .Add("get-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetUserFunction), "\tget user") + .Add("add-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddUserFunction), "\tadd user") + .Add("del-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelUserFunction), "\tdel user") + .Add("chg-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgUserFunction), "\tchange user") + .Add("check-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::CheckUserFunction), "\tcheck user existance and credentials") + .Add("send-message", SGCONF::MakeAPIAction(commands, "", true, SGCONF::SendMessageFunction), "\tsend message"); +blocks.Add("Service management options") + .Add("get-services", SGCONF::MakeAPIAction(commands, SGCONF::GetServicesFunction), "\tget service list") + .Add("get-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetServiceFunction), "\tget service") + .Add("add-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddServiceFunction), "\tadd service") + .Add("del-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelServiceFunction), "\tdel service") + .Add("chg-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgServiceFunction), "\tchange service"); +blocks.Add("Corporation management options") + .Add("get-corps", SGCONF::MakeAPIAction(commands, SGCONF::GetCorpsFunction), "\tget corporation list") + .Add("get-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetCorpFunction), "\tget corporation") + .Add("add-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddCorpFunction), "\tadd corporation") + .Add("del-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelCorpFunction), "\tdel corporation") + .Add("chg-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgCorpFunction), "\tchange corporation"); SGCONF::PARSER_STATE state(false, argc, argv); diff --git a/projects/sgconf/services.cpp b/projects/sgconf/services.cpp new file mode 100644 index 00000000..3609166d --- /dev/null +++ b/projects/sgconf/services.cpp @@ -0,0 +1,121 @@ +#include "services.h" + +#include "config.h" + +#include "stg/servconf.h" +#include "stg/servconf_types.h" +#include "stg/service_conf.h" +#include "stg/common.h" + +#include + +namespace +{ + +std::string Indent(size_t level, bool dash = false) +{ +if (level == 0) + return ""; +return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' '); +} + +void PrintService(const STG::GET_SERVICE::INFO & info, size_t level = 0) +{ +std::cout << Indent(level, true) << "name: " << info.name << "\n" + << Indent(level) << "cost: " << info.cost << "\n" + << Indent(level) << "payment day: " << info.payDay << "\n" + << Indent(level) << "comment: " << info.comment << "\n"; +} + +void SimpleCallback(bool result, + const std::string & reason, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl; + return; + } +std::cout << "Success.\n"; +} + +void GetServicesCallback(bool result, + const std::string & reason, + const std::vector & info, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl; + return; + } +std::cout << "Services:\n"; +for (size_t i = 0; i < info.size(); ++i) + PrintService(info[i], 1); +} + +void GetServiceCallback(bool result, + const std::string & reason, + const STG::GET_SERVICE::INFO & info, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl; + return; + } +PrintService(info); +} + +} // namespace anonymous + +bool SGCONF::GetServicesFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok; +} + +bool SGCONF::GetServiceFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok; +} + +bool SGCONF::DelServiceFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok; +} + +bool SGCONF::AddServiceFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} + +bool SGCONF::ChgServiceFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} diff --git a/projects/sgconf/services.h b/projects/sgconf/services.h new file mode 100644 index 00000000..1b8b0c3c --- /dev/null +++ b/projects/sgconf/services.h @@ -0,0 +1,34 @@ +#ifndef __STG_SGCONF_SERVICES_H__ +#define __STG_SGCONF_SERVICES_H__ + +#include +#include + +namespace SGCONF +{ + +struct CONFIG; + +bool GetServicesFunction(const CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/); + +bool GetServiceFunction(const CONFIG & config, + const std::string & arg, + const std::map & /*options*/); + +bool DelServiceFunction(const CONFIG & config, + const std::string & arg, + const std::map & /*options*/); + +bool AddServiceFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +bool ChgServiceFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +} // namespace SGCONF + +#endif diff --git a/projects/sgconf/users.cpp b/projects/sgconf/users.cpp index 37b3db5e..3958869c 100644 --- a/projects/sgconf/users.cpp +++ b/projects/sgconf/users.cpp @@ -141,8 +141,8 @@ return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok; } bool SGCONF::AddUserFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) + const std::string & arg, + const std::map & /*options*/) { // TODO std::cerr << "Unimplemented.\n"; @@ -150,6 +150,15 @@ return false; } bool SGCONF::ChgUserFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} + +bool SGCONF::CheckUserFunction(const SGCONF::CONFIG & config, const std::string & arg, const std::map & options) { @@ -157,3 +166,12 @@ bool SGCONF::ChgUserFunction(const SGCONF::CONFIG & config, std::cerr << "Unimplemented.\n"; return false; } + +bool SGCONF::SendMessageFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} diff --git a/projects/sgconf/users.h b/projects/sgconf/users.h index d6c2dd8b..a7a30f07 100644 --- a/projects/sgconf/users.h +++ b/projects/sgconf/users.h @@ -29,6 +29,14 @@ bool ChgUserFunction(const CONFIG & config, const std::string & arg, const std::map & options); +bool CheckUserFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +bool SendMessageFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + } #endif