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 "settings_impl.h"
36 #include "stg_logger.h"
37 #include "dotconfpp.h"
41 //-----------------------------------------------------------------------------
42 SETTINGS_IMPL::SETTINGS_IMPL()
44 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 storeModuleSettings(),
67 logger(GetStgLogger())
70 //-----------------------------------------------------------------------------
71 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
73 modulesPath("/usr/lib/stg"),
78 logFile("/var/log/stargazer.log"),
79 pidFile("/var/run/stargazer.pid"),
80 monitorDir("/var/stargazer/monitoring"),
82 detailStatWritePeriod(dsPeriod_1_6),
90 freeMbAllowInet(false),
91 dayFeeIsLastDay(false),
92 writeFreeMbTraffCost(false),
96 storeModuleSettings(),
97 logger(GetStgLogger())
100 //-----------------------------------------------------------------------------
101 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
103 modulesPath(rval.modulesPath),
104 dirName(rval.dirName),
105 confDir(rval.confDir),
106 scriptsDir(rval.scriptsDir),
108 logFile(rval.logFile),
109 pidFile(rval.pidFile),
110 monitorDir(rval.monitorDir),
111 monitoring(rval.monitoring),
112 detailStatWritePeriod(rval.detailStatWritePeriod),
113 statWritePeriod(rval.statWritePeriod),
114 stgExecMsgKey(rval.stgExecMsgKey),
115 executersNum(rval.executersNum),
116 fullFee(rval.fullFee),
118 dayResetTraff(rval.dayResetTraff),
119 spreadFee(rval.spreadFee),
120 freeMbAllowInet(rval.freeMbAllowInet),
121 dayFeeIsLastDay(rval.dayFeeIsLastDay),
122 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
123 showFeeInCash(rval.showFeeInCash),
124 messageTimeout(rval.messageTimeout),
125 modulesSettings(rval.modulesSettings),
126 storeModuleSettings(rval.storeModuleSettings),
127 logger(GetStgLogger())
130 //-----------------------------------------------------------------------------
131 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
133 if (0 == strcasecmp(value.c_str(), "yes"))
138 if (0 == strcasecmp(value.c_str(), "no"))
144 strError = "Incorrect value \'" + value + "\'.";
147 //-----------------------------------------------------------------------------
148 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
150 if (str2x<int>(value, *val))
152 strError = "Cannot convert \'" + value + "\' to integer.";
157 //-----------------------------------------------------------------------------
158 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
160 if (str2x<unsigned>(value, *val))
162 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
167 //-----------------------------------------------------------------------------
168 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
170 if (ParseInt(value, val) != 0)
173 if (*val < min || *val > max)
175 strError = "Value \'" + value + "\' out of range.";
181 //-----------------------------------------------------------------------------
182 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
184 if (ParseUnsigned(value, val) != 0)
187 if (*val < min || *val > max)
189 strError = "Value \'" + value + "\' out of range.";
195 //-----------------------------------------------------------------------------
196 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
198 const DOTCONFDocumentNode * childNode;
202 pv.param = node->getName();
204 if (node->getValue(1))
206 strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
210 value = node->getValue(0);
214 strError = "Module name expected.";
218 childNode = node->getChildNode();
221 pv.param = childNode->getName();
223 while ((value = childNode->getValue(i++)) != NULL)
225 pv.value.push_back(value);
227 params->push_back(pv);
229 childNode = childNode->getNextNode();
234 //-----------------------------------------------------------------------------
235 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
237 printfd(__FILE__, buf);
238 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
239 settings->logger(buf);
241 //-----------------------------------------------------------------------------
242 int SETTINGS_IMPL::ReadSettings()
244 const char * requiredOptions[] = {
250 "DetailStatWritePeriod",
256 "WriteFreeMbTraffCost",
259 int storeModulesCount = 0;
260 modulesSettings.clear();
262 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
263 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
264 conf.setRequiredOptionNames(requiredOptions);
265 string confFile = confDir + "/stargazer.conf";
267 if(conf.setContent(confFile.c_str()) != 0)
269 strError = "Cannot read file " + confFile;
273 const DOTCONFDocumentNode * node = conf.getFirstNode();
277 if (strcasecmp(node->getName(), "ScriptDir") == 0)
279 scriptsDir = node->getValue(0);
282 if (strcasecmp(node->getName(), "LogFile") == 0)
284 logFile = node->getValue(0);
287 if (strcasecmp(node->getName(), "PIDFile") == 0)
289 pidFile = node->getValue(0);
292 if (strcasecmp(node->getName(), "ModulesPath") == 0)
294 modulesPath = node->getValue(0);
297 if (strcasecmp(node->getName(), "Rules") == 0)
299 rules = node->getValue(0);
302 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
304 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
306 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
311 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
313 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
315 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
320 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
322 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
324 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
329 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
331 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
333 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
338 if (strcasecmp(node->getName(), "DayFee") == 0)
340 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
342 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
347 if (strcasecmp(node->getName(), "FullFee") == 0)
349 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
351 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
356 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
358 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
360 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
365 if (strcasecmp(node->getName(), "SpreadFee") == 0)
367 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
369 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
374 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
376 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
378 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
383 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
385 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
387 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
392 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
394 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
396 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
401 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
403 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
405 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
410 if (strcasecmp(node->getName(), "MonitorDir") == 0)
412 monitorDir = node->getValue(0);
416 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
422 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
424 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
426 strError = "Incorrect MessageTimeout value: \'" + string(node->getValue(0)) + "\'";
431 if (strcasecmp(node->getName(), "DirNames") == 0)
433 const DOTCONFDocumentNode * child = node->getChildNode();
436 const DOTCONFDocumentNode * dirNameNode;
437 dirName.reserve(DIR_NUM);
438 for (int i = 0; i < DIR_NUM; i++)
441 sprintf(strDirName, "DirName%d", i);
442 dirNameNode = conf.findNode(strDirName, node);
443 if (dirNameNode && dirNameNode->getValue(0))
445 dirName[i] = dirNameNode->getValue(0);
451 if (strcasecmp(node->getName(), "StoreModule") == 0)
453 if (node->getValue(1))
455 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
459 if (storeModulesCount)
461 strError = "Should be only one StoreModule.";
466 storeModuleSettings.moduleName = node->getValue(0);
467 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
470 if (strcasecmp(node->getName(), "Modules") == 0)
472 if (node->getValue(0))
474 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
477 const DOTCONFDocumentNode * child = node->getChildNode();
480 if (strcasecmp(child->getName(), "Module") != 0)
482 child = child->getNextNode();
485 MODULE_SETTINGS modSettings;
486 modSettings.moduleParams.clear();
487 modSettings.moduleName = child->getValue();
489 ParseModuleSettings(child, &modSettings.moduleParams);
491 modulesSettings.push_back(modSettings);
493 child = child->getNextNode();
497 node = node->getNextNode();
502 //-----------------------------------------------------------------------------
503 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
505 if (detailStatPeriodStr == "1")
507 detailStatWritePeriod = dsPeriod_1;
510 else if (detailStatPeriodStr == "1/2")
512 detailStatWritePeriod = dsPeriod_1_2;
515 else if (detailStatPeriodStr == "1/4")
517 detailStatWritePeriod = dsPeriod_1_4;
520 else if (detailStatPeriodStr == "1/6")
522 detailStatWritePeriod = dsPeriod_1_6;
528 //-----------------------------------------------------------------------------