3 #include "api_action.h"
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/tariff_conf.h"
11 #include "stg/common.h"
12 #include "stg/os_int.h"
24 std::string Indent(size_t level, bool dash = false)
28 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
31 std::string ChangePolicyToString(TARIFF::CHANGE_POLICY changePolicy)
35 case TARIFF::ALLOW: return "allow";
36 case TARIFF::TO_CHEAP: return "to_cheap";
37 case TARIFF::TO_EXPENSIVE: return "to_expensive";
38 case TARIFF::DENY: return "deny";
43 std::string PeriodToString(TARIFF::PERIOD period)
55 std::string TraffTypeToString(TARIFF::TRAFF_TYPE traffType)
59 case TARIFF::TRAFF_UP:
61 case TARIFF::TRAFF_DOWN:
63 case TARIFF::TRAFF_UP_DOWN:
64 return "upload + download";
65 case TARIFF::TRAFF_MAX:
66 return "max(upload, download)";
71 void ConvPeriod(const std::string & value, RESETABLE<TARIFF::PERIOD> & res)
73 std::string lowered = ToLower(value);
74 if (lowered == "daily")
76 else if (lowered == "monthly")
79 throw SGCONF::ACTION::ERROR("Period should be 'daily' or 'monthly'. Got: '" + value + "'");
82 void ConvChangePolicy(const std::string & value, RESETABLE<TARIFF::CHANGE_POLICY> & res)
84 std::string lowered = ToLower(value);
85 if (lowered == "allow")
87 else if (lowered == "to_cheap")
88 res = TARIFF::TO_CHEAP;
89 else if (lowered == "to_expensive")
90 res = TARIFF::TO_EXPENSIVE;
91 else if (lowered == "deny")
94 throw SGCONF::ACTION::ERROR("Change policy should be 'allow', 'to_cheap', 'to_expensive' or 'deny'. Got: '" + value + "'");
97 void ConvChangePolicyTimeout(const std::string & value, RESETABLE<time_t> & res)
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);
105 void ConvTraffType(const std::string & value, RESETABLE<TARIFF::TRAFF_TYPE> & res)
107 std::string lowered = ToLower(value);
108 lowered.erase(std::remove(lowered.begin(), lowered.end(), ' '), lowered.end());
109 if (lowered == "upload")
110 res = TARIFF::TRAFF_UP;
111 else if (lowered == "download")
112 res = TARIFF::TRAFF_DOWN;
113 else if (lowered == "upload+download")
114 res = TARIFF::TRAFF_UP_DOWN;
115 else if (lowered.substr(0, 3) == "max")
116 res = TARIFF::TRAFF_MAX;
118 throw SGCONF::ACTION::ERROR("Traff type should be 'upload', 'download', 'upload + download' or 'max'. Got: '" + value + "'");
121 DIRPRICE_DATA_RES ConvTimeSpan(const std::string & value)
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 DIRPRICE_DATA_RES 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) + "'");
148 void Splice(std::vector<DIRPRICE_DATA_RES> & lhs, const std::vector<DIRPRICE_DATA_RES> & rhs)
150 for (size_t i = 0; i < lhs.size() && i < rhs.size(); ++i)
151 lhs[i].Splice(rhs[i]);
154 void ConvTimes(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
156 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
157 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvTimeSpan));
160 struct ConvPrice : public std::unary_function<std::string, DIRPRICE_DATA_RES>
162 typedef RESETABLE<double> (DIRPRICE_DATA_RES::* MemPtr);
163 ConvPrice(MemPtr before, MemPtr after)
164 : m_before(before), m_after(after)
167 DIRPRICE_DATA_RES operator()(const std::string & value)
169 DIRPRICE_DATA_RES res;
170 size_t slashPos = value.find_first_of('/');
171 if (slashPos == std::string::npos)
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;
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;
197 void ConvDayPrices(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
199 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
200 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvPrice(&DIRPRICE_DATA_RES::priceDayA, &DIRPRICE_DATA_RES::priceDayB)));
203 void ConvNightPrices(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
205 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
206 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvPrice(&DIRPRICE_DATA_RES::priceNightA, &DIRPRICE_DATA_RES::priceNightB)));
209 DIRPRICE_DATA_RES ConvThreshold(std::string value)
211 DIRPRICE_DATA_RES 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;
219 void ConvThresholds(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
221 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
222 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvThreshold));
225 std::string TimeToString(int h, int m)
227 std::ostringstream stream;
228 stream << (h < 10 ? "0" : "") << h << ":"
229 << (m < 10 ? "0" : "") << m;
233 void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level)
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!
245 void PrintTariffConf(const TARIFF_CONF & conf, size_t level)
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";
257 void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0)
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);
265 std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
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"));
282 void SimpleCallback(bool result,
283 const std::string & reason,
288 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
291 std::cout << "Success.\n";
294 void GetTariffsCallback(bool result,
295 const std::string & reason,
296 const std::vector<STG::GET_TARIFF::INFO> & info,
301 std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
304 std::cout << "Tariffs:\n";
305 for (size_t i = 0; i < info.size(); ++i)
306 PrintTariff(info[i], 1);
309 void GetTariffCallback(bool result,
310 const std::string & reason,
311 const std::vector<STG::GET_TARIFF::INFO> & info,
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);
318 std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
321 for (size_t i = 0; i < info.size(); ++i)
322 if (info[i].tariffConf.name == name)
323 PrintTariff(info[i]);
326 bool GetTariffsFunction(const SGCONF::CONFIG & config,
327 const std::string & /*arg*/,
328 const std::map<std::string, std::string> & /*options*/)
330 STG::SERVCONF proto(config.server.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;
339 bool GetTariffFunction(const SGCONF::CONFIG & config,
340 const std::string & arg,
341 const std::map<std::string, std::string> & /*options*/)
343 STG::SERVCONF proto(config.server.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;
355 bool DelTariffFunction(const SGCONF::CONFIG & config,
356 const std::string & arg,
357 const std::map<std::string, std::string> & /*options*/)
359 STG::SERVCONF proto(config.server.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;
368 bool AddTariffFunction(const SGCONF::CONFIG & config,
369 const std::string & arg,
370 const std::map<std::string, std::string> & options)
372 TARIFF_DATA_RES 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)
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();
394 STG::SERVCONF proto(config.server.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;
403 bool ChgTariffFunction(const SGCONF::CONFIG & config,
404 const std::string & arg,
405 const std::map<std::string, std::string> & options)
407 TARIFF_DATA_RES 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)
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();
429 STG::SERVCONF proto(config.server.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;
438 } // namespace anonymous
440 void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
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");