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 $
36 #include "stg_logger.h"
37 #include "dotconfpp.h"
41 //-----------------------------------------------------------------------------
43 : confDir("/etc/stargazer"),
44 scriptsDir("/etc/stargazer"),
45 pidFile("/var/run/stargazer.pid"),
47 detailStatWritePeriod(dsPeriod_1_6),
55 freeMbAllowInet(false),
56 dayFeeIsLastDay(false),
57 writeFreeMbTraffCost(false),
60 logger(GetStgLogger())
63 //-----------------------------------------------------------------------------
64 SETTINGS::SETTINGS(const std::string & cd)
68 detailStatWritePeriod(dsPeriod_1_6),
76 freeMbAllowInet(false),
77 dayFeeIsLastDay(false),
78 writeFreeMbTraffCost(false),
81 logger(GetStgLogger())
84 //-----------------------------------------------------------------------------
85 SETTINGS::SETTINGS(const SETTINGS & rval)
86 : confDir(rval.confDir),
87 scriptsDir(rval.scriptsDir),
88 pidFile(rval.pidFile),
89 monitoring(rval.monitoring),
90 detailStatWritePeriod(dsPeriod_1_6),
92 stgExecMsgKey(rval.stgExecMsgKey),
93 executersNum(rval.executersNum),
94 fullFee(rval.fullFee),
97 spreadFee(rval.spreadFee),
98 freeMbAllowInet(false),
99 dayFeeIsLastDay(false),
100 writeFreeMbTraffCost(false),
101 showFeeInCash(rval.showFeeInCash),
102 messageTimeout(rval.messageTimeout),
103 logger(GetStgLogger())
106 //-----------------------------------------------------------------------------
107 SETTINGS::~SETTINGS()
110 //-----------------------------------------------------------------------------
111 int SETTINGS::ParseYesNo(const string & value, bool * val)
113 if (0 == strcasecmp(value.c_str(), "yes"))
118 if (0 == strcasecmp(value.c_str(), "no"))
124 strError = "Incorrect value \'" + value + "\'.";
127 //-----------------------------------------------------------------------------
128 int SETTINGS::ParseInt(const string & value, int * val)
130 if (str2x<int>(value, *val))
132 strError = "Cannot convert \'" + value + "\' to integer.";
137 //-----------------------------------------------------------------------------
138 int SETTINGS::ParseUnsigned(const string & value, unsigned * val)
140 if (str2x<unsigned>(value, *val))
142 strError = "Cannot convert \'" + value + "\' to unsigned integer.";
147 //-----------------------------------------------------------------------------
148 int SETTINGS::ParseIntInRange(const string & value, int min, int max, int * val)
150 if (ParseInt(value, val) != 0)
153 if (*val < min || *val > max)
155 strError = "Value \'" + value + "\' out of range.";
161 //-----------------------------------------------------------------------------
162 int SETTINGS::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
164 if (ParseUnsigned(value, val) != 0)
167 if (*val < min || *val > max)
169 strError = "Value \'" + value + "\' out of range.";
175 //-----------------------------------------------------------------------------
176 int SETTINGS::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
178 const DOTCONFDocumentNode * childNode;
182 pv.param = node->getName();
184 if (node->getValue(1))
186 strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
190 value = node->getValue(0);
194 strError = "Module name expected.";
198 childNode = node->getChildNode();
201 pv.param = childNode->getName();
203 while ((value = childNode->getValue(i++)) != NULL)
205 pv.value.push_back(value);
207 params->push_back(pv);
209 childNode = childNode->getNextNode();
214 //-----------------------------------------------------------------------------
215 void SETTINGS::ErrorCallback(void * data, const char * buf)
217 printfd(__FILE__, buf);
218 SETTINGS * settings = static_cast<SETTINGS *>(data);
219 settings->logger(buf);
221 //-----------------------------------------------------------------------------
222 int SETTINGS::ReadSettings()
224 const char * requiredOptions[] = {
230 "DetailStatWritePeriod",
236 "WriteFreeMbTraffCost",
239 int storeModulesCount = 0;
240 modulesSettings.clear();
242 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
243 conf.setErrorCallback(SETTINGS::ErrorCallback, this);
244 conf.setRequiredOptionNames(requiredOptions);
245 string confFile = confDir + "/stargazer.conf";
247 if(conf.setContent(confFile.c_str()) != 0)
249 strError = "Cannot read file " + confFile;
253 const DOTCONFDocumentNode * node = conf.getFirstNode();
257 if (strcasecmp(node->getName(), "ScriptDir") == 0)
259 scriptsDir = node->getValue(0);
262 if (strcasecmp(node->getName(), "LogFile") == 0)
264 logFile = node->getValue(0);
267 if (strcasecmp(node->getName(), "PIDFile") == 0)
269 pidFile = node->getValue(0);
272 if (strcasecmp(node->getName(), "ModulesPath") == 0)
274 modulesPath = node->getValue(0);
277 if (strcasecmp(node->getName(), "Rules") == 0)
279 rules = node->getValue(0);
282 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
284 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
286 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
291 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
293 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
295 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
300 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
302 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
304 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
309 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
311 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
313 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
318 if (strcasecmp(node->getName(), "DayFee") == 0)
320 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
322 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
327 if (strcasecmp(node->getName(), "FullFee") == 0)
329 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
331 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
336 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
338 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
340 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
345 if (strcasecmp(node->getName(), "SpreadFee") == 0)
347 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
349 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
354 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
356 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
358 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
363 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
365 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
367 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
372 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
374 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
376 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
381 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
383 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
385 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
390 if (strcasecmp(node->getName(), "MonitorDir") == 0)
392 monitorDir = node->getValue(0);
396 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
402 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
404 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
406 strError = "Incorrect MessageTimeout value: \'" + string(node->getValue(0)) + "\'";
411 if (strcasecmp(node->getName(), "DirNames") == 0)
413 const DOTCONFDocumentNode * child = node->getChildNode();
416 const DOTCONFDocumentNode * dirNameNode;
417 for (int i = 0; i < DIR_NUM; i++)
420 sprintf(strDirName, "DirName%d", i);
421 dirNameNode = conf.findNode(strDirName, node);
422 if (dirNameNode && dirNameNode->getValue(0))
424 dirName[i] = dirNameNode->getValue(0);
430 if (strcasecmp(node->getName(), "StoreModule") == 0)
432 if (node->getValue(1))
434 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
438 if (storeModulesCount)
440 strError = "Should be only one StoreModule.";
445 storeModuleSettings.moduleName = node->getValue(0);
446 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
449 if (strcasecmp(node->getName(), "Modules") == 0)
451 if (node->getValue(0))
453 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
456 const DOTCONFDocumentNode * child = node->getChildNode();
459 if (strcasecmp(child->getName(), "Module") != 0)
461 child = child->getNextNode();
464 MODULE_SETTINGS modSettings;
465 modSettings.moduleParams.clear();
466 modSettings.moduleName = child->getValue();
468 ParseModuleSettings(child, &modSettings.moduleParams);
470 modulesSettings.push_back(modSettings);
472 child = child->getNextNode();
476 node = node->getNextNode();
481 //-----------------------------------------------------------------------------
482 int SETTINGS::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
484 if (detailStatPeriodStr == "1")
486 detailStatWritePeriod = dsPeriod_1;
489 else if (detailStatPeriodStr == "1/2")
491 detailStatWritePeriod = dsPeriod_1_2;
494 else if (detailStatPeriodStr == "1/4")
496 detailStatWritePeriod = dsPeriod_1_4;
499 else if (detailStatPeriodStr == "1/6")
501 detailStatWritePeriod = dsPeriod_1_6;
507 //-----------------------------------------------------------------------------