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"),
77 logFile("/var/log/stargazer.log"),
78 pidFile("/var/run/stargazer.pid"),
79 monitorDir("/var/stargazer/monitoring"),
81 detailStatWritePeriod(dsPeriod_1_6),
89 freeMbAllowInet(false),
90 dayFeeIsLastDay(false),
91 writeFreeMbTraffCost(false),
95 storeModuleSettings(),
96 logger(GetStgLogger())
99 //-----------------------------------------------------------------------------
100 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
102 modulesPath(rval.modulesPath),
103 confDir(rval.confDir),
104 scriptsDir(rval.scriptsDir),
106 logFile(rval.logFile),
107 pidFile(rval.pidFile),
108 monitorDir(rval.monitorDir),
109 monitoring(rval.monitoring),
110 detailStatWritePeriod(rval.detailStatWritePeriod),
111 statWritePeriod(rval.statWritePeriod),
112 stgExecMsgKey(rval.stgExecMsgKey),
113 executersNum(rval.executersNum),
114 fullFee(rval.fullFee),
116 dayResetTraff(rval.dayResetTraff),
117 spreadFee(rval.spreadFee),
118 freeMbAllowInet(rval.freeMbAllowInet),
119 dayFeeIsLastDay(rval.dayFeeIsLastDay),
120 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
121 showFeeInCash(rval.showFeeInCash),
122 messageTimeout(rval.messageTimeout),
123 modulesSettings(rval.modulesSettings),
124 storeModuleSettings(rval.storeModuleSettings),
125 logger(GetStgLogger())
128 //-----------------------------------------------------------------------------
129 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
131 if (0 == strcasecmp(value.c_str(), "yes"))
136 if (0 == strcasecmp(value.c_str(), "no"))
142 strError = "Incorrect value \'" + value + "\'.";
145 //-----------------------------------------------------------------------------
146 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
148 if (str2x<int>(value, *val))
150 strError = "Cannot convert \'" + value + "\' to integer.";
155 //-----------------------------------------------------------------------------
156 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
158 if (str2x<unsigned>(value, *val))
160 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
165 //-----------------------------------------------------------------------------
166 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
168 if (ParseInt(value, val) != 0)
171 if (*val < min || *val > max)
173 strError = "Value \'" + value + "\' out of range.";
179 //-----------------------------------------------------------------------------
180 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
182 if (ParseUnsigned(value, val) != 0)
185 if (*val < min || *val > max)
187 strError = "Value \'" + value + "\' out of range.";
193 //-----------------------------------------------------------------------------
194 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
196 const DOTCONFDocumentNode * childNode;
200 pv.param = node->getName();
202 if (node->getValue(1))
204 strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
208 value = node->getValue(0);
212 strError = "Module name expected.";
216 childNode = node->getChildNode();
219 pv.param = childNode->getName();
221 while ((value = childNode->getValue(i++)) != NULL)
223 pv.value.push_back(value);
225 params->push_back(pv);
227 childNode = childNode->getNextNode();
232 //-----------------------------------------------------------------------------
233 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
235 printfd(__FILE__, buf);
236 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
237 settings->logger(buf);
239 //-----------------------------------------------------------------------------
240 int SETTINGS_IMPL::ReadSettings()
242 const char * requiredOptions[] = {
248 "DetailStatWritePeriod",
254 "WriteFreeMbTraffCost",
257 int storeModulesCount = 0;
258 modulesSettings.clear();
260 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
261 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
262 conf.setRequiredOptionNames(requiredOptions);
263 string confFile = confDir + "/stargazer.conf";
265 if(conf.setContent(confFile.c_str()) != 0)
267 strError = "Cannot read file " + confFile;
271 const DOTCONFDocumentNode * node = conf.getFirstNode();
275 if (strcasecmp(node->getName(), "ScriptDir") == 0)
277 scriptsDir = node->getValue(0);
280 if (strcasecmp(node->getName(), "LogFile") == 0)
282 logFile = node->getValue(0);
285 if (strcasecmp(node->getName(), "PIDFile") == 0)
287 pidFile = node->getValue(0);
290 if (strcasecmp(node->getName(), "ModulesPath") == 0)
292 modulesPath = node->getValue(0);
295 if (strcasecmp(node->getName(), "Rules") == 0)
297 rules = node->getValue(0);
300 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
302 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
304 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
309 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
311 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
313 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
318 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
320 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
322 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
327 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
329 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
331 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
336 if (strcasecmp(node->getName(), "DayFee") == 0)
338 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
340 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
345 if (strcasecmp(node->getName(), "FullFee") == 0)
347 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
349 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
354 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
356 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
358 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
363 if (strcasecmp(node->getName(), "SpreadFee") == 0)
365 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
367 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
372 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
374 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
376 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
381 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
383 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
385 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
390 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
392 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
394 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
399 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
401 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
403 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
408 if (strcasecmp(node->getName(), "MonitorDir") == 0)
410 monitorDir = node->getValue(0);
414 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
420 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
422 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
424 strError = "Incorrect MessageTimeout value: \'" + string(node->getValue(0)) + "\'";
429 if (strcasecmp(node->getName(), "DirNames") == 0)
431 const DOTCONFDocumentNode * child = node->getChildNode();
434 const DOTCONFDocumentNode * dirNameNode;
435 for (int i = 0; i < DIR_NUM; i++)
438 sprintf(strDirName, "DirName%d", i);
439 dirNameNode = conf.findNode(strDirName, node);
440 if (dirNameNode && dirNameNode->getValue(0))
442 dirName[i] = dirNameNode->getValue(0);
448 if (strcasecmp(node->getName(), "StoreModule") == 0)
450 if (node->getValue(1))
452 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
456 if (storeModulesCount)
458 strError = "Should be only one StoreModule.";
463 storeModuleSettings.moduleName = node->getValue(0);
464 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
467 if (strcasecmp(node->getName(), "Modules") == 0)
469 if (node->getValue(0))
471 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
474 const DOTCONFDocumentNode * child = node->getChildNode();
477 if (strcasecmp(child->getName(), "Module") != 0)
479 child = child->getNextNode();
482 MODULE_SETTINGS modSettings;
483 modSettings.moduleParams.clear();
484 modSettings.moduleName = child->getValue();
486 ParseModuleSettings(child, &modSettings.moduleParams);
488 modulesSettings.push_back(modSettings);
490 child = child->getNextNode();
494 node = node->getNextNode();
499 //-----------------------------------------------------------------------------
500 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
502 if (detailStatPeriodStr == "1")
504 detailStatWritePeriod = dsPeriod_1;
507 else if (detailStatPeriodStr == "1/2")
509 detailStatWritePeriod = dsPeriod_1_2;
512 else if (detailStatPeriodStr == "1/4")
514 detailStatWritePeriod = dsPeriod_1_4;
517 else if (detailStatPeriodStr == "1/6")
519 detailStatWritePeriod = dsPeriod_1_6;
525 //-----------------------------------------------------------------------------