]> git.stg.codes - stg.git/blob - projects/sgconf/tariffs.cpp
Cryptography with void* in the interfaces.
[stg.git] / projects / sgconf / tariffs.cpp
1 #include "tariffs.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6 #include "utils.h"
7
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/tariff_conf.h"
11 #include "stg/common.h"
12
13 #include <iostream>
14 #include <algorithm>
15 #include <sstream>
16 #include <string>
17 #include <map>
18 #include <cstdint>
19 #include <cassert>
20
21 namespace
22 {
23
24 std::string Indent(size_t level, bool dash = false)
25 {
26 if (level == 0)
27     return "";
28 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
29 }
30
31 std::string ChangePolicyToString(STG::Tariff::ChangePolicy changePolicy)
32 {
33 switch (changePolicy)
34     {
35     case STG::Tariff::ALLOW: return "allow";
36     case STG::Tariff::TO_CHEAP: return "to_cheap";
37     case STG::Tariff::TO_EXPENSIVE: return "to_expensive";
38     case STG::Tariff::DENY: return "deny";
39     }
40 return "unknown";
41 }
42
43 std::string PeriodToString(STG::Tariff::Period period)
44 {
45 switch (period)
46     {
47     case STG::Tariff::DAY:
48         return "daily";
49     case STG::Tariff::MONTH:
50         return "monthly";
51     }
52 return "unknown";
53 }
54
55 std::string TraffTypeToString(STG::Tariff::TraffType traffType)
56 {
57 switch (traffType)
58     {
59     case STG::Tariff::TRAFF_UP:
60         return "upload";
61     case STG::Tariff::TRAFF_DOWN:
62         return "download";
63     case STG::Tariff::TRAFF_UP_DOWN:
64         return "upload + download";
65     case STG::Tariff::TRAFF_MAX:
66         return "max(upload, download)";
67     }
68 return "unknown";
69 }
70
71 void ConvPeriod(const std::string & value, STG::Optional<STG::Tariff::Period> & res)
72 {
73 std::string lowered = ToLower(value);
74 if (lowered == "daily")
75     res = STG::Tariff::DAY;
76 else if (lowered == "monthly")
77     res = STG::Tariff::MONTH;
78 else
79     throw SGCONF::ACTION::ERROR("Period should be 'daily' or 'monthly'. Got: '" + value + "'");
80 }
81
82 void ConvChangePolicy(const std::string & value, STG::Optional<STG::Tariff::ChangePolicy> & res)
83 {
84 std::string lowered = ToLower(value);
85 if (lowered == "allow")
86     res = STG::Tariff::ALLOW;
87 else if (lowered == "to_cheap")
88     res = STG::Tariff::TO_CHEAP;
89 else if (lowered == "to_expensive")
90     res = STG::Tariff::TO_EXPENSIVE;
91 else if (lowered == "deny")
92     res = STG::Tariff::DENY;
93 else
94     throw SGCONF::ACTION::ERROR("Change policy should be 'allow', 'to_cheap', 'to_expensive' or 'deny'. Got: '" + value + "'");
95 }
96
97 void ConvChangePolicyTimeout(const std::string & value, STG::Optional<time_t> & res)
98 {
99 struct tm brokenTime;
100 if (stg_strptime(value.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime) == NULL)
101     throw SGCONF::ACTION::ERROR("Credit expiration should be in format 'YYYY-MM-DD HH:MM:SS'. Got: '" + value + "'");
102 res = stg_timegm(&brokenTime);
103 }
104
105 void ConvTraffType(const std::string & value, STG::Optional<STG::Tariff::TraffType> & res)
106 {
107 std::string lowered = ToLower(value);
108 lowered.erase(std::remove(lowered.begin(), lowered.end(), ' '), lowered.end());
109 if (lowered == "upload")
110     res = STG::Tariff::TRAFF_UP;
111 else if (lowered == "download")
112     res = STG::Tariff::TRAFF_DOWN;
113 else if (lowered == "upload+download")
114     res = STG::Tariff::TRAFF_UP_DOWN;
115 else if (lowered.substr(0, 3) == "max")
116     res = STG::Tariff::TRAFF_MAX;
117 else
118     throw SGCONF::ACTION::ERROR("Traff type should be 'upload', 'download', 'upload + download' or 'max'. Got: '" + value + "'");
119 }
120
121 STG::DirPriceDataOpt ConvTimeSpan(const std::string & value)
122 {
123 size_t dashPos = value.find_first_of('-');
124 if (dashPos == std::string::npos)
125     throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
126 size_t fromColon = value.find_first_of(':');
127 if (fromColon == std::string::npos || fromColon > dashPos)
128     throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
129 size_t toColon = value.find_first_of(':', dashPos);
130 if (toColon == std::string::npos)
131     throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
132 STG::DirPriceDataOpt res;
133 res.hDay = FromString<int>(value.substr(0, fromColon));
134 if (res.hDay.data() < 0 || res.hDay.data() > 23)
135     throw SGCONF::ACTION::ERROR("Invalid 'from' hours. Got: '" + value.substr(0, fromColon) + "'");
136 res.mDay = FromString<int>(value.substr(fromColon + 1, dashPos - fromColon - 1));
137 if (res.mDay.data() < 0 || res.mDay.data() > 59)
138     throw SGCONF::ACTION::ERROR("Invalid 'from' minutes. Got: '" + value.substr(fromColon + 1, dashPos - fromColon - 1) + "'");
139 res.hNight = FromString<int>(value.substr(dashPos + 1, toColon - dashPos - 1));
140 if (res.hNight.data() < 0 || res.hNight.data() > 23)
141     throw SGCONF::ACTION::ERROR("Invalid 'to' hours. Got: '" + value.substr(dashPos + 1, toColon - dashPos - 1) + "'");
142 res.mNight = FromString<int>(value.substr(toColon + 1, value.length() - toColon));
143 if (res.mNight.data() < 0 || res.mNight.data() > 59)
144     throw SGCONF::ACTION::ERROR("Invalid 'to' minutes. Got: '" + value.substr(toColon + 1, value.length() - toColon) + "'");
145 return res;
146 }
147
148 void splice(std::vector<STG::DirPriceDataOpt> & lhs, const std::vector<STG::DirPriceDataOpt> & rhs)
149 {
150 for (size_t i = 0; i < lhs.size() && i < rhs.size(); ++i)
151     lhs[i].splice(rhs[i]);
152 }
153
154 void ConvTimes(std::string value, std::vector<STG::DirPriceDataOpt> & res)
155 {
156 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
157 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvTimeSpan));
158 }
159
160 struct ConvPrice : public std::unary_function<std::string, STG::DirPriceDataOpt>
161 {
162     typedef STG::Optional<double> (STG::DirPriceDataOpt::* MemPtr);
163     ConvPrice(MemPtr before, MemPtr after)
164         : m_before(before), m_after(after)
165     {}
166
167     STG::DirPriceDataOpt operator()(const std::string & value)
168     {
169         STG::DirPriceDataOpt res;
170     size_t slashPos = value.find_first_of('/');
171     if (slashPos == std::string::npos)
172         {
173         double price = 0;
174         if (str2x(value, price) < 0)
175             throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value + "'");
176         (res.*m_before) = (res.*m_after) = price;
177         res.noDiscount = true;
178         }
179     else
180         {
181         double price = 0;
182         if (str2x(value.substr(0, slashPos), price) < 0)
183             throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(0, slashPos) + "'");
184         (res.*m_before) = price;
185         if (str2x(value.substr(slashPos + 1, value.length() - slashPos), price) < 0)
186             throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(slashPos + 1, value.length() - slashPos) + "'");
187         (res.*m_after) = price;
188         res.noDiscount = false;
189         }
190     return res;
191     }
192
193     MemPtr m_before;
194     MemPtr m_after;
195 };
196
197 void ConvDayPrices(std::string value, std::vector<STG::DirPriceDataOpt> & res)
198 {
199 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
200 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvPrice(&STG::DirPriceDataOpt::priceDayA, &STG::DirPriceDataOpt::priceDayB)));
201 }
202
203 void ConvNightPrices(std::string value, std::vector<STG::DirPriceDataOpt> & res)
204 {
205 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
206 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvPrice(&STG::DirPriceDataOpt::priceNightA, &STG::DirPriceDataOpt::priceNightB)));
207 }
208
209 STG::DirPriceDataOpt ConvThreshold(std::string value)
210 {
211     STG::DirPriceDataOpt res;
212 double threshold = 0;
213 if (str2x(value, threshold) < 0)
214     throw SGCONF::ACTION::ERROR("Threshold should be a floating point value. Got: '" + value + "'");
215 res.threshold = threshold;
216 return res;
217 }
218
219 void ConvThresholds(std::string value, std::vector<STG::DirPriceDataOpt> & res)
220 {
221 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
222 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvThreshold));
223 }
224
225 std::string TimeToString(int h, int m)
226 {
227 std::ostringstream stream;
228 stream << (h < 10 ? "0" : "") << h << ":"
229        << (m < 10 ? "0" : "") << m;
230 return stream.str();
231 }
232
233 void PrintDirPriceData(size_t dir, const STG::DirPriceData & data, size_t level)
234 {
235 std::string night = TimeToString(data.hNight, data.mNight);
236 std::string day = TimeToString(data.hDay, data.mDay);
237 std::cout << Indent(level, true) << "dir: " << dir << "\n"
238           << Indent(level)       << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n"
239           << Indent(level)       << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n"
240           << Indent(level)       << "threshold: " << data.threshold << "\n"
241           << Indent(level)       << "single price: " << (data.singlePrice ? "yes" : "no") << "\n"
242           << Indent(level)       << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention!
243 }
244
245 void PrintTariffConf(const STG::TariffConf & conf, size_t level)
246 {
247 std::cout << Indent(level, true) << "name: " << conf.name << "\n"
248           << Indent(level)       << "fee: " << conf.fee << "\n"
249           << Indent(level)       << "free mb: " << conf.free << "\n"
250           << Indent(level)       << "passive cost: " << conf.passiveCost << "\n"
251           << Indent(level)       << "traff type: " << TraffTypeToString(conf.traffType) << "\n"
252           << Indent(level)       << "period: " << PeriodToString(conf.period) << "\n"
253           << Indent(level)       << "change policy: " << ChangePolicyToString(conf.changePolicy) << "\n"
254           << Indent(level)       << "change policy timeout: " << formatTime(conf.changePolicyTimeout) << "\n";
255 }
256
257 void PrintTariff(const STG::GetTariff::Info & info, size_t level = 0)
258 {
259 PrintTariffConf(info.tariffConf, level);
260 std::cout << Indent(level) << "dir prices:\n";
261 for (size_t i = 0; i < info.dirPrice.size(); ++i)
262     PrintDirPriceData(i, info.dirPrice[i], level + 1);
263 }
264
265 std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
266 {
267 std::vector<SGCONF::API_ACTION::PARAM> params;
268 params.push_back(SGCONF::API_ACTION::PARAM("fee", "<fee>", "\t\ttariff fee"));
269 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
270 params.push_back(SGCONF::API_ACTION::PARAM("passive-cost", "<cost>", "\tpassive cost"));
271 params.push_back(SGCONF::API_ACTION::PARAM("traff-type", "<type>", "\ttraffic type (up, down, up+down, max)"));
272 params.push_back(SGCONF::API_ACTION::PARAM("period", "<period>", "\ttarification period (daily, monthly)"));
273 params.push_back(SGCONF::API_ACTION::PARAM("change-policy", "<policy>", "tariff change policy (allow, to_cheap, to_expensive, deny)"));
274 params.push_back(SGCONF::API_ACTION::PARAM("change-policy-timeout", "<yyyy-mm-dd hh:mm:ss>", "tariff change policy timeout"));
275 params.push_back(SGCONF::API_ACTION::PARAM("times", "<hh:mm-hh:mm, ...>", "coma-separated day time-spans for each direction"));
276 params.push_back(SGCONF::API_ACTION::PARAM("day-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
277 params.push_back(SGCONF::API_ACTION::PARAM("night-prices", "<price/price, ...>", "coma-separated night prices for each direction"));
278 params.push_back(SGCONF::API_ACTION::PARAM("thresholds", "<threshold, ...>", "coma-separated thresholds for each direction"));
279 return params;
280 }
281
282 void SimpleCallback(bool result,
283                     const std::string & reason,
284                     void * /*data*/)
285 {
286 if (!result)
287     {
288     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
289     return;
290     }
291 std::cout << "Success.\n";
292 }
293
294 void GetTariffsCallback(bool result,
295                         const std::string & reason,
296                         const std::vector<STG::GetTariff::Info> & info,
297                         void * /*data*/)
298 {
299 if (!result)
300     {
301     std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
302     return;
303     }
304 std::cout << "Tariffs:\n";
305 for (size_t i = 0; i < info.size(); ++i)
306     PrintTariff(info[i], 1);
307 }
308
309 void GetTariffCallback(bool result,
310                        const std::string & reason,
311                        const std::vector<STG::GetTariff::Info> & info,
312                        void * data)
313 {
314 assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
315 const std::string & name = *static_cast<const std::string *>(data);
316 if (!result)
317     {
318     std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
319     return;
320     }
321 for (size_t i = 0; i < info.size(); ++i)
322     if (info[i].tariffConf.name == name)
323         PrintTariff(info[i]);
324 }
325
326 bool GetTariffsFunction(const SGCONF::CONFIG & config,
327                         const std::string & /*arg*/,
328                         const std::map<std::string, std::string> & /*options*/)
329 {
330 STG::ServConf proto(config.server.data(),
331                     config.port.data(),
332                     config.localAddress.data(),
333                     config.localPort.data(),
334                     config.userName.data(),
335                     config.userPass.data());
336 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
337 }
338
339 bool GetTariffFunction(const SGCONF::CONFIG & config,
340                        const std::string & arg,
341                        const std::map<std::string, std::string> & /*options*/)
342 {
343 STG::ServConf proto(config.server.data(),
344                     config.port.data(),
345                     config.localAddress.data(),
346                     config.localPort.data(),
347                     config.userName.data(),
348                     config.userPass.data());
349 // STG currently doesn't support <GetTariff name="..."/>.
350 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
351 std::string name(arg);
352 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
353 }
354
355 bool DelTariffFunction(const SGCONF::CONFIG & config,
356                        const std::string & arg,
357                        const std::map<std::string, std::string> & /*options*/)
358 {
359 STG::ServConf proto(config.server.data(),
360                     config.port.data(),
361                     config.localAddress.data(),
362                     config.localPort.data(),
363                     config.userName.data(),
364                     config.userPass.data());
365 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
366 }
367
368 bool AddTariffFunction(const SGCONF::CONFIG & config,
369                        const std::string & arg,
370                        const std::map<std::string, std::string> & options)
371 {
372 STG::TariffDataOpt conf;
373 conf.tariffConf.name = arg;
374 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
375 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
376 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
377 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
378 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
379 SGCONF::MaybeSet(options, "change-policy", conf.tariffConf.changePolicy, ConvChangePolicy);
380 SGCONF::MaybeSet(options, "change-policy-timeout", conf.tariffConf.changePolicyTimeout, ConvChangePolicyTimeout);
381 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
382 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
383 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
384 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
385 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
386     {
387     if (!conf.dirPrice[i].priceDayA.empty() &&
388         !conf.dirPrice[i].priceNightA.empty() &&
389         !conf.dirPrice[i].priceDayB.empty() &&
390         !conf.dirPrice[i].priceNightB.empty())
391         conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.data() == conf.dirPrice[i].priceNightA.data() &&
392                                        conf.dirPrice[i].priceDayB.data() == conf.dirPrice[i].priceNightB.data();
393     }
394 STG::ServConf proto(config.server.data(),
395                     config.port.data(),
396                     config.localAddress.data(),
397                     config.localPort.data(),
398                     config.userName.data(),
399                     config.userPass.data());
400 return proto.AddTariff(arg, conf, SimpleCallback, NULL) == STG::st_ok;
401 }
402
403 bool ChgTariffFunction(const SGCONF::CONFIG & config,
404                        const std::string & arg,
405                        const std::map<std::string, std::string> & options)
406 {
407 STG::TariffDataOpt conf;
408 conf.tariffConf.name = arg;
409 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
410 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
411 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
412 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
413 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
414 SGCONF::MaybeSet(options, "change-policy", conf.tariffConf.changePolicy, ConvChangePolicy);
415 SGCONF::MaybeSet(options, "change-policy-timeout", conf.tariffConf.changePolicyTimeout, ConvChangePolicyTimeout);
416 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
417 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
418 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
419 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
420 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
421     {
422     if (!conf.dirPrice[i].priceDayA.empty() &&
423         !conf.dirPrice[i].priceNightA.empty() &&
424         !conf.dirPrice[i].priceDayB.empty() &&
425         !conf.dirPrice[i].priceNightB.empty())
426         conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.data() == conf.dirPrice[i].priceNightA.data() &&
427                                        conf.dirPrice[i].priceDayB.data() == conf.dirPrice[i].priceNightB.data();
428     }
429 STG::ServConf proto(config.server.data(),
430                     config.port.data(),
431                     config.localAddress.data(),
432                     config.localPort.data(),
433                     config.userName.data(),
434                     config.userPass.data());
435 return proto.ChgTariff(conf, SimpleCallback, NULL) == STG::st_ok;
436 }
437
438 } // namespace anonymous
439
440 void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
441 {
442 std::vector<API_ACTION::PARAM> params(GetTariffParams());
443 blocks.Add("Tariff management options")
444       .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
445       .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", GetTariffFunction), "get tariff")
446       .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, AddTariffFunction), "add tariff")
447       .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", DelTariffFunction), "delete tariff")
448       .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, ChgTariffFunction), "change tariff");
449 }