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 scriptDir("/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 scriptDir(rval.scriptDir),
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 //printfd(__FILE__, "Conffile: %s\n", confFile.c_str());
249 if(conf.setContent(confFile.c_str()) != 0)
251 strError = "Cannot read file " + confFile;
255 const DOTCONFDocumentNode * node = conf.getFirstNode();
259 if (strcasecmp(node->getName(), "ScriptDir") == 0)
261 scriptDir = node->getValue(0);
262 //printfd(__FILE__, "LogFile: %s\n", logFile.c_str());
265 if (strcasecmp(node->getName(), "LogFile") == 0)
267 logFile = node->getValue(0);
268 //printfd(__FILE__, "LogFile: %s\n", logFile.c_str());
271 if (strcasecmp(node->getName(), "PIDFile") == 0)
273 pidFile = node->getValue(0);
274 //printfd(__FILE__, "PIDFile: %s\n", pidFile.c_str());
277 if (strcasecmp(node->getName(), "ModulesPath") == 0)
279 modulesPath = node->getValue(0);
280 //printfd(__FILE__, "ModulesPath: %s\n", logFile.c_str());
283 if (strcasecmp(node->getName(), "Rules") == 0)
285 rules = node->getValue(0);
286 //printfd(__FILE__, "Rules: %s\n", rules.c_str());
289 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
291 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
293 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
296 //printfd(__FILE__, "DetailStatWritePeriod: %d\n", detailStatWritePeriod);
299 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
301 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
303 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
306 //printfd(__FILE__, "StatWritePeriod: %d\n", statWritePeriod);
309 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
311 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
313 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
318 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
320 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, static_cast<unsigned *>(&executersNum)) != 0)
322 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
325 //printfd(__FILE__, "DayResetTraff: %d\n", dayResetTraff);
328 if (strcasecmp(node->getName(), "DayFee") == 0)
330 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
332 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
335 //printfd(__FILE__, "DayFee: %d\n", dayFee);
338 if (strcasecmp(node->getName(), "FullFee") == 0)
340 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
342 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
345 //printfd(__FILE__, "DayFee: %d\n", dayFee);
348 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
350 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
352 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
355 //printfd(__FILE__, "DayResetTraff: %d\n", dayResetTraff);
358 if (strcasecmp(node->getName(), "SpreadFee") == 0)
360 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
362 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
365 //printfd(__FILE__, "SpreadFee: %d\n", spreadFee);
368 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
370 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
372 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
375 //printfd(__FILE__, "FreeMbAllowInet: %d\n", freeMbAllowInet);
378 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
380 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
382 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
385 //printfd(__FILE__, "DayFeeIsLastDay: %d\n", dayFeeIsLastDay);
388 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
390 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
392 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
395 //printfd(__FILE__, "WriteFreeMbTraffCost: %d\n", writeFreeMbTraffCost);
398 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
400 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
402 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
405 //printfd(__FILE__, "ShowFeeInCash: %d\n", showFeeInCash);
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)) + "\'";
427 //printfd(__FILE__, "MessageTimeout: %d\n", messageTimeout);
430 if (strcasecmp(node->getName(), "DirNames") == 0)
432 // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ DirNames
433 const DOTCONFDocumentNode * child = node->getChildNode();
436 const DOTCONFDocumentNode * dirNameNode;
437 for (int i = 0; i < DIR_NUM; i++)
440 sprintf(strDirName, "DirName%d", i);
441 dirNameNode = conf.findNode(strDirName, node);
442 if (dirNameNode && dirNameNode->getValue(0))
444 dirName[i] = dirNameNode->getValue(0);
450 if (strcasecmp(node->getName(), "StoreModule") == 0)
452 // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ StoreModule
453 //printfd(__FILE__, "StoreModule\n");
455 if (node->getValue(1))
457 // StoreModule ÄÏÌÖÅÎ ÉÍÅÔØ 1 ÁÔÒÉÂÕÔ
458 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
462 if (storeModulesCount)
464 // äÏÌÖÅÎ ÂÙÔØ ÔÏÌØËÏ ÏÄÉÎ ÍÏÄÕÌØ StoreModule!
465 strError = "Should be only one StoreModule.";
470 storeModuleSettings.moduleName = node->getValue(0);
471 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
474 // þÉÔÁÅÍ ÎÁÓÔÒÏÊËÉ ×ÓÅÈ ÏÓÔÁ×ÛÉÈÓÑ ÍÏÄÕÌÅÊ.
475 if (strcasecmp(node->getName(), "Modules") == 0)
477 // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ Modules
478 if (node->getValue(0))
480 // Modules ÎÅ ÄÏÌÖÅÎ ÉÍÅÔØ ÁÔÒÉÂÕÏ×
481 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
484 const DOTCONFDocumentNode * child = node->getChildNode();
488 if (strcasecmp(child->getName(), "Module") != 0)
490 child = child->getNextNode();
493 MODULE_SETTINGS modSettings;
494 modSettings.moduleParams.clear();
495 modSettings.moduleName = child->getValue();
497 ParseModuleSettings(child, &modSettings.moduleParams);
499 modulesSettings.push_back(modSettings);
501 child = child->getNextNode();
505 node = node->getNextNode();
510 //-----------------------------------------------------------------------------
511 int SETTINGS::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
513 if (detailStatPeriodStr == "1")
515 detailStatWritePeriod = dsPeriod_1;
518 else if (detailStatPeriodStr == "1/2")
520 detailStatWritePeriod = dsPeriod_1_2;
523 else if (detailStatPeriodStr == "1/4")
525 detailStatWritePeriod = dsPeriod_1_4;
528 else if (detailStatPeriodStr == "1/6")
530 detailStatWritePeriod = dsPeriod_1_6;
536 //-----------------------------------------------------------------------------