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()
45 modulesPath("/usr/lib/stg"),
47 confDir("/etc/stargazer"),
48 scriptsDir("/etc/stargazer"),
49 rules("/etc/stargazer/rules"),
50 logFile("/var/log/stargazer.log"),
51 pidFile("/var/run/stargazer.pid"),
52 monitorDir("/var/stargazer/monitoring"),
54 detailStatWritePeriod(dsPeriod_1_6),
62 freeMbAllowInet(false),
63 dayFeeIsLastDay(false),
64 writeFreeMbTraffCost(false),
68 reconnectOnTariffChange(false),
70 storeModuleSettings(),
71 logger(GetStgLogger())
74 //-----------------------------------------------------------------------------
75 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
78 modulesPath("/usr/lib/stg"),
83 logFile("/var/log/stargazer.log"),
84 pidFile("/var/run/stargazer.pid"),
85 monitorDir("/var/stargazer/monitoring"),
87 detailStatWritePeriod(dsPeriod_1_6),
95 freeMbAllowInet(false),
96 dayFeeIsLastDay(false),
97 writeFreeMbTraffCost(false),
101 reconnectOnTariffChange(false),
103 storeModuleSettings(),
104 logger(GetStgLogger())
107 //-----------------------------------------------------------------------------
108 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
111 modulesPath(rval.modulesPath),
112 dirName(rval.dirName),
113 confDir(rval.confDir),
114 scriptsDir(rval.scriptsDir),
116 logFile(rval.logFile),
117 pidFile(rval.pidFile),
118 monitorDir(rval.monitorDir),
119 monitoring(rval.monitoring),
120 detailStatWritePeriod(rval.detailStatWritePeriod),
121 statWritePeriod(rval.statWritePeriod),
122 stgExecMsgKey(rval.stgExecMsgKey),
123 executersNum(rval.executersNum),
124 fullFee(rval.fullFee),
126 dayResetTraff(rval.dayResetTraff),
127 spreadFee(rval.spreadFee),
128 freeMbAllowInet(rval.freeMbAllowInet),
129 dayFeeIsLastDay(rval.dayFeeIsLastDay),
130 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
131 showFeeInCash(rval.showFeeInCash),
132 messageTimeout(rval.messageTimeout),
133 feeChargeType(rval.feeChargeType),
134 reconnectOnTariffChange(rval.reconnectOnTariffChange),
135 modulesSettings(rval.modulesSettings),
136 storeModuleSettings(rval.storeModuleSettings),
137 logger(GetStgLogger())
140 //-----------------------------------------------------------------------------
141 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
143 if (0 == strcasecmp(value.c_str(), "yes"))
148 if (0 == strcasecmp(value.c_str(), "no"))
154 strError = "Incorrect value \'" + value + "\'.";
157 //-----------------------------------------------------------------------------
158 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
160 if (str2x<int>(value, *val))
162 strError = "Cannot convert \'" + value + "\' to integer.";
167 //-----------------------------------------------------------------------------
168 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
170 if (str2x<unsigned>(value, *val))
172 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
177 //-----------------------------------------------------------------------------
178 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
180 if (ParseInt(value, val) != 0)
183 if (*val < min || *val > max)
185 strError = "Value \'" + value + "\' out of range.";
191 //-----------------------------------------------------------------------------
192 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
194 if (ParseUnsigned(value, val) != 0)
197 if (*val < min || *val > max)
199 strError = "Value \'" + value + "\' out of range.";
205 //-----------------------------------------------------------------------------
206 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
208 const DOTCONFDocumentNode * childNode;
212 pv.param = node->getName();
214 if (node->getValue(1))
216 strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
220 value = node->getValue(0);
224 strError = "Module name expected.";
228 childNode = node->getChildNode();
231 pv.param = childNode->getName();
233 while ((value = childNode->getValue(i++)) != NULL)
235 pv.value.push_back(value);
237 params->push_back(pv);
239 childNode = childNode->getNextNode();
244 //-----------------------------------------------------------------------------
245 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
247 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
248 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
249 settings->logger("%s", buf);
251 //-----------------------------------------------------------------------------
252 int SETTINGS_IMPL::ReadSettings()
254 const char * requiredOptions[] = {
260 "DetailStatWritePeriod",
266 "WriteFreeMbTraffCost",
269 int storeModulesCount = 0;
270 modulesSettings.clear();
272 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
273 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
274 conf.setRequiredOptionNames(requiredOptions);
275 string confFile = confDir + "/stargazer.conf";
277 if(conf.setContent(confFile.c_str()) != 0)
279 strError = "Cannot read file " + confFile;
283 const DOTCONFDocumentNode * node = conf.getFirstNode();
287 if (strcasecmp(node->getName(), "ScriptDir") == 0)
289 scriptsDir = node->getValue(0);
292 if (strcasecmp(node->getName(), "LogFile") == 0)
294 logFile = node->getValue(0);
297 if (strcasecmp(node->getName(), "PIDFile") == 0)
299 pidFile = node->getValue(0);
302 if (strcasecmp(node->getName(), "ModulesPath") == 0)
304 modulesPath = node->getValue(0);
307 if (strcasecmp(node->getName(), "Rules") == 0)
309 rules = node->getValue(0);
312 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
314 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
316 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
321 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
323 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
325 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
330 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
332 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
334 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
339 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
341 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
343 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
348 if (strcasecmp(node->getName(), "DayFee") == 0)
350 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
352 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
357 if (strcasecmp(node->getName(), "FullFee") == 0)
359 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
361 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
366 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
368 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
370 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
375 if (strcasecmp(node->getName(), "SpreadFee") == 0)
377 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
379 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
384 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
386 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
388 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
393 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
395 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
397 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
402 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
404 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
406 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
411 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
413 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
415 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
420 if (strcasecmp(node->getName(), "MonitorDir") == 0)
422 monitorDir = node->getValue(0);
426 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
432 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
434 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
436 strError = "Incorrect MessageTimeout value: \'" + string(node->getValue(0)) + "\'";
441 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
443 if (ParseUnsignedInRange(node->getValue(0), 0, 2, &feeChargeType) != 0)
445 strError = "Incorrect FeeChargeType value: \'" + string(node->getValue(0)) + "\'";
450 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
452 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
454 strError = "Incorrect ReconnectOnTariffChange value: \'" + string(node->getValue(0)) + "\'";
459 if (strcasecmp(node->getName(), "DirNames") == 0)
461 const DOTCONFDocumentNode * child = node->getChildNode();
464 const DOTCONFDocumentNode * dirNameNode;
465 dirName.reserve(DIR_NUM);
466 for (int i = 0; i < DIR_NUM; i++)
469 sprintf(strDirName, "DirName%d", i);
470 dirNameNode = conf.findNode(strDirName, node);
471 if (dirNameNode && dirNameNode->getValue(0))
473 dirName[i] = dirNameNode->getValue(0);
479 if (strcasecmp(node->getName(), "StoreModule") == 0)
481 if (node->getValue(1))
483 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
487 if (storeModulesCount)
489 strError = "Should be only one StoreModule.";
494 storeModuleSettings.moduleName = node->getValue(0);
495 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
498 if (strcasecmp(node->getName(), "Modules") == 0)
500 if (node->getValue(0))
502 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
505 const DOTCONFDocumentNode * child = node->getChildNode();
508 if (strcasecmp(child->getName(), "Module") != 0)
510 child = child->getNextNode();
513 MODULE_SETTINGS modSettings;
514 modSettings.moduleParams.clear();
515 modSettings.moduleName = child->getValue();
517 ParseModuleSettings(child, &modSettings.moduleParams);
519 modulesSettings.push_back(modSettings);
521 child = child->getNextNode();
525 node = node->getNextNode();
530 //-----------------------------------------------------------------------------
531 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
533 if (detailStatPeriodStr == "1")
535 detailStatWritePeriod = dsPeriod_1;
538 else if (detailStatPeriodStr == "1/2")
540 detailStatWritePeriod = dsPeriod_1_2;
543 else if (detailStatPeriodStr == "1/4")
545 detailStatWritePeriod = dsPeriod_1_4;
548 else if (detailStatPeriodStr == "1/6")
550 detailStatWritePeriod = dsPeriod_1_6;
556 //-----------------------------------------------------------------------------