From: Maxim Mamontov Date: Tue, 27 May 2014 18:19:56 +0000 (+0300) Subject: Simplified module interfaces. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/7766e753fefb962209f1898a234053f8c7bbe59d Simplified module interfaces. --- diff --git a/projects/sgconf/Makefile b/projects/sgconf/Makefile index fd012fc4..52a70ae8 100644 --- a/projects/sgconf/Makefile +++ b/projects/sgconf/Makefile @@ -8,6 +8,7 @@ PROG = sgconf SRCS = ./main.cpp \ ./options.cpp \ + ./api_action.cpp \ ./actions.cpp \ ./admins.cpp \ ./tariffs.cpp \ diff --git a/projects/sgconf/action.h b/projects/sgconf/action.h index 0c1b4f92..b45ac470 100644 --- a/projects/sgconf/action.h +++ b/projects/sgconf/action.h @@ -22,6 +22,7 @@ #define __STG_SGCONF_ACTION_H__ #include +#include #include namespace SGCONF @@ -29,6 +30,7 @@ namespace SGCONF class OPTION_BLOCK; struct PARSER_STATE; +struct CONFIG; class ACTION { diff --git a/projects/sgconf/admins.cpp b/projects/sgconf/admins.cpp index da404bf3..e756618b 100644 --- a/projects/sgconf/admins.cpp +++ b/projects/sgconf/admins.cpp @@ -1,5 +1,7 @@ #include "admins.h" +#include "api_action.h" +#include "options.h" #include "config.h" #include "stg/servconf.h" @@ -7,6 +9,8 @@ #include "stg/os_int.h" #include +#include +#include #include namespace @@ -83,12 +87,10 @@ for (size_t i = 0; i < info.size(); ++i) PrintAdmin(info[i]); } -} // namespace anonymous - -bool SGCONF::GetAdminsFunction(const SGCONF::CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/) +bool GetAdminsFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -97,9 +99,9 @@ STG::SERVCONF proto(config.server.data(), return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok; } -bool SGCONF::GetAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool GetAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -111,9 +113,9 @@ std::string login(arg); return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok; } -bool SGCONF::DelAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool DelAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -122,20 +124,32 @@ STG::SERVCONF proto(config.server.data(), return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok; } -bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool AddAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { // TODO std::cerr << "Unimplemented.\n"; return false; } -bool SGCONF::ChgAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & options) +bool ChgAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) { // TODO std::cerr << "Unimplemented.\n"; return false; } + +} // namespace anonymous + +void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) +{ +blocks.Add("Admin management options") + .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list") + .Add("get-admin", SGCONF::MakeAPIAction(commands, "", true, GetAdminFunction), "get admin") + .Add("add-admin", SGCONF::MakeAPIAction(commands, "", true, AddAdminFunction), "add admin") + .Add("del-admin", SGCONF::MakeAPIAction(commands, "", true, DelAdminFunction), "del admin") + .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", true, ChgAdminFunction), "change admin"); +} diff --git a/projects/sgconf/admins.h b/projects/sgconf/admins.h index d3271724..6bc47380 100644 --- a/projects/sgconf/admins.h +++ b/projects/sgconf/admins.h @@ -1,33 +1,13 @@ #ifndef __STG_SGCONF_ADMINS_H__ #define __STG_SGCONF_ADMINS_H__ -#include -#include - namespace SGCONF { -struct CONFIG; - -bool GetAdminsFunction(const CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/); - -bool GetAdminFunction(const CONFIG & config, - const std::string & arg, - const std::map & /*options*/); - -bool DelAdminFunction(const CONFIG & config, - const std::string & arg, - const std::map & /*options*/); - -bool AddAdminFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); +class OPTION_BLOCKS; +class COMMANDS; -bool ChgAdminFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); +void AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks); } // namespace SGCONF diff --git a/projects/sgconf/api_action.cpp b/projects/sgconf/api_action.cpp new file mode 100644 index 00000000..1ff0e596 --- /dev/null +++ b/projects/sgconf/api_action.cpp @@ -0,0 +1,21 @@ +#include "api_action.h" + +#include "parser_state.h" + +SGCONF::PARSER_STATE SGCONF::API_ACTION::Parse(int argc, char ** argv) +{ +PARSER_STATE state(false, argc, argv); +if (!m_argument.empty()) + { + if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); + m_argument = *argv; + --state.argc; + ++state.argv; + } +m_suboptions.Parse(state.argc, state.argv); +m_commands.Add(m_funPtr, m_argument, m_params); +return state; +} diff --git a/projects/sgconf/api_action.h b/projects/sgconf/api_action.h new file mode 100644 index 00000000..55b38447 --- /dev/null +++ b/projects/sgconf/api_action.h @@ -0,0 +1,119 @@ +#ifndef __STG_SGCONF_API_ACTION_H__ +#define __STG_SGCONF_API_ACTION_H__ + +#include "action.h" + +#include "options.h" + +#include +#include +#include + +namespace SGCONF +{ + +typedef bool (* API_FUNCTION) (const CONFIG &, + const std::string &, + const std::map &); + +class COMMAND +{ + public: + COMMAND(API_FUNCTION funPtr, + const std::string & arg, + const std::map & options) + : m_funPtr(funPtr), + m_arg(arg), + m_options(options) + {} + bool Execute(const SGCONF::CONFIG & config) const + { + return m_funPtr(config, m_arg, m_options); + } + + private: + API_FUNCTION m_funPtr; + std::string m_arg; + std::map m_options; +}; + +class COMMANDS +{ + public: + void Add(API_FUNCTION funPtr, + const std::string & arg, + const std::map & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); } + bool Execute(const SGCONF::CONFIG & config) const + { + std::vector::const_iterator it(m_commands.begin()); + bool res = true; + while (it != m_commands.end() && res) + { + res = res && it->Execute(config); + ++it; + } + return res; + } + private: + std::vector m_commands; +}; + +class API_ACTION : public ACTION +{ + public: + API_ACTION(COMMANDS & commands, + const std::string & paramDescription, + bool needArgument, + const OPTION_BLOCK& suboptions, + API_FUNCTION funPtr) + : m_commands(commands), + m_description(paramDescription), + m_argument(needArgument ? "1" : ""), // Hack + m_suboptions(suboptions), + m_funPtr(funPtr) + {} + API_ACTION(COMMANDS & commands, + const std::string & paramDescription, + bool needArgument, + API_FUNCTION funPtr) + : m_commands(commands), + m_description(paramDescription), + m_argument(needArgument ? "1" : ""), // Hack + m_funPtr(funPtr) + {} + + virtual ACTION * Clone() const { return new API_ACTION(*this); } + + virtual std::string ParamDescription() const { return m_description; } + virtual std::string DefaultDescription() const { return ""; } + virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } + virtual PARSER_STATE Parse(int argc, char ** argv); + + private: + COMMANDS & m_commands; + std::string m_description; + std::string m_argument; + OPTION_BLOCK m_suboptions; + std::map m_params; + API_FUNCTION m_funPtr; +}; + +inline +ACTION * MakeAPIAction(COMMANDS & commands, + const std::string & paramDescription, + bool needArgument, + API_FUNCTION funPtr) +{ +return new API_ACTION(commands, paramDescription, needArgument, funPtr); +} + +inline +ACTION * MakeAPIAction(COMMANDS & commands, + API_FUNCTION funPtr) +{ +return new API_ACTION(commands, "", false, funPtr); +} + +} + +#endif diff --git a/projects/sgconf/corps.cpp b/projects/sgconf/corps.cpp index 56293efd..b48a7c71 100644 --- a/projects/sgconf/corps.cpp +++ b/projects/sgconf/corps.cpp @@ -1,5 +1,7 @@ #include "corps.h" +#include "api_action.h" +#include "options.h" #include "config.h" #include "stg/servconf.h" @@ -8,6 +10,8 @@ #include "stg/common.h" #include +#include +#include namespace { @@ -65,11 +69,9 @@ if (!result) PrintCorp(info); } -} // namespace anonymous - -bool SGCONF::GetCorpsFunction(const SGCONF::CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/) +bool GetCorpsFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -78,9 +80,9 @@ STG::SERVCONF proto(config.server.data(), return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok; } -bool SGCONF::GetCorpFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool GetCorpFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -89,9 +91,9 @@ STG::SERVCONF proto(config.server.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*/) +bool DelCorpFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -100,20 +102,32 @@ STG::SERVCONF proto(config.server.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*/) +bool 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) +bool ChgCorpFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) { // TODO std::cerr << "Unimplemented.\n"; return false; } + +} // namespace anonymous + +void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) +{ +blocks.Add("Corporation management options") + .Add("get-corps", SGCONF::MakeAPIAction(commands, GetCorpsFunction), "\tget corporation list") + .Add("get-corp", SGCONF::MakeAPIAction(commands, "", true, GetCorpFunction), "get corporation") + .Add("add-corp", SGCONF::MakeAPIAction(commands, "", true, AddCorpFunction), "add corporation") + .Add("del-corp", SGCONF::MakeAPIAction(commands, "", true, DelCorpFunction), "del corporation") + .Add("chg-corp", SGCONF::MakeAPIAction(commands, "", true, ChgCorpFunction), "change corporation"); +} diff --git a/projects/sgconf/corps.h b/projects/sgconf/corps.h index 58a65161..de823b7b 100644 --- a/projects/sgconf/corps.h +++ b/projects/sgconf/corps.h @@ -1,33 +1,13 @@ #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); +class OPTION_BLOCKS; +class COMMANDS; -bool ChgCorpFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); +void AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks); } // namespace SGCONF diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 6a71f652..73917852 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -26,6 +26,7 @@ #include "services.h" #include "corps.h" +#include "api_action.h" #include "options.h" #include "actions.h" #include "config.h" @@ -173,107 +174,6 @@ class CONFIG_ACTION : public ACTION void ParseHostAndPort(const std::string & hostAndPort); }; -typedef bool (* API_FUNCTION) (const SGCONF::CONFIG &, - const std::string &, - const std::map &); - -class COMMAND -{ - public: - COMMAND(API_FUNCTION funPtr, - const std::string & arg, - const std::map & options) - : m_funPtr(funPtr), - m_arg(arg), - m_options(options) - {} - bool Execute(const SGCONF::CONFIG & config) const - { - return m_funPtr(config, m_arg, m_options); - } - - private: - API_FUNCTION m_funPtr; - std::string m_arg; - std::map m_options; -}; - -class COMMANDS -{ - public: - void Add(API_FUNCTION funPtr, - const std::string & arg, - const std::map & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); } - bool Execute(const SGCONF::CONFIG & config) const - { - std::list::const_iterator it(m_commands.begin()); - bool res = true; - while (it != m_commands.end() && res) - { - res = res && it->Execute(config); - ++it; - } - return res; - } - private: - std::list m_commands; -}; - -class API_ACTION : public ACTION -{ - public: - API_ACTION(COMMANDS & commands, - const std::string & paramDescription, - bool needArgument, - const OPTION_BLOCK& suboptions, - API_FUNCTION funPtr) - : m_commands(commands), - m_description(paramDescription), - m_argument(needArgument ? "1" : ""), // Hack - m_suboptions(suboptions), - m_funPtr(funPtr) - {} - API_ACTION(COMMANDS & commands, - const std::string & paramDescription, - bool needArgument, - API_FUNCTION funPtr) - : m_commands(commands), - m_description(paramDescription), - m_argument(needArgument ? "1" : ""), // Hack - m_funPtr(funPtr) - {} - - virtual ACTION * Clone() const { return new API_ACTION(*this); } - - virtual std::string ParamDescription() const { return m_description; } - virtual std::string DefaultDescription() const { return ""; } - virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv) - { - PARSER_STATE state(false, argc, argv); - if (!m_argument.empty()) - { - if (argc == 0 || - argv == NULL || - *argv == NULL) - throw ERROR("Missing argument."); - m_argument = *argv; - --state.argc; - ++state.argv; - } - m_suboptions.Parse(state.argc, state.argv); - m_commands.Add(m_funPtr, m_argument, m_params); - return state; - } - - private: - COMMANDS & m_commands; - std::string m_description; - std::string m_argument; - OPTION_BLOCK m_suboptions; - std::map m_params; - API_FUNCTION m_funPtr; -}; PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv) { @@ -332,26 +232,8 @@ CONFIG_ACTION * MakeParamAction(SGCONF::CONFIG & config, return new CONFIG_ACTION(config, paramDescription); } -inline -ACTION * MakeAPIAction(COMMANDS & commands, - const std::string & paramDescription, - bool needArgument, - API_FUNCTION funPtr) -{ -return new API_ACTION(commands, paramDescription, needArgument, funPtr); -} - -inline -ACTION * MakeAPIAction(COMMANDS & commands, - API_FUNCTION funPtr) -{ -return new API_ACTION(commands, "", false, funPtr); -} - } // namespace SGCONF -time_t stgTime; - //----------------------------------------------------------------------------- int main(int argc, char **argv) { @@ -371,40 +253,12 @@ SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options") .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), ""), "\tadministrative login") .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, ""), "\tpassword for the administrative login") .Add("a", "address", SGCONF::MakeParamAction(config, ""), "connection params as a single string in format: :@:"); -blocks.Add("Raw XML") - .Add("r", "raw", SGCONF::MakeAPIAction(commands, "", true, SGCONF::RawXMLFunction), "\tmake raw XML request"); -blocks.Add("Admin management options") - .Add("get-admins", SGCONF::MakeAPIAction(commands, SGCONF::GetAdminsFunction), "\tget admin list") - .Add("get-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetAdminFunction), "get admin") - .Add("add-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddAdminFunction), "add admin") - .Add("del-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelAdminFunction), "del admin") - .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgAdminFunction), "change admin"); -blocks.Add("Tariff management options") - .Add("get-tariffs", SGCONF::MakeAPIAction(commands, SGCONF::GetTariffsFunction), "\tget tariff list") - .Add("get-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetTariffFunction), "get tariff") - .Add("add-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddTariffFunction), "add tariff") - .Add("del-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelTariffFunction), "del tariff") - .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgTariffFunction), "change 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), "get user") - .Add("add-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddUserFunction), "add user") - .Add("del-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelUserFunction), "del user") - .Add("chg-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgUserFunction), "change user") - .Add("check-user", SGCONF::MakeAPIAction(commands, "", true, SGCONF::CheckUserFunction), "check user existance and credentials") - .Add("send-message", SGCONF::MakeAPIAction(commands, "", true, SGCONF::SendMessageFunction), "send 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), "get service") - .Add("add-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddServiceFunction), "add service") - .Add("del-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelServiceFunction), "del service") - .Add("chg-service", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgServiceFunction), "change 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), "get corporation") - .Add("add-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddCorpFunction), "add corporation") - .Add("del-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelCorpFunction), "del corporation") - .Add("chg-corp", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgCorpFunction), "change corporation"); +SGCONF::AppendXMLOptionBlock(commands, blocks); +SGCONF::AppendAdminsOptionBlock(commands, blocks); +SGCONF::AppendTariffsOptionBlock(commands, blocks); +SGCONF::AppendUsersOptionBlock(commands, blocks); +SGCONF::AppendServicesOptionBlock(commands, blocks); +SGCONF::AppendCorpsOptionBlock(commands, blocks); SGCONF::PARSER_STATE state(false, argc, argv); diff --git a/projects/sgconf/options.h b/projects/sgconf/options.h index 6b33ee8c..53875091 100644 --- a/projects/sgconf/options.h +++ b/projects/sgconf/options.h @@ -107,6 +107,7 @@ class OPTION_BLOCKS public: OPTION_BLOCK & Add(const std::string & description) { m_blocks.push_back(OPTION_BLOCK(description)); return m_blocks.back(); } + void Add(const OPTION_BLOCK & block) { m_blocks.push_back(block); } void Help(size_t level) const; PARSER_STATE Parse(int argc, char ** argv); diff --git a/projects/sgconf/services.cpp b/projects/sgconf/services.cpp index 3609166d..ba490467 100644 --- a/projects/sgconf/services.cpp +++ b/projects/sgconf/services.cpp @@ -1,5 +1,7 @@ #include "services.h" +#include "api_action.h" +#include "options.h" #include "config.h" #include "stg/servconf.h" @@ -8,6 +10,8 @@ #include "stg/common.h" #include +#include +#include namespace { @@ -67,11 +71,9 @@ if (!result) PrintService(info); } -} // namespace anonymous - -bool SGCONF::GetServicesFunction(const SGCONF::CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/) +bool GetServicesFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -80,9 +82,9 @@ STG::SERVCONF proto(config.server.data(), return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok; } -bool SGCONF::GetServiceFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool GetServiceFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -91,9 +93,9 @@ STG::SERVCONF proto(config.server.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*/) +bool DelServiceFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -102,20 +104,32 @@ STG::SERVCONF proto(config.server.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*/) +bool 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) +bool ChgServiceFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) { // TODO std::cerr << "Unimplemented.\n"; return false; } + +} // namespace anonymous + +void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) +{ +blocks.Add("Service management options") + .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list") + .Add("get-service", SGCONF::MakeAPIAction(commands, "", true, GetServiceFunction), "get service") + .Add("add-service", SGCONF::MakeAPIAction(commands, "", true, AddServiceFunction), "add service") + .Add("del-service", SGCONF::MakeAPIAction(commands, "", true, DelServiceFunction), "del service") + .Add("chg-service", SGCONF::MakeAPIAction(commands, "", true, ChgServiceFunction), "change service"); +} diff --git a/projects/sgconf/services.h b/projects/sgconf/services.h index 1b8b0c3c..c5045273 100644 --- a/projects/sgconf/services.h +++ b/projects/sgconf/services.h @@ -1,33 +1,13 @@ #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); +class OPTION_BLOCKS; +class COMMANDS; -bool ChgServiceFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); +void AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks); } // namespace SGCONF diff --git a/projects/sgconf/tariffs.cpp b/projects/sgconf/tariffs.cpp index 04bf6879..4d4feb9a 100644 --- a/projects/sgconf/tariffs.cpp +++ b/projects/sgconf/tariffs.cpp @@ -1,5 +1,7 @@ #include "tariffs.h" +#include "api_action.h" +#include "options.h" #include "config.h" #include "stg/servconf.h" @@ -9,6 +11,8 @@ #include #include +#include +#include #include namespace @@ -131,11 +135,9 @@ for (size_t i = 0; i < info.size(); ++i) PrintTariff(info[i]); } -} // namespace anonymous - -bool SGCONF::GetTariffsFunction(const SGCONF::CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/) +bool GetTariffsFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -144,9 +146,9 @@ STG::SERVCONF proto(config.server.data(), return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok; } -bool SGCONF::GetTariffFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool GetTariffFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -158,9 +160,9 @@ std::string name(arg); return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok; } -bool SGCONF::DelTariffFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool DelTariffFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -169,20 +171,32 @@ STG::SERVCONF proto(config.server.data(), return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok; } -bool SGCONF::AddTariffFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool AddTariffFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { // TODO std::cerr << "Unimplemented.\n"; return false; } -bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & options) +bool ChgTariffFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) { // TODO std::cerr << "Unimplemented.\n"; return false; } + +} // namespace anonymous + +void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) +{ +blocks.Add("Tariff management options") + .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list") + .Add("get-tariff", SGCONF::MakeAPIAction(commands, "", true, GetTariffFunction), "get tariff") + .Add("add-tariff", SGCONF::MakeAPIAction(commands, "", true, AddTariffFunction), "add tariff") + .Add("del-tariff", SGCONF::MakeAPIAction(commands, "", true, DelTariffFunction), "del tariff") + .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", true, ChgTariffFunction), "change tariff"); +} diff --git a/projects/sgconf/tariffs.h b/projects/sgconf/tariffs.h index b3cd99e0..cde906bf 100644 --- a/projects/sgconf/tariffs.h +++ b/projects/sgconf/tariffs.h @@ -1,33 +1,13 @@ #ifndef __STG_SGCONF_TARIFFS_H__ #define __STG_SGCONF_TARIFFS_H__ -#include -#include - namespace SGCONF { -struct CONFIG; - -bool GetTariffsFunction(const CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/); - -bool GetTariffFunction(const CONFIG & config, - const std::string & arg, - const std::map & /*options*/); - -bool DelTariffFunction(const CONFIG & config, - const std::string & arg, - const std::map & /*options*/); - -bool AddTariffFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); +class OPTION_BLOCKS; +class COMMANDS; -bool ChgTariffFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); +void AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks); } // namespace SGCONF diff --git a/projects/sgconf/users.cpp b/projects/sgconf/users.cpp index 3958869c..25159924 100644 --- a/projects/sgconf/users.cpp +++ b/projects/sgconf/users.cpp @@ -1,5 +1,7 @@ #include "users.h" +#include "api_action.h" +#include "options.h" #include "config.h" #include "stg/servconf.h" @@ -7,6 +9,8 @@ #include "stg/common.h" #include +#include +#include namespace { @@ -105,11 +109,9 @@ if (!result) PrintUser(info); } -} // namespace anonymous - -bool SGCONF::GetUsersFunction(const SGCONF::CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/) +bool GetUsersFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -118,9 +120,9 @@ STG::SERVCONF proto(config.server.data(), return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok; } -bool SGCONF::GetUserFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool GetUserFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -129,9 +131,9 @@ STG::SERVCONF proto(config.server.data(), return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok; } -bool SGCONF::DelUserFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool DelUserFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { STG::SERVCONF proto(config.server.data(), config.port.data(), @@ -140,38 +142,52 @@ STG::SERVCONF proto(config.server.data(), return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok; } -bool SGCONF::AddUserFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool AddUserFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) { // TODO std::cerr << "Unimplemented.\n"; return false; } -bool SGCONF::ChgUserFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & options) +bool 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) +bool CheckUserFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) { // TODO std::cerr << "Unimplemented.\n"; return false; } -bool SGCONF::SendMessageFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & options) +bool SendMessageFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) { // TODO std::cerr << "Unimplemented.\n"; return false; } + +} // namespace anonymous + +void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) +{ +blocks.Add("User management options") + .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list") + .Add("get-user", SGCONF::MakeAPIAction(commands, "", true, GetUserFunction), "get user") + .Add("add-user", SGCONF::MakeAPIAction(commands, "", true, AddUserFunction), "add user") + .Add("del-user", SGCONF::MakeAPIAction(commands, "", true, DelUserFunction), "del user") + .Add("chg-user", SGCONF::MakeAPIAction(commands, "", true, ChgUserFunction), "change user") + .Add("check-user", SGCONF::MakeAPIAction(commands, "", true, CheckUserFunction), "check user existance and credentials") + .Add("send-message", SGCONF::MakeAPIAction(commands, "", true, SendMessageFunction), "send message"); +} diff --git a/projects/sgconf/users.h b/projects/sgconf/users.h index a7a30f07..a757fe10 100644 --- a/projects/sgconf/users.h +++ b/projects/sgconf/users.h @@ -1,41 +1,13 @@ #ifndef __STG_SGCONF_USERS_H__ #define __STG_SGCONF_USERS_H__ -#include -#include - namespace SGCONF { -struct CONFIG; - -bool GetUsersFunction(const CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/); - -bool GetUserFunction(const CONFIG & config, - const std::string & arg, - const std::map & /*options*/); - -bool DelUserFunction(const CONFIG & config, - const std::string & arg, - const std::map & /*options*/); - -bool AddUserFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); - -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); +class OPTION_BLOCKS; +class COMMANDS; -bool SendMessageFunction(const CONFIG & config, - const std::string & arg, - const std::map & options); +void AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks); } diff --git a/projects/sgconf/xml.cpp b/projects/sgconf/xml.cpp index ec8b9178..5a7d2afd 100644 --- a/projects/sgconf/xml.cpp +++ b/projects/sgconf/xml.cpp @@ -1,10 +1,14 @@ #include "xml.h" +#include "api_action.h" +#include "options.h" #include "config.h" #include "stg/servconf.h" #include +#include +#include #include @@ -52,19 +56,7 @@ if (el != NULL) std::cout << Indent(state->level) << "\n"; } -void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/) -{ -if (!result) - { - std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl; - return; - } -SGCONF::PrintXML(response); -} - -} - -void SGCONF::PrintXML(const std::string& xml) +void PrintXML(const std::string& xml) { ParserState state = { 0 }; @@ -81,13 +73,31 @@ if (XML_Parse(parser, xml.c_str(), xml.length(), true) == XML_STATUS_ERROR) XML_ParserFree(parser); } -bool SGCONF::RawXMLFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl; + return; + } +PrintXML(response); +} + +bool RawXMLFunction(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.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok; +} + +} + +void SGCONF::AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) { - STG::SERVCONF proto(config.server.data(), - config.port.data(), - config.userName.data(), - config.userPass.data()); - return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok; +blocks.Add("Raw XML") + .Add("r", "raw", SGCONF::MakeAPIAction(commands, "", true, RawXMLFunction), "\tmake raw XML request"); } diff --git a/projects/sgconf/xml.h b/projects/sgconf/xml.h index 549a69b3..453d5eb6 100644 --- a/projects/sgconf/xml.h +++ b/projects/sgconf/xml.h @@ -21,19 +21,13 @@ #ifndef __STG_SGCONF_XML_H__ #define __STG_SGCONF_XML_H__ -#include -#include - namespace SGCONF { -struct CONFIG; - -void PrintXML(const std::string& xml); +class OPTION_BLOCKS; +class COMMANDS; -bool RawXMLFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/); +void AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks); }