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 storeModuleSettings(),
68 logger(GetStgLogger())
71 //-----------------------------------------------------------------------------
72 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
74 modulesPath("/usr/lib/stg"),
79 logFile("/var/log/stargazer.log"),
80 pidFile("/var/run/stargazer.pid"),
81 monitorDir("/var/stargazer/monitoring"),
83 detailStatWritePeriod(dsPeriod_1_6),
91 freeMbAllowInet(false),
92 dayFeeIsLastDay(false),
93 writeFreeMbTraffCost(false),
97 storeModuleSettings(),
98 logger(GetStgLogger())
101 //-----------------------------------------------------------------------------
102 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
104 modulesPath(rval.modulesPath),
105 dirName(rval.dirName),
106 confDir(rval.confDir),
107 scriptsDir(rval.scriptsDir),
109 logFile(rval.logFile),
110 pidFile(rval.pidFile),
111 monitorDir(rval.monitorDir),
112 monitoring(rval.monitoring),
113 detailStatWritePeriod(rval.detailStatWritePeriod),
114 statWritePeriod(rval.statWritePeriod),
115 stgExecMsgKey(rval.stgExecMsgKey),
116 executersNum(rval.executersNum),
117 fullFee(rval.fullFee),
119 dayResetTraff(rval.dayResetTraff),
120 spreadFee(rval.spreadFee),
121 freeMbAllowInet(rval.freeMbAllowInet),
122 dayFeeIsLastDay(rval.dayFeeIsLastDay),
123 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
124 showFeeInCash(rval.showFeeInCash),
125 messageTimeout(rval.messageTimeout),
126 modulesSettings(rval.modulesSettings),
127 storeModuleSettings(rval.storeModuleSettings),
128 logger(GetStgLogger())
131 //-----------------------------------------------------------------------------
132 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
134 if (0 == strcasecmp(value.c_str(), "yes"))
139 if (0 == strcasecmp(value.c_str(), "no"))
145 strError = "Incorrect value \'" + value + "\'.";
148 //-----------------------------------------------------------------------------
149 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
151 if (str2x<int>(value, *val))
153 strError = "Cannot convert \'" + value + "\' to integer.";
158 //-----------------------------------------------------------------------------
159 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
161 if (str2x<unsigned>(value, *val))
163 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
168 //-----------------------------------------------------------------------------
169 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
171 if (ParseInt(value, val) != 0)
174 if (*val < min || *val > max)
176 strError = "Value \'" + value + "\' out of range.";
182 //-----------------------------------------------------------------------------
183 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
185 if (ParseUnsigned(value, val) != 0)
188 if (*val < min || *val > max)
190 strError = "Value \'" + value + "\' out of range.";
196 //-----------------------------------------------------------------------------
197 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
199 const DOTCONFDocumentNode * childNode;
203 pv.param = node->getName();
205 if (node->getValue(1))
207 strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
211 value = node->getValue(0);
215 strError = "Module name expected.";
219 childNode = node->getChildNode();
222 pv.param = childNode->getName();
224 while ((value = childNode->getValue(i++)) != NULL)
226 pv.value.push_back(value);
228 params->push_back(pv);
230 childNode = childNode->getNextNode();
235 //-----------------------------------------------------------------------------
236 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
238 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
239 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
240 settings->logger("%s", buf);
242 //-----------------------------------------------------------------------------
243 int SETTINGS_IMPL::ReadSettings()
245 const char * requiredOptions[] = {
251 "DetailStatWritePeriod",
257 "WriteFreeMbTraffCost",
260 int storeModulesCount = 0;
261 modulesSettings.clear();
263 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
264 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
265 conf.setRequiredOptionNames(requiredOptions);
266 string confFile = confDir + "/stargazer.conf";
268 if(conf.setContent(confFile.c_str()) != 0)
270 strError = "Cannot read file " + confFile;
274 const DOTCONFDocumentNode * node = conf.getFirstNode();
278 if (strcasecmp(node->getName(), "ScriptDir") == 0)
280 scriptsDir = node->getValue(0);
283 if (strcasecmp(node->getName(), "LogFile") == 0)
285 logFile = node->getValue(0);
288 if (strcasecmp(node->getName(), "PIDFile") == 0)
290 pidFile = node->getValue(0);
293 if (strcasecmp(node->getName(), "ModulesPath") == 0)
295 modulesPath = node->getValue(0);
298 if (strcasecmp(node->getName(), "Rules") == 0)
300 rules = node->getValue(0);
303 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
305 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
307 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
312 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
314 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
316 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
321 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
323 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
325 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
330 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
332 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
334 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
339 if (strcasecmp(node->getName(), "DayFee") == 0)
341 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
343 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
348 if (strcasecmp(node->getName(), "FullFee") == 0)
350 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
352 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
357 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
359 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
361 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
366 if (strcasecmp(node->getName(), "SpreadFee") == 0)
368 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
370 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
375 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
377 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
379 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
384 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
386 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
388 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
393 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
395 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
397 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
402 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
404 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
406 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
411 if (strcasecmp(node->getName(), "MonitorDir") == 0)
413 monitorDir = node->getValue(0);
417 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
423 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
425 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
427 strError = "Incorrect MessageTimeout value: \'" + string(node->getValue(0)) + "\'";
432 if (strcasecmp(node->getName(), "DirNames") == 0)
434 const DOTCONFDocumentNode * child = node->getChildNode();
437 const DOTCONFDocumentNode * dirNameNode;
438 dirName.reserve(DIR_NUM);
439 for (int i = 0; i < DIR_NUM; i++)
442 sprintf(strDirName, "DirName%d", i);
443 dirNameNode = conf.findNode(strDirName, node);
444 if (dirNameNode && dirNameNode->getValue(0))
446 dirName[i] = dirNameNode->getValue(0);
452 if (strcasecmp(node->getName(), "StoreModule") == 0)
454 if (node->getValue(1))
456 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
460 if (storeModulesCount)
462 strError = "Should be only one StoreModule.";
467 storeModuleSettings.moduleName = node->getValue(0);
468 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
471 if (strcasecmp(node->getName(), "Modules") == 0)
473 if (node->getValue(0))
475 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
478 const DOTCONFDocumentNode * child = node->getChildNode();
481 if (strcasecmp(child->getName(), "Module") != 0)
483 child = child->getNextNode();
486 MODULE_SETTINGS modSettings;
487 modSettings.moduleParams.clear();
488 modSettings.moduleName = child->getValue();
490 ParseModuleSettings(child, &modSettings.moduleParams);
492 modulesSettings.push_back(modSettings);
494 child = child->getNextNode();
498 node = node->getNextNode();
503 //-----------------------------------------------------------------------------
504 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
506 if (detailStatPeriodStr == "1")
508 detailStatWritePeriod = dsPeriod_1;
511 else if (detailStatPeriodStr == "1/2")
513 detailStatWritePeriod = dsPeriod_1_2;
516 else if (detailStatPeriodStr == "1/4")
518 detailStatWritePeriod = dsPeriod_1_4;
521 else if (detailStatPeriodStr == "1/6")
523 detailStatWritePeriod = dsPeriod_1_6;
529 //-----------------------------------------------------------------------------