]> git.stg.codes - stg.git/blob - projects/sgconf/tariffs.cpp
04bf68797a1481f3399eaf4c9cc3487d2fc33be4
[stg.git] / projects / sgconf / tariffs.cpp
1 #include "tariffs.h"
2
3 #include "config.h"
4
5 #include "stg/servconf.h"
6 #include "stg/servconf_types.h"
7 #include "stg/tariff_conf.h"
8 #include "stg/os_int.h"
9
10 #include <iostream>
11 #include <sstream>
12 #include <cassert>
13
14 namespace
15 {
16
17 std::string Indent(size_t level, bool dash = false)
18 {
19 if (level == 0)
20     return "";
21 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
22 }
23
24 std::string PeriodToString(TARIFF::PERIOD period)
25 {
26 switch (period)
27     {
28     case TARIFF::DAY:
29         return "daily";
30     case TARIFF::MONTH:
31         return "monthly";
32     }
33 return "unknown";
34 }
35
36 std::string TraffTypeToString(int traffType)
37 {
38 switch (traffType)
39     {
40     case TRAFF_UP:
41         return "upload";
42     case TRAFF_DOWN:
43         return "download";
44     case TRAFF_UP_DOWN:
45         return "upload + download";
46     case TRAFF_MAX:
47         return "max(upload, download)";
48     }
49 return "unknown";
50 }
51
52 std::string TimeToString(int h, int m)
53 {
54 std::ostringstream stream;
55 stream << (h < 10 ? "0" : "") << h << ":"
56        << (m < 10 ? "0" : "") << m;
57 return stream.str();
58 }
59
60 void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level)
61 {
62 std::string night = TimeToString(data.hNight, data.mNight);
63 std::string day = TimeToString(data.hDay, data.mDay);
64 std::cout << Indent(level, true) << "dir: " << dir << "\n"
65           << Indent(level)       << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n"
66           << Indent(level)       << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n"
67           << Indent(level)       << "threshold: " << data.threshold << "\n"
68           << Indent(level)       << "single price: " << (data.singlePrice ? "yes" : "no") << "\n"
69           << Indent(level)       << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention!
70 }
71
72 void PrintTariffConf(const TARIFF_CONF & conf, size_t level)
73 {
74 std::cout << Indent(level, true) << "name: " << conf.name << "\n"
75           << Indent(level)       << "fee: " << conf.fee << "\n"
76           << Indent(level)       << "free mb: " << conf.free << "\n"
77           << Indent(level)       << "passive cost: " << conf.passiveCost << "\n"
78           << Indent(level)       << "traff type: " << TraffTypeToString(conf.traffType) << "\n"
79           << Indent(level)       << "period: " << PeriodToString(conf.period) << "\n";
80 }
81
82 void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0)
83 {
84 PrintTariffConf(info.tariffConf, level);
85 std::cout << Indent(level) << "dir prices:\n";
86 for (size_t i = 0; i < info.dirPrice.size(); ++i)
87     PrintDirPriceData(i, info.dirPrice[i], level + 1);
88 }
89
90 void SimpleCallback(bool result,
91                     const std::string & reason,
92                     void * /*data*/)
93 {
94 if (!result)
95     {
96     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
97     return;
98     }
99 std::cout << "Success.\n";
100 }
101
102 void GetTariffsCallback(bool result,
103                         const std::string & reason,
104                         const std::vector<STG::GET_TARIFF::INFO> & info,
105                         void * /*data*/)
106 {
107 if (!result)
108     {
109     std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
110     return;
111     }
112 std::cout << "Tariffs:\n";
113 for (size_t i = 0; i < info.size(); ++i)
114     PrintTariff(info[i], 1);
115 }
116
117 void GetTariffCallback(bool result,
118                        const std::string & reason,
119                        const std::vector<STG::GET_TARIFF::INFO> & info,
120                        void * data)
121 {
122 assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
123 const std::string & name = *static_cast<const std::string *>(data);
124 if (!result)
125     {
126     std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
127     return;
128     }
129 for (size_t i = 0; i < info.size(); ++i)
130     if (info[i].tariffConf.name == name)
131         PrintTariff(info[i]);
132 }
133
134 } // namespace anonymous
135
136 bool SGCONF::GetTariffsFunction(const SGCONF::CONFIG & config,
137                                 const std::string & /*arg*/,
138                                 const std::map<std::string, std::string> & /*options*/)
139 {
140 STG::SERVCONF proto(config.server.data(),
141                     config.port.data(),
142                     config.userName.data(),
143                     config.userPass.data());
144 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
145 }
146
147 bool SGCONF::GetTariffFunction(const SGCONF::CONFIG & config,
148                                const std::string & arg,
149                                const std::map<std::string, std::string> & /*options*/)
150 {
151 STG::SERVCONF proto(config.server.data(),
152                     config.port.data(),
153                     config.userName.data(),
154                     config.userPass.data());
155 // STG currently doesn't support <GetTariff name="..."/>.
156 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
157 std::string name(arg);
158 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
159 }
160
161 bool SGCONF::DelTariffFunction(const SGCONF::CONFIG & config,
162                                const std::string & arg,
163                                const std::map<std::string, std::string> & /*options*/)
164 {
165 STG::SERVCONF proto(config.server.data(),
166                     config.port.data(),
167                     config.userName.data(),
168                     config.userPass.data());
169 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
170 }
171
172 bool SGCONF::AddTariffFunction(const SGCONF::CONFIG & config,
173                                const std::string & arg,
174                                const std::map<std::string, std::string> & /*options*/)
175 {
176 // TODO
177 std::cerr << "Unimplemented.\n";
178 return false;
179 }
180
181 bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config,
182                                const std::string & arg,
183                                const std::map<std::string, std::string> & options)
184 {
185 // TODO
186 std::cerr << "Unimplemented.\n";
187 return false;
188 }