]> git.stg.codes - stg.git/commitdiff
Implemented some operations with tariffs.
authorMaxim Mamontov <faust.madf@gmail.com>
Tue, 20 May 2014 20:46:01 +0000 (23:46 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Tue, 20 May 2014 20:46:01 +0000 (23:46 +0300)
projects/sgconf/Makefile
projects/sgconf/admins.cpp
projects/sgconf/main.cpp
projects/sgconf/tariffs.cpp [new file with mode: 0644]
projects/sgconf/tariffs.h [new file with mode: 0644]
stglibs/common.lib/include/stg/common.h
stglibs/srvconf.lib/parsers/get_admin.cpp
stglibs/srvconf.lib/parsers/get_tariff.cpp

index dd2cc66b9977501bb036d6d325f26d47c06d9b01..263f66034c338d43b9ba778a606df4fd4b661283 100644 (file)
@@ -11,6 +11,7 @@ SRCS = ./main.cpp \
        ./options.cpp \
        ./actions.cpp \
        ./admins.cpp \
+       ./tariffs.cpp \
        ./xml.cpp
 
 STGLIBS = conffiles \
index 03aa5a2010315b49db71e6d32c8b2c2887aacbd8..ac8134a437c8b4f281871f95345d650ecdece2c3 100644 (file)
@@ -126,6 +126,8 @@ bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config,
                               const std::string & arg,
                               const std::map<std::string, std::string> & /*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<std::string, std::string> & options)
 {
+// TODO
+std::cerr << "Unimplemented.\n";
 return false;
 }
index babfd86f2992729dceec28bbc634cba0a6340949..1e64ca2a741a0ced8b3752c8a8ce4501bc7234b1 100644 (file)
@@ -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 string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
 blocks.Add("Raw XML")
       .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", 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, "<login>", true, SGCONF::GetAdminFunction), "\tget admin")
       .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddAdminFunction), "\tadd admin")
       .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelAdminFunction), "\tdel admin")
       .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", 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, "<name>", true, SGCONF::GetTariffFunction), "\tget tariff")
+      .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddTariffFunction), "\tadd tariff")
+      .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelTariffFunction), "\tdel tariff")
+      .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", 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 (file)
index 0000000..046a612
--- /dev/null
@@ -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 <iostream>
+#include <sstream>
+#include <cassert>
+
+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<STG::GET_TARIFF::INFO> & 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<STG::GET_TARIFF::INFO> & info,
+                       void * data)
+{
+assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
+const std::string & name = *static_cast<const std::string *>(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<std::string, std::string> & /*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<std::string, std::string> & /*options*/)
+{
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.userName.data(),
+                    config.userPass.data());
+// STG currently doesn't support <GetTariff name="..."/>.
+// 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<std::string, std::string> & /*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<std::string, std::string> & /*options*/)
+{
+// TODO
+std::cerr << "Unimplemented.\n";
+return false;
+}
+
+bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config,
+                               const std::string & arg,
+                               const std::map<std::string, std::string> & options)
+{
+// TODO
+std::cerr << "Unimplemented.\n";
+return false;
+}
diff --git a/projects/sgconf/tariffs.h b/projects/sgconf/tariffs.h
new file mode 100644 (file)
index 0000000..28f25f9
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef __STG_SGCONF_TARIFFS_H__
+#define __STG_SGCONF_TARIFFS_H__
+
+#include <string>
+#include <map>
+
+namespace SGCONF
+{
+
+class CONFIG;
+
+bool GetTariffsFunction(const CONFIG & config,
+                        const std::string & /*arg*/,
+                        const std::map<std::string, std::string> & /*options*/);
+
+bool GetTariffFunction(const CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & /*options*/);
+
+bool DelTariffFunction(const CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & /*options*/);
+
+bool AddTariffFunction(const CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & options);
+
+bool ChgTariffFunction(const CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & options);
+
+} // namespace SGCONF
+
+#endif
index f2e4e7e2432ab52aa75ed29efbe684726a7fe5ea..263cfe37a68b357aadfd2693845b8b2e65b62f92 100644 (file)
@@ -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);
index 809280b74b6da0936a8e4d92223c7461c428e653..8f775d0638c903b2a4b7687e1c67b2e96f5d8785 100644 (file)
@@ -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;
                     }
             }
index eb94441ea5b4fd086c5217a08343828c8c67304f..9ab61638a7397c89eca58e89a8030f0f54f549b6 100644 (file)
@@ -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 + "'.";
 }