]> git.stg.codes - stg.git/blob - projects/sgconf/tariffs.cpp
Implemented some operations with tariffs.
[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 }
135
136
137 bool SGCONF::GetTariffsFunction(const SGCONF::CONFIG & config,
138                                 const std::string & /*arg*/,
139                                 const std::map<std::string, std::string> & /*options*/)
140 {
141 STG::SERVCONF proto(config.server.data(),
142                     config.port.data(),
143                     config.userName.data(),
144                     config.userPass.data());
145 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
146 }
147
148 bool SGCONF::GetTariffFunction(const SGCONF::CONFIG & config,
149                                const std::string & arg,
150                                const std::map<std::string, std::string> & /*options*/)
151 {
152 STG::SERVCONF proto(config.server.data(),
153                     config.port.data(),
154                     config.userName.data(),
155                     config.userPass.data());
156 // STG currently doesn't support <GetTariff name="..."/>.
157 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
158 std::string name(arg);
159 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
160 }
161
162 bool SGCONF::DelTariffFunction(const SGCONF::CONFIG & config,
163                                const std::string & arg,
164                                const std::map<std::string, std::string> & /*options*/)
165 {
166 STG::SERVCONF proto(config.server.data(),
167                     config.port.data(),
168                     config.userName.data(),
169                     config.userPass.data());
170 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
171 }
172
173 bool SGCONF::AddTariffFunction(const SGCONF::CONFIG & config,
174                                const std::string & arg,
175                                const std::map<std::string, std::string> & /*options*/)
176 {
177 // TODO
178 std::cerr << "Unimplemented.\n";
179 return false;
180 }
181
182 bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config,
183                                const std::string & arg,
184                                const std::map<std::string, std::string> & options)
185 {
186 // TODO
187 std::cerr << "Unimplemented.\n";
188 return false;
189 }