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]);
134 } // namespace anonymous
136 bool SGCONF::GetTariffsFunction(const SGCONF::CONFIG & config,
137 const std::string & /*arg*/,
138 const std::map<std::string, std::string> & /*options*/)
140 STG::SERVCONF proto(config.server.data(),
142 config.userName.data(),
143 config.userPass.data());
144 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
147 bool SGCONF::GetTariffFunction(const SGCONF::CONFIG & config,
148 const std::string & arg,
149 const std::map<std::string, std::string> & /*options*/)
151 STG::SERVCONF proto(config.server.data(),
153 config.userName.data(),
154 config.userPass.data());
155 // STG currently doesn't support <GetTariff name="..."/>.
156 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
157 std::string name(arg);
158 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
161 bool SGCONF::DelTariffFunction(const SGCONF::CONFIG & config,
162 const std::string & arg,
163 const std::map<std::string, std::string> & /*options*/)
165 STG::SERVCONF proto(config.server.data(),
167 config.userName.data(),
168 config.userPass.data());
169 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
172 bool SGCONF::AddTariffFunction(const SGCONF::CONFIG & config,
173 const std::string & arg,
174 const std::map<std::string, std::string> & /*options*/)
177 std::cerr << "Unimplemented.\n";
181 bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config,
182 const std::string & arg,
183 const std::map<std::string, std::string> & options)
186 std::cerr << "Unimplemented.\n";