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"
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(STG::Tariff::ChangePolicy changePolicy)
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";
43 std::string PeriodToString(STG::Tariff::Period period)
47 case STG::Tariff::DAY:
49 case STG::Tariff::MONTH:
55 std::string TraffTypeToString(STG::Tariff::TraffType traffType)
59 case STG::Tariff::TRAFF_UP:
61 case STG::Tariff::TRAFF_DOWN:
63 case STG::Tariff::TRAFF_UP_DOWN:
64 return "upload + download";
65 case STG::Tariff::TRAFF_MAX:
66 return "max(upload, download)";
71 void ConvPeriod(const std::string & value, STG::Optional<STG::Tariff::Period> & res)
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;
79 throw SGCONF::ACTION::ERROR("Period should be 'daily' or 'monthly'. Got: '" + value + "'");
82 void ConvChangePolicy(const std::string & value, STG::Optional<STG::Tariff::ChangePolicy> & res)
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;
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, STG::Optional<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, STG::Optional<STG::Tariff::TraffType> & res)
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;
118 throw SGCONF::ACTION::ERROR("Traff type should be 'upload', 'download', 'upload + download' or 'max'. Got: '" + value + "'");
121 STG::DirPriceDataOpt 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 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) + "'");
148 void splice(std::vector<STG::DirPriceDataOpt> & lhs, const std::vector<STG::DirPriceDataOpt> & 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<STG::DirPriceDataOpt> & res)
156 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
157 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvTimeSpan));
160 struct ConvPrice : public std::unary_function<std::string, STG::DirPriceDataOpt>
162 typedef STG::Optional<double> (STG::DirPriceDataOpt::* MemPtr);
163 ConvPrice(MemPtr before, MemPtr after)
164 : m_before(before), m_after(after)
167 STG::DirPriceDataOpt operator()(const std::string & value)
169 STG::DirPriceDataOpt 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<STG::DirPriceDataOpt> & res)
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)));
203 void ConvNightPrices(std::string value, std::vector<STG::DirPriceDataOpt> & res)
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)));
209 STG::DirPriceDataOpt ConvThreshold(std::string value)
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;
219 void ConvThresholds(std::string value, std::vector<STG::DirPriceDataOpt> & res)
221 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
222 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(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 STG::DirPriceData & 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 STG::TariffConf & 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::GetTariff::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::GetTariff::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::GetTariff::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 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)
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 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)
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");