5 #include "stg/servconf.h"
6 #include "stg/servconf_types.h"
7 #include "stg/tariff_conf.h"
8 #include "stg/os_int.h"
17 std::string Indent(size_t level, bool dash = false)
21 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
24 std::string PeriodToString(TARIFF::PERIOD period)
36 std::string TraffTypeToString(int traffType)
45 return "upload + download";
47 return "max(upload, download)";
52 std::string TimeToString(int h, int m)
54 std::ostringstream stream;
55 stream << (h < 10 ? "0" : "") << h << ":"
56 << (m < 10 ? "0" : "") << m;
60 void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level)
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!
72 void PrintTariffConf(const TARIFF_CONF & conf, size_t level)
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";
82 void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0)
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);
90 void SimpleCallback(bool result,
91 const std::string & reason,
96 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
99 std::cout << "Success.\n";
102 void GetTariffsCallback(bool result,
103 const std::string & reason,
104 const std::vector<STG::GET_TARIFF::INFO> & info,
109 std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
112 std::cout << "Tariffs:\n";
113 for (size_t i = 0; i < info.size(); ++i)
114 PrintTariff(info[i], 1);
117 void GetTariffCallback(bool result,
118 const std::string & reason,
119 const std::vector<STG::GET_TARIFF::INFO> & info,
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);
126 std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
129 for (size_t i = 0; i < info.size(); ++i)
130 if (info[i].tariffConf.name == name)
131 PrintTariff(info[i]);
137 bool SGCONF::GetTariffsFunction(const SGCONF::CONFIG & config,
138 const std::string & /*arg*/,
139 const std::map<std::string, std::string> & /*options*/)
141 STG::SERVCONF proto(config.server.data(),
143 config.userName.data(),
144 config.userPass.data());
145 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
148 bool SGCONF::GetTariffFunction(const SGCONF::CONFIG & config,
149 const std::string & arg,
150 const std::map<std::string, std::string> & /*options*/)
152 STG::SERVCONF proto(config.server.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;
162 bool SGCONF::DelTariffFunction(const SGCONF::CONFIG & config,
163 const std::string & arg,
164 const std::map<std::string, std::string> & /*options*/)
166 STG::SERVCONF proto(config.server.data(),
168 config.userName.data(),
169 config.userPass.data());
170 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
173 bool SGCONF::AddTariffFunction(const SGCONF::CONFIG & config,
174 const std::string & arg,
175 const std::map<std::string, std::string> & /*options*/)
178 std::cerr << "Unimplemented.\n";
182 bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config,
183 const std::string & arg,
184 const std::map<std::string, std::string> & options)
187 std::cerr << "Unimplemented.\n";