From: Maxim Mamontov Date: Tue, 20 May 2014 20:46:01 +0000 (+0300) Subject: Implemented some operations with tariffs. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/01cdd74060b063287784d3aff7f9f861a404b789?ds=inline Implemented some operations with tariffs. --- diff --git a/projects/sgconf/Makefile b/projects/sgconf/Makefile index dd2cc66b..263f6603 100644 --- a/projects/sgconf/Makefile +++ b/projects/sgconf/Makefile @@ -11,6 +11,7 @@ SRCS = ./main.cpp \ ./options.cpp \ ./actions.cpp \ ./admins.cpp \ + ./tariffs.cpp \ ./xml.cpp STGLIBS = conffiles \ diff --git a/projects/sgconf/admins.cpp b/projects/sgconf/admins.cpp index 03aa5a20..ac8134a4 100644 --- a/projects/sgconf/admins.cpp +++ b/projects/sgconf/admins.cpp @@ -126,6 +126,8 @@ bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config, const std::string & arg, const std::map & /*options*/) { +// TODO +std::cerr << "Unimplemented.\n"; return false; } @@ -133,5 +135,7 @@ bool SGCONF::ChgAdminFunction(const SGCONF::CONFIG & config, const std::string & arg, const std::map & options) { +// TODO +std::cerr << "Unimplemented.\n"; return false; } diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index babfd86f..1e64ca2a 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -25,6 +25,7 @@ #include "xml.h" #include "admins.h" +#include "tariffs.h" #include "options.h" #include "actions.h" #include "config.h" @@ -1383,12 +1384,18 @@ SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options") .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("Admins management options") +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), "\tget admin") .Add("add-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddAdminFunction), "\tadd admin") .Add("del-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelAdminFunction), "\tdel admin") .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgAdminFunction), "\tchange 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), "\tget tariff") + .Add("add-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddTariffFunction), "\tadd tariff") + .Add("del-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelTariffFunction), "\tdel tariff") + .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgTariffFunction), "\tchange tariff"); SGCONF::PARSER_STATE state(false, argc, argv); diff --git a/projects/sgconf/tariffs.cpp b/projects/sgconf/tariffs.cpp new file mode 100644 index 00000000..046a6121 --- /dev/null +++ b/projects/sgconf/tariffs.cpp @@ -0,0 +1,189 @@ +#include "tariffs.h" + +#include "config.h" + +#include "stg/servconf.h" +#include "stg/servconf_types.h" +#include "stg/tariff_conf.h" +#include "stg/os_int.h" + +#include +#include +#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, ' '); +} + +std::string PeriodToString(TARIFF::PERIOD period) +{ +switch (period) + { + case TARIFF::DAY: + return "daily"; + case TARIFF::MONTH: + return "monthly"; + } +return "unknown"; +} + +std::string TraffTypeToString(int traffType) +{ +switch (traffType) + { + case TRAFF_UP: + return "upload"; + case TRAFF_DOWN: + return "download"; + case TRAFF_UP_DOWN: + return "upload + download"; + case TRAFF_MAX: + return "max(upload, download)"; + } +return "unknown"; +} + +std::string TimeToString(int h, int m) +{ +std::ostringstream stream; +stream << (h < 10 ? "0" : "") << h << ":" + << (m < 10 ? "0" : "") << m; +return stream.str(); +} + +void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level) +{ +std::string night = TimeToString(data.hNight, data.mNight); +std::string day = TimeToString(data.hDay, data.mDay); +std::cout << Indent(level, true) << "dir: " << dir << "\n" + << Indent(level) << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n" + << Indent(level) << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n" + << Indent(level) << "threshold: " << data.threshold << "\n" + << Indent(level) << "single price: " << (data.singlePrice ? "yes" : "no") << "\n" + << Indent(level) << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention! +} + +void PrintTariffConf(const TARIFF_CONF & conf, size_t level) +{ +std::cout << Indent(level, true) << "name: " << conf.name << "\n" + << Indent(level) << "fee: " << conf.fee << "\n" + << Indent(level) << "free mb: " << conf.free << "\n" + << Indent(level) << "passive cost: " << conf.passiveCost << "\n" + << Indent(level) << "traff type: " << TraffTypeToString(conf.traffType) << "\n" + << Indent(level) << "period: " << PeriodToString(conf.period) << "\n"; +} + +void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0) +{ +PrintTariffConf(info.tariffConf, level); +std::cout << Indent(level) << "dir prices:\n"; +for (size_t i = 0; i < info.dirPrice.size(); ++i) + PrintDirPriceData(i, info.dirPrice[i], level + 1); +} + +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 GetTariffsCallback(bool result, + const std::string & reason, + const std::vector & info, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl; + return; + } +std::cout << "Tariffs:\n"; +for (size_t i = 0; i < info.size(); ++i) + PrintTariff(info[i], 1); +} + +void GetTariffCallback(bool result, + const std::string & reason, + const std::vector & info, + void * data) +{ +assert(data != NULL && "Expecting pointer to std::string with the tariff's name."); +const std::string & name = *static_cast(data); +if (!result) + { + std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl; + return; + } +for (size_t i = 0; i < info.size(); ++i) + if (info[i].tariffConf.name == name) + PrintTariff(info[i]); +} + +} + + +bool SGCONF::GetTariffsFunction(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.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok; +} + +bool SGCONF::GetTariffFunction(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()); +// STG currently doesn't support . +// So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'. +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*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.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*/) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} + +bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) +{ +// TODO +std::cerr << "Unimplemented.\n"; +return false; +} diff --git a/projects/sgconf/tariffs.h b/projects/sgconf/tariffs.h new file mode 100644 index 00000000..28f25f91 --- /dev/null +++ b/projects/sgconf/tariffs.h @@ -0,0 +1,34 @@ +#ifndef __STG_SGCONF_TARIFFS_H__ +#define __STG_SGCONF_TARIFFS_H__ + +#include +#include + +namespace SGCONF +{ + +class 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); + +bool ChgTariffFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +} // namespace SGCONF + +#endif diff --git a/stglibs/common.lib/include/stg/common.h b/stglibs/common.lib/include/stg/common.h index f2e4e7e2..263cfe37 100644 --- a/stglibs/common.lib/include/stg/common.h +++ b/stglibs/common.lib/include/stg/common.h @@ -114,6 +114,8 @@ bool WaitPackets(int sd); //----------------------------------------------------------------------------- int str2x(const std::string & str, int32_t & x); int str2x(const std::string & str, uint32_t & x); +inline +int str2x(const std::string & str, double & x) { return strtodouble2(str.c_str(), x); } #ifndef WIN32 int str2x(const std::string & str, int64_t & x); int str2x(const std::string & str, uint64_t & x); diff --git a/stglibs/srvconf.lib/parsers/get_admin.cpp b/stglibs/srvconf.lib/parsers/get_admin.cpp index 809280b7..8f775d06 100644 --- a/stglibs/srvconf.lib/parsers/get_admin.cpp +++ b/stglibs/srvconf.lib/parsers/get_admin.cpp @@ -106,7 +106,7 @@ if (strcasecmp(el, "admin") == 0) for (const char ** pos = attr; *pos != NULL; pos = pos + 2) if (!TryParse(propertyParsers, ToLower(*pos), pos, *pos)) { - error = "Invalid parameter."; + error = std::string("Invalid parameter '") + *pos + "'."; break; } } diff --git a/stglibs/srvconf.lib/parsers/get_tariff.cpp b/stglibs/srvconf.lib/parsers/get_tariff.cpp index eb94441e..9ab61638 100644 --- a/stglibs/srvconf.lib/parsers/get_tariff.cpp +++ b/stglibs/srvconf.lib/parsers/get_tariff.cpp @@ -178,7 +178,11 @@ if (strcasecmp(el, "tariff") == 0) error = "Tariff not found."; } else + { parsingAnswer = true; + if (strcasecmp(attr[0], "name") == 0) + info.tariffConf.name = attr[1]; + } } else parsingAnswer = true; @@ -188,5 +192,5 @@ if (strcasecmp(el, "tariff") == 0) void GET_TARIFF::PARSER::ParseTariffParams(const char * el, const char ** attr) { if (!TryParse(propertyParsers, ToLower(el), attr)) - error = "Invalid parameter."; + error = std::string("Invalid parameter '") + el + "'."; }