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"
41 //-----------------------------------------------------------------------------
42 SETTINGS_IMPL::SETTINGS_IMPL()
44 modulesPath("/usr/lib/stg"),
46 confDir("/etc/stargazer"),
47 scriptsDir("/etc/stargazer"),
48 rules("/etc/stargazer/rules"),
49 logFile("/var/log/stargazer.log"),
50 pidFile("/var/run/stargazer.pid"),
51 monitorDir("/var/stargazer/monitoring"),
53 detailStatWritePeriod(dsPeriod_1_6),
61 freeMbAllowInet(false),
62 dayFeeIsLastDay(false),
63 writeFreeMbTraffCost(false),
67 reconnectOnTariffChange(false),
69 storeModuleSettings(),
70 logger(GetStgLogger())
73 //-----------------------------------------------------------------------------
74 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)
108 modulesPath(rval.modulesPath),
109 dirName(rval.dirName),
110 confDir(rval.confDir),
111 scriptsDir(rval.scriptsDir),
113 logFile(rval.logFile),
114 pidFile(rval.pidFile),
115 monitorDir(rval.monitorDir),
116 monitoring(rval.monitoring),
117 detailStatWritePeriod(rval.detailStatWritePeriod),
118 statWritePeriod(rval.statWritePeriod),
119 stgExecMsgKey(rval.stgExecMsgKey),
120 executersNum(rval.executersNum),
121 fullFee(rval.fullFee),
123 dayResetTraff(rval.dayResetTraff),
124 spreadFee(rval.spreadFee),
125 freeMbAllowInet(rval.freeMbAllowInet),
126 dayFeeIsLastDay(rval.dayFeeIsLastDay),
127 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
128 showFeeInCash(rval.showFeeInCash),
129 messageTimeout(rval.messageTimeout),
130 feeChargeType(rval.feeChargeType),
131 reconnectOnTariffChange(rval.reconnectOnTariffChange),
132 modulesSettings(rval.modulesSettings),
133 storeModuleSettings(rval.storeModuleSettings),
134 logger(GetStgLogger())
137 //-----------------------------------------------------------------------------
138 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
140 if (0 == strcasecmp(value.c_str(), "yes"))
145 if (0 == strcasecmp(value.c_str(), "no"))
151 strError = "Incorrect value \'" + value + "\'.";
154 //-----------------------------------------------------------------------------
155 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
157 if (str2x<int>(value, *val))
159 strError = "Cannot convert \'" + value + "\' to integer.";
164 //-----------------------------------------------------------------------------
165 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
167 if (str2x<unsigned>(value, *val))
169 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
174 //-----------------------------------------------------------------------------
175 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
177 if (ParseInt(value, val) != 0)
180 if (*val < min || *val > max)
182 strError = "Value \'" + value + "\' out of range.";
188 //-----------------------------------------------------------------------------
189 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
191 if (ParseUnsigned(value, val) != 0)
194 if (*val < min || *val > max)
196 strError = "Value \'" + value + "\' out of range.";
202 //-----------------------------------------------------------------------------
203 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
205 const DOTCONFDocumentNode * childNode;
209 pv.param = node->getName();
211 if (node->getValue(1))
213 strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
217 value = node->getValue(0);
221 strError = "Module name expected.";
225 childNode = node->getChildNode();
228 pv.param = childNode->getName();
230 while ((value = childNode->getValue(i++)) != NULL)
232 pv.value.push_back(value);
234 params->push_back(pv);
236 childNode = childNode->getNextNode();
241 //-----------------------------------------------------------------------------
242 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
244 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
245 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
246 settings->logger("%s", buf);
248 //-----------------------------------------------------------------------------
249 int SETTINGS_IMPL::ReadSettings()
251 const char * requiredOptions[] = {
257 "DetailStatWritePeriod",
263 "WriteFreeMbTraffCost",
266 int storeModulesCount = 0;
267 modulesSettings.clear();
269 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
270 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
271 conf.setRequiredOptionNames(requiredOptions);
272 string confFile = confDir + "/stargazer.conf";
274 if(conf.setContent(confFile.c_str()) != 0)
276 strError = "Cannot read file " + confFile;
280 const DOTCONFDocumentNode * node = conf.getFirstNode();
284 if (strcasecmp(node->getName(), "ScriptDir") == 0)
286 scriptsDir = node->getValue(0);
289 if (strcasecmp(node->getName(), "LogFile") == 0)
291 logFile = node->getValue(0);
294 if (strcasecmp(node->getName(), "PIDFile") == 0)
296 pidFile = node->getValue(0);
299 if (strcasecmp(node->getName(), "ModulesPath") == 0)
301 modulesPath = node->getValue(0);
304 if (strcasecmp(node->getName(), "Rules") == 0)
306 rules = node->getValue(0);
309 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
311 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
313 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
318 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
320 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
322 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
327 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
329 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
331 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
336 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
338 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
340 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
345 if (strcasecmp(node->getName(), "DayFee") == 0)
347 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
349 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
354 if (strcasecmp(node->getName(), "FullFee") == 0)
356 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
358 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
363 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
365 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
367 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
372 if (strcasecmp(node->getName(), "SpreadFee") == 0)
374 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
376 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
381 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
383 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
385 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
390 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
392 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
394 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
399 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
401 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
403 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
408 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
410 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
412 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
417 if (strcasecmp(node->getName(), "MonitorDir") == 0)
419 monitorDir = node->getValue(0);
423 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
429 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
431 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
433 strError = "Incorrect MessageTimeout value: \'" + string(node->getValue(0)) + "\'";
438 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
440 if (ParseUnsignedInRange(node->getValue(0), 0, 2, &feeChargeType) != 0)
442 strError = "Incorrect FeeChargeType value: \'" + string(node->getValue(0)) + "\'";
447 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
449 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
451 strError = "Incorrect ReconnectOnTariffChange value: \'" + string(node->getValue(0)) + "\'";
456 if (strcasecmp(node->getName(), "DirNames") == 0)
458 const DOTCONFDocumentNode * child = node->getChildNode();
461 const DOTCONFDocumentNode * dirNameNode;
462 dirName.reserve(DIR_NUM);
463 for (int i = 0; i < DIR_NUM; i++)
466 sprintf(strDirName, "DirName%d", i);
467 dirNameNode = conf.findNode(strDirName, node);
468 if (dirNameNode && dirNameNode->getValue(0))
470 dirName[i] = dirNameNode->getValue(0);
476 if (strcasecmp(node->getName(), "StoreModule") == 0)
478 if (node->getValue(1))
480 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
484 if (storeModulesCount)
486 strError = "Should be only one StoreModule.";
491 storeModuleSettings.moduleName = node->getValue(0);
492 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
495 if (strcasecmp(node->getName(), "Modules") == 0)
497 if (node->getValue(0))
499 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
502 const DOTCONFDocumentNode * child = node->getChildNode();
505 if (strcasecmp(child->getName(), "Module") != 0)
507 child = child->getNextNode();
510 MODULE_SETTINGS modSettings;
511 modSettings.moduleParams.clear();
512 modSettings.moduleName = child->getValue();
514 ParseModuleSettings(child, &modSettings.moduleParams);
516 modulesSettings.push_back(modSettings);
518 child = child->getNextNode();
522 node = node->getNextNode();
527 //-----------------------------------------------------------------------------
528 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
530 if (detailStatPeriodStr == "1")
532 detailStatWritePeriod = dsPeriod_1;
535 else if (detailStatPeriodStr == "1/2")
537 detailStatWritePeriod = dsPeriod_1_2;
540 else if (detailStatPeriodStr == "1/4")
542 detailStatWritePeriod = dsPeriod_1_4;
545 else if (detailStatPeriodStr == "1/6")
547 detailStatWritePeriod = dsPeriod_1_6;
553 //-----------------------------------------------------------------------------