2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2010/08/19 13:42:30 $
35 #include "stg/logger.h"
36 #include "stg/dotconfpp.h"
37 #include "settings_impl.h"
39 //-----------------------------------------------------------------------------
40 SETTINGS_IMPL::SETTINGS_IMPL()
43 modulesPath("/usr/lib/stg"),
45 confDir("/etc/stargazer"),
46 scriptsDir("/etc/stargazer"),
47 rules("/etc/stargazer/rules"),
48 logFile("/var/log/stargazer.log"),
49 pidFile("/var/run/stargazer.pid"),
50 monitorDir("/var/stargazer/monitoring"),
52 detailStatWritePeriod(dsPeriod_1_6),
60 freeMbAllowInet(false),
61 dayFeeIsLastDay(false),
62 writeFreeMbTraffCost(false),
66 reconnectOnTariffChange(false),
68 storeModuleSettings(),
69 logger(GetStgLogger())
72 //-----------------------------------------------------------------------------
73 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
76 modulesPath("/usr/lib/stg"),
81 logFile("/var/log/stargazer.log"),
82 pidFile("/var/run/stargazer.pid"),
83 monitorDir("/var/stargazer/monitoring"),
85 detailStatWritePeriod(dsPeriod_1_6),
93 freeMbAllowInet(false),
94 dayFeeIsLastDay(false),
95 writeFreeMbTraffCost(false),
99 reconnectOnTariffChange(false),
101 storeModuleSettings(),
102 logger(GetStgLogger())
105 //-----------------------------------------------------------------------------
106 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
109 modulesPath(rval.modulesPath),
110 dirName(rval.dirName),
111 confDir(rval.confDir),
112 scriptsDir(rval.scriptsDir),
114 logFile(rval.logFile),
115 pidFile(rval.pidFile),
116 monitorDir(rval.monitorDir),
117 monitoring(rval.monitoring),
118 detailStatWritePeriod(rval.detailStatWritePeriod),
119 statWritePeriod(rval.statWritePeriod),
120 stgExecMsgKey(rval.stgExecMsgKey),
121 executersNum(rval.executersNum),
122 fullFee(rval.fullFee),
124 dayResetTraff(rval.dayResetTraff),
125 spreadFee(rval.spreadFee),
126 freeMbAllowInet(rval.freeMbAllowInet),
127 dayFeeIsLastDay(rval.dayFeeIsLastDay),
128 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
129 showFeeInCash(rval.showFeeInCash),
130 messageTimeout(rval.messageTimeout),
131 feeChargeType(rval.feeChargeType),
132 reconnectOnTariffChange(rval.reconnectOnTariffChange),
133 modulesSettings(rval.modulesSettings),
134 storeModuleSettings(rval.storeModuleSettings),
135 logger(GetStgLogger())
138 //-----------------------------------------------------------------------------
139 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
141 const DOTCONFDocumentNode * childNode;
145 pv.param = node->getName();
147 if (node->getValue(1))
149 strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'.";
153 value = node->getValue(0);
157 strError = "Module name expected.";
161 childNode = node->getChildNode();
164 pv.param = childNode->getName();
166 while ((value = childNode->getValue(i++)) != NULL)
168 pv.value.push_back(value);
170 params->push_back(pv);
172 childNode = childNode->getNextNode();
177 //-----------------------------------------------------------------------------
178 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
180 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
181 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
182 settings->logger("%s", buf);
184 //-----------------------------------------------------------------------------
185 int SETTINGS_IMPL::ReadSettings()
187 const char * requiredOptions[] = {
193 "DetailStatWritePeriod",
199 "WriteFreeMbTraffCost",
202 int storeModulesCount = 0;
203 modulesSettings.clear();
205 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
206 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
207 conf.setRequiredOptionNames(requiredOptions);
208 std::string confFile = confDir + "/stargazer.conf";
210 if(conf.setContent(confFile.c_str()) != 0)
212 strError = "Cannot read file " + confFile;
216 const DOTCONFDocumentNode * node = conf.getFirstNode();
220 if (strcasecmp(node->getName(), "ScriptDir") == 0)
222 scriptsDir = node->getValue(0);
225 if (strcasecmp(node->getName(), "LogFile") == 0)
227 logFile = node->getValue(0);
230 if (strcasecmp(node->getName(), "PIDFile") == 0)
232 pidFile = node->getValue(0);
235 if (strcasecmp(node->getName(), "ModulesPath") == 0)
237 modulesPath = node->getValue(0);
240 if (strcasecmp(node->getName(), "Rules") == 0)
242 rules = node->getValue(0);
245 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
247 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
249 strError = "Incorrect DetailStatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
254 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
256 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
258 strError = "Incorrect StatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
263 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
265 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
267 strError = "Incorrect ExecMsgKey value: \'" + std::string(node->getValue(0)) + "\'";
272 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
274 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
276 strError = "Incorrect ExecutersNum value: \'" + std::string(node->getValue(0)) + "\'";
281 if (strcasecmp(node->getName(), "DayFee") == 0)
283 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
285 strError = "Incorrect DayFee value: \'" + std::string(node->getValue(0)) + "\'";
290 if (strcasecmp(node->getName(), "FullFee") == 0)
292 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
294 strError = "Incorrect FullFee value: \'" + std::string(node->getValue(0)) + "\'";
299 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
301 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
303 strError = "Incorrect DayResetTraff value: \'" + std::string(node->getValue(0)) + "\'";
308 if (strcasecmp(node->getName(), "SpreadFee") == 0)
310 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
312 strError = "Incorrect SpreadFee value: \'" + std::string(node->getValue(0)) + "\'";
317 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
319 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
321 strError = "Incorrect FreeMbAllowInet value: \'" + std::string(node->getValue(0)) + "\'";
326 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
328 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
330 strError = "Incorrect DayFeeIsLastDay value: \'" + std::string(node->getValue(0)) + "\'";
335 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
337 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
339 strError = "Incorrect WriteFreeMbTraffCost value: \'" + std::string(node->getValue(0)) + "\'";
344 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
346 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
348 strError = "Incorrect ShowFeeInCash value: \'" + std::string(node->getValue(0)) + "\'";
353 if (strcasecmp(node->getName(), "MonitorDir") == 0)
355 monitorDir = node->getValue(0);
359 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
365 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
367 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
369 strError = "Incorrect MessageTimeout value: \'" + std::string(node->getValue(0)) + "\'";
374 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
376 if (ParseUnsignedInRange(node->getValue(0), 0, 3, &feeChargeType) != 0)
378 strError = "Incorrect FeeChargeType value: \'" + std::string(node->getValue(0)) + "\'";
383 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
385 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
387 strError = "Incorrect ReconnectOnTariffChange value: \'" + std::string(node->getValue(0)) + "\'";
392 if (strcasecmp(node->getName(), "DirNames") == 0)
394 const DOTCONFDocumentNode * child = node->getChildNode();
397 const DOTCONFDocumentNode * dirNameNode;
398 dirName.reserve(DIR_NUM);
399 for (int i = 0; i < DIR_NUM; i++)
402 sprintf(strDirName, "DirName%d", i);
403 dirNameNode = conf.findNode(strDirName, node);
404 if (dirNameNode && dirNameNode->getValue(0))
406 dirName[i] = dirNameNode->getValue(0);
412 if (strcasecmp(node->getName(), "StoreModule") == 0)
414 if (node->getValue(1))
416 strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
420 if (storeModulesCount)
422 strError = "Should be only one StoreModule.";
427 storeModuleSettings.moduleName = node->getValue(0);
428 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
431 if (strcasecmp(node->getName(), "Modules") == 0)
433 if (node->getValue(0))
435 strError = "Unexpected \'" + std::string(node->getValue(0)) + "\'.";
438 const DOTCONFDocumentNode * child = node->getChildNode();
441 if (strcasecmp(child->getName(), "Module") != 0)
443 child = child->getNextNode();
446 MODULE_SETTINGS modSettings;
447 modSettings.moduleParams.clear();
448 modSettings.moduleName = child->getValue();
450 ParseModuleSettings(child, &modSettings.moduleParams);
452 modulesSettings.push_back(modSettings);
454 child = child->getNextNode();
458 if (strcasecmp(node->getName(), "ScriptParams") == 0)
460 for (int i = 0; node->getValue(i) != NULL; ++i)
462 scriptParams.push_back(node->getValue(i));
465 node = node->getNextNode();
470 //-----------------------------------------------------------------------------
471 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const std::string & detailStatPeriodStr)
473 if (detailStatPeriodStr == "1")
475 detailStatWritePeriod = dsPeriod_1;
478 else if (detailStatPeriodStr == "1/2")
480 detailStatWritePeriod = dsPeriod_1_2;
483 else if (detailStatPeriodStr == "1/4")
485 detailStatWritePeriod = dsPeriod_1_4;
488 else if (detailStatPeriodStr == "1/6")
490 detailStatWritePeriod = dsPeriod_1_6;
496 //-----------------------------------------------------------------------------