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 $
42 //-----------------------------------------------------------------------------
44 : confDir("/etc/stargazer"),
45 scriptDir("/etc/stargazer"),
46 pidFile("/var/run/stargazer.pid"),
48 detailStatWritePeriod(dsPeriod_1_6),
56 freeMbAllowInet(false),
57 dayFeeIsLastDay(false),
58 writeFreeMbTraffCost(false),
60 logger(GetStgLogger())
63 //-----------------------------------------------------------------------------
64 SETTINGS::SETTINGS(const std::string & cd)
69 detailStatWritePeriod(dsPeriod_1_6),
77 freeMbAllowInet(false),
78 dayFeeIsLastDay(false),
79 writeFreeMbTraffCost(false),
81 logger(GetStgLogger())
84 //-----------------------------------------------------------------------------
85 SETTINGS::SETTINGS(const SETTINGS & rval)
86 : confDir(rval.confDir),
87 scriptDir(rval.scriptDir),
88 detailStatWritePeriod(dsPeriod_1_6),
92 freeMbAllowInet(false),
93 dayFeeIsLastDay(false),
94 writeFreeMbTraffCost(false),
95 logger(GetStgLogger())
97 spreadFee = rval.spreadFee;
98 pidFile = rval.pidFile;
99 stgExecMsgKey = rval.stgExecMsgKey;
100 executersNum = rval.executersNum;
101 showFeeInCash = rval.showFeeInCash;
102 fullFee = rval.fullFee;
103 monitoring = rval.monitoring;
105 //-----------------------------------------------------------------------------
106 SETTINGS::~SETTINGS()
109 //-----------------------------------------------------------------------------
110 int SETTINGS::ParseYesNo(const string & value, bool * val)
112 if (0 == strcasecmp(value.c_str(), "yes"))
117 if (0 == strcasecmp(value.c_str(), "no"))
123 strError = "Incorrect value \'" + value + "\'.";
126 //-----------------------------------------------------------------------------
127 int SETTINGS::ParseInt(const string & value, int * val)
130 *val = strtol(value.c_str(), &res, 10);*/
131 if (str2x<int>(value, *val))
133 strError = "Cannot convert \'" + value + "\' to integer.";
138 //-----------------------------------------------------------------------------
139 int SETTINGS::ParseIntInRange(const string & value, int min, int max, int * val)
141 if (ParseInt(value, val) != 0)
144 if (*val < min || *val > max)
146 strError = "Value \'" + value + "\' out of range.";
152 //-----------------------------------------------------------------------------
153 int SETTINGS::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
157 const DOTCONFDocumentNode * childNode;
161 pv.param = node->getName();
163 if (node->getValue(1))
165 strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
169 value = node->getValue(0);
173 strError = "Module name expected.";
177 childNode = node->getChildNode();
180 pv.param = childNode->getName();
182 while ((value = childNode->getValue(i)) != NULL)
184 //printfd(__FILE__, "--> param=\'%s\' value=\'%s\'\n", childNode->getName(), value);
185 pv.value.push_back(value);
188 params->push_back(pv);
190 childNode = childNode->getNextNode();
193 /*for (unsigned i = 0; i < params->size(); i++)
195 printfd(__FILE__, "param \'%s\'\n", (*params)[i].param.c_str());
196 for (unsigned j = 0; j < (*params)[i].value.size(); j++)
198 printfd(__FILE__, "value \'%s\'\n", (*params)[i].value[j].c_str());
204 //-----------------------------------------------------------------------------
205 string SETTINGS::GetStrError() const
209 //-----------------------------------------------------------------------------
210 void SETTINGS::ErrorCallback(void * data, const char * buf)
212 printfd(__FILE__, buf);
213 SETTINGS * settings = static_cast<SETTINGS *>(data);
214 settings->logger(buf);
216 //-----------------------------------------------------------------------------
217 int SETTINGS::ReadSettings()
219 const char * requiredOptions[] = {
225 "DetailStatWritePeriod",
231 "WriteFreeMbTraffCost",
234 int storeModulesCount = 0;
235 modulesSettings.clear();
237 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
238 conf.setErrorCallback(SETTINGS::ErrorCallback, this);
239 conf.setRequiredOptionNames(requiredOptions);
240 string confFile = confDir + "/stargazer.conf";
242 //printfd(__FILE__, "Conffile: %s\n", confFile.c_str());
244 if(conf.setContent(confFile.c_str()) != 0)
246 strError = "Cannot read file " + confFile;
250 const DOTCONFDocumentNode * node = conf.getFirstNode();
254 if (strcasecmp(node->getName(), "ScriptDir") == 0)
256 scriptDir = node->getValue(0);
257 //printfd(__FILE__, "LogFile: %s\n", logFile.c_str());
260 if (strcasecmp(node->getName(), "LogFile") == 0)
262 logFile = node->getValue(0);
263 //printfd(__FILE__, "LogFile: %s\n", logFile.c_str());
266 if (strcasecmp(node->getName(), "PIDFile") == 0)
268 pidFile = node->getValue(0);
269 //printfd(__FILE__, "PIDFile: %s\n", pidFile.c_str());
272 if (strcasecmp(node->getName(), "ModulesPath") == 0)
274 modulesPath = node->getValue(0);
275 //printfd(__FILE__, "ModulesPath: %s\n", logFile.c_str());
278 if (strcasecmp(node->getName(), "Rules") == 0)
280 rules = node->getValue(0);
281 //printfd(__FILE__, "Rules: %s\n", rules.c_str());
284 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
286 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
288 strError = "Incorrect DetailStatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
291 //printfd(__FILE__, "DetailStatWritePeriod: %d\n", detailStatWritePeriod);
294 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
296 if (ParseIntInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
298 strError = "Incorrect StatWritePeriod value: \'" + string(node->getValue(0)) + "\'";
301 //printfd(__FILE__, "StatWritePeriod: %d\n", statWritePeriod);
304 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
306 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
308 strError = "Incorrect ExecMsgKey value: \'" + string(node->getValue(0)) + "\'";
313 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
315 if (ParseIntInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
317 strError = "Incorrect ExecutersNum value: \'" + string(node->getValue(0)) + "\'";
320 //printfd(__FILE__, "DayResetTraff: %d\n", dayResetTraff);
323 /*if (strcasecmp(node->getName(), "ExecutersWaitTimeout") == 0)
325 if (ParseIntInRange(node->getValue(0), 1, 600, &executersWaitTimeout) != 0)
327 strError = "Incorrect ExecutersWaitTimeout value: \'" + string(node->getValue(0)) + "\'";
330 //printfd(__FILE__, "DayResetTraff: %d\n", dayResetTraff);
333 if (strcasecmp(node->getName(), "DayFee") == 0)
335 if (ParseIntInRange(node->getValue(0), 0, 31, &dayFee) != 0)
337 strError = "Incorrect DayFee value: \'" + string(node->getValue(0)) + "\'";
340 //printfd(__FILE__, "DayFee: %d\n", dayFee);
343 if (strcasecmp(node->getName(), "FullFee") == 0)
345 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
347 strError = "Incorrect FullFee value: \'" + string(node->getValue(0)) + "\'";
350 //printfd(__FILE__, "DayFee: %d\n", dayFee);
353 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
355 if (ParseIntInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
357 strError = "Incorrect DayResetTraff value: \'" + string(node->getValue(0)) + "\'";
360 //printfd(__FILE__, "DayResetTraff: %d\n", dayResetTraff);
363 if (strcasecmp(node->getName(), "SpreadFee") == 0)
365 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
367 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
370 //printfd(__FILE__, "SpreadFee: %d\n", spreadFee);
373 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
375 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
377 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
380 //printfd(__FILE__, "FreeMbAllowInet: %d\n", freeMbAllowInet);
383 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
385 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
387 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
390 //printfd(__FILE__, "DayFeeIsLastDay: %d\n", dayFeeIsLastDay);
393 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
395 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
397 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
400 //printfd(__FILE__, "WriteFreeMbTraffCost: %d\n", writeFreeMbTraffCost);
403 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
405 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
407 strError = "Incorrect ShowFeeInCash value: \'" + string(node->getValue(0)) + "\'";
410 //printfd(__FILE__, "ShowFeeInCash: %d\n", showFeeInCash);
413 if (strcasecmp(node->getName(), "MonitorDir") == 0)
415 monitorDir = node->getValue(0);
419 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
425 if (strcasecmp(node->getName(), "DirNames") == 0)
427 // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ DirNames
428 const DOTCONFDocumentNode * child = node->getChildNode();
431 const DOTCONFDocumentNode * dirNameNode;
432 for (int i = 0; i < DIR_NUM; i++)
435 sprintf(strDirName, "DirName%d", i);
436 dirNameNode = conf.findNode(strDirName, node);
437 if (dirNameNode && dirNameNode->getValue(0))
439 dirName[i] = dirNameNode->getValue(0);
440 //printfd(__FILE__, "dirName[%d]: %s\n", i, dirName[i].c_str());
446 if (strcasecmp(node->getName(), "StoreModule") == 0)
448 // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ StoreModule
449 //printfd(__FILE__, "StoreModule\n");
451 if (node->getValue(1))
453 // StoreModule ÄÏÌÖÅÎ ÉÍÅÔØ 1 ÁÔÒÉÂÕÔ
454 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
458 if (storeModulesCount)
460 // äÏÌÖÅÎ ÂÙÔØ ÔÏÌØËÏ ÏÄÉÎ ÍÏÄÕÌØ StoreModule!
461 strError = "Should be only one StoreModule.";
466 //storeModuleSettings.clear(); //TODO To make constructor
467 //printfd(__FILE__, "StoreModule %s\n", node->getValue());
468 storeModuleSettings.moduleName = node->getValue(0);
469 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
472 // þÉÔÁÅÍ ÎÁÓÔÒÏÊËÉ ×ÓÅÈ ÏÓÔÁ×ÛÉÈÓÑ ÍÏÄÕÌÅÊ.
473 if (strcasecmp(node->getName(), "Modules") == 0)
475 // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ Modules
476 if (node->getValue(0))
478 // Modules ÎÅ ÄÏÌÖÅÎ ÉÍÅÔØ ÁÔÒÉÂÕÏ×
479 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
482 const DOTCONFDocumentNode * child = node->getChildNode();
486 //printfd(__FILE__, "Module \'%s\'\n", child->getValue(0));
487 if (strcasecmp(child->getName(), "Module") != 0)
489 child = child->getNextNode();
492 MODULE_SETTINGS modSettings;
493 modSettings.moduleParams.clear();
494 modSettings.moduleName = child->getValue();
496 ParseModuleSettings(child, &modSettings.moduleParams);
498 modulesSettings.push_back(modSettings);
500 child = child->getNextNode();
504 node = node->getNextNode();
507 //sort(modulesSettings.begin(), modulesSettings.end());
508 //modulesSettings.erase(unique(modulesSettings.begin(), modulesSettings.end()), modulesSettings.end());
512 //-----------------------------------------------------------------------------
513 int SETTINGS::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
515 if (detailStatPeriodStr == "1")
517 detailStatWritePeriod = dsPeriod_1;
520 else if (detailStatPeriodStr == "1/2")
522 detailStatWritePeriod = dsPeriod_1_2;
525 else if (detailStatPeriodStr == "1/4")
527 detailStatWritePeriod = dsPeriod_1_4;
530 else if (detailStatPeriodStr == "1/6")
532 detailStatWritePeriod = dsPeriod_1_6;
538 //-----------------------------------------------------------------------------
539 int SETTINGS::Reload ()
541 return ReadSettings();
543 //-----------------------------------------------------------------------------
544 const MODULE_SETTINGS & SETTINGS::GetStoreModuleSettings() const
546 return storeModuleSettings;
548 //-----------------------------------------------------------------------------
549 const vector<MODULE_SETTINGS> & SETTINGS::GetModulesSettings() const
551 return modulesSettings;
553 //-----------------------------------------------------------------------------
554 /*int SETTINGS::GetExecutersWaitTimeout() const
556 return executersWaitTimeout;
558 //-----------------------------------------------------------------------------