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"
39 //-----------------------------------------------------------------------------
40 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
41 : modulesPath("/usr/lib/stg"),
43 confDir(cd.empty() ? "/etc/stargazer" : cd),
45 rules(confDir + "/rules"),
46 logFile("/var/log/stargazer.log"),
47 pidFile("/var/run/stargazer.pid"),
48 monitorDir("/var/stargazer/monitoring"),
50 detailStatWritePeriod(dsPeriod_1_6),
58 freeMbAllowInet(false),
59 dayFeeIsLastDay(false),
61 writeFreeMbTraffCost(false),
65 reconnectOnTariffChange(false),
66 logger(GetStgLogger())
69 //-----------------------------------------------------------------------------
70 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
71 : modulesPath(rval.modulesPath),
72 dirName(rval.dirName),
73 confDir(rval.confDir),
74 scriptsDir(rval.scriptsDir),
76 logFile(rval.logFile),
77 pidFile(rval.pidFile),
78 monitorDir(rval.monitorDir),
79 monitoring(rval.monitoring),
80 detailStatWritePeriod(rval.detailStatWritePeriod),
81 statWritePeriod(rval.statWritePeriod),
82 stgExecMsgKey(rval.stgExecMsgKey),
83 executersNum(rval.executersNum),
84 fullFee(rval.fullFee),
86 dayResetTraff(rval.dayResetTraff),
87 spreadFee(rval.spreadFee),
88 freeMbAllowInet(rval.freeMbAllowInet),
89 dayFeeIsLastDay(rval.dayFeeIsLastDay),
90 stopOnError(rval.stopOnError),
91 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
92 showFeeInCash(rval.showFeeInCash),
93 messageTimeout(rval.messageTimeout),
94 feeChargeType(rval.feeChargeType),
95 reconnectOnTariffChange(rval.reconnectOnTariffChange),
96 modulesSettings(rval.modulesSettings),
97 storeModuleSettings(rval.storeModuleSettings),
98 logger(GetStgLogger())
101 //-----------------------------------------------------------------------------
102 SETTINGS_IMPL & SETTINGS_IMPL::operator=(const SETTINGS_IMPL & rhs)
104 modulesPath = rhs.modulesPath;
105 dirName = rhs.dirName;
106 confDir = rhs.confDir;
107 scriptsDir = rhs.scriptsDir;
109 logFile = rhs.logFile;
110 pidFile = rhs.pidFile;
111 monitorDir = rhs.monitorDir;
112 scriptParams = rhs.scriptParams;
113 monitoring = rhs.monitoring;
114 detailStatWritePeriod = rhs.detailStatWritePeriod;
115 statWritePeriod = rhs.statWritePeriod;
116 stgExecMsgKey = rhs.stgExecMsgKey;
117 executersNum = rhs.executersNum;
118 fullFee = rhs.fullFee;
120 dayResetTraff = rhs.dayResetTraff;
121 spreadFee = rhs.spreadFee;
122 freeMbAllowInet = rhs.freeMbAllowInet;
123 dayFeeIsLastDay = rhs.dayFeeIsLastDay;
124 stopOnError = rhs.stopOnError;
125 writeFreeMbTraffCost = rhs.writeFreeMbTraffCost;
126 showFeeInCash = rhs.showFeeInCash;
127 messageTimeout = rhs.messageTimeout;
128 feeChargeType = rhs.feeChargeType;
129 reconnectOnTariffChange = rhs.reconnectOnTariffChange;
131 modulesSettings = rhs.modulesSettings;
132 storeModuleSettings = rhs.storeModuleSettings;
135 //-----------------------------------------------------------------------------
136 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
138 const DOTCONFDocumentNode * childNode;
142 pv.param = node->getName();
144 if (node->getValue(1))
146 strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'.";
150 value = node->getValue(0);
154 strError = "Module name expected.";
158 childNode = node->getChildNode();
161 pv.param = childNode->getName();
163 while ((value = childNode->getValue(i++)) != NULL)
165 pv.value.push_back(value);
167 params->push_back(pv);
169 childNode = childNode->getNextNode();
174 //-----------------------------------------------------------------------------
175 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
177 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
178 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
179 settings->logger("%s", buf);
181 //-----------------------------------------------------------------------------
182 int SETTINGS_IMPL::ReadSettings()
184 const char * requiredOptions[] = {
190 "DetailStatWritePeriod",
196 "WriteFreeMbTraffCost",
199 int storeModulesCount = 0;
200 modulesSettings.clear();
202 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
203 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
204 conf.setRequiredOptionNames(requiredOptions);
205 std::string confFile = confDir + "/stargazer.conf";
207 if(conf.setContent(confFile.c_str()) != 0)
209 strError = "Cannot read file " + confFile;
213 const DOTCONFDocumentNode * node = conf.getFirstNode();
217 if (strcasecmp(node->getName(), "ScriptDir") == 0)
219 scriptsDir = node->getValue(0);
222 if (strcasecmp(node->getName(), "LogFile") == 0)
224 logFile = node->getValue(0);
227 if (strcasecmp(node->getName(), "PIDFile") == 0)
229 pidFile = node->getValue(0);
232 if (strcasecmp(node->getName(), "ModulesPath") == 0)
234 modulesPath = node->getValue(0);
237 if (strcasecmp(node->getName(), "Rules") == 0)
239 rules = node->getValue(0);
242 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
244 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
246 strError = "Incorrect DetailStatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
251 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
253 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
255 strError = "Incorrect StatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
260 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
262 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
264 strError = "Incorrect ExecMsgKey value: \'" + std::string(node->getValue(0)) + "\'";
269 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
271 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
273 strError = "Incorrect ExecutersNum value: \'" + std::string(node->getValue(0)) + "\'";
278 if (strcasecmp(node->getName(), "DayFee") == 0)
280 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
282 strError = "Incorrect DayFee value: \'" + std::string(node->getValue(0)) + "\'";
287 if (strcasecmp(node->getName(), "FullFee") == 0)
289 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
291 strError = "Incorrect FullFee value: \'" + std::string(node->getValue(0)) + "\'";
296 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
298 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
300 strError = "Incorrect DayResetTraff value: \'" + std::string(node->getValue(0)) + "\'";
305 if (strcasecmp(node->getName(), "SpreadFee") == 0)
307 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
309 strError = "Incorrect SpreadFee value: \'" + std::string(node->getValue(0)) + "\'";
314 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
316 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
318 strError = "Incorrect FreeMbAllowInet value: \'" + std::string(node->getValue(0)) + "\'";
323 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
325 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
327 strError = "Incorrect DayFeeIsLastDay value: \'" + std::string(node->getValue(0)) + "\'";
332 if (strcasecmp(node->getName(), "StopOnError") == 0)
334 if (ParseYesNo(node->getValue(0), &stopOnError) != 0)
336 strError = "Incorrect StopOnError value: \'" + std::string(node->getValue(0)) + "\'";
341 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
343 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
345 strError = "Incorrect WriteFreeMbTraffCost value: \'" + std::string(node->getValue(0)) + "\'";
350 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
352 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
354 strError = "Incorrect ShowFeeInCash value: \'" + std::string(node->getValue(0)) + "\'";
359 if (strcasecmp(node->getName(), "MonitorDir") == 0)
361 monitorDir = node->getValue(0);
365 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
371 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
373 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
375 strError = "Incorrect MessageTimeout value: \'" + std::string(node->getValue(0)) + "\'";
380 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
382 if (ParseUnsignedInRange(node->getValue(0), 0, 3, &feeChargeType) != 0)
384 strError = "Incorrect FeeChargeType value: \'" + std::string(node->getValue(0)) + "\'";
389 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
391 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
393 strError = "Incorrect ReconnectOnTariffChange value: \'" + std::string(node->getValue(0)) + "\'";
398 if (strcasecmp(node->getName(), "DirNames") == 0)
400 const DOTCONFDocumentNode * child = node->getChildNode();
403 const DOTCONFDocumentNode * dirNameNode;
404 dirName.reserve(DIR_NUM);
405 for (int i = 0; i < DIR_NUM; i++)
408 sprintf(strDirName, "DirName%d", i);
409 dirNameNode = conf.findNode(strDirName, node);
410 if (dirNameNode && dirNameNode->getValue(0))
412 dirName[i] = dirNameNode->getValue(0);
418 if (strcasecmp(node->getName(), "StoreModule") == 0)
420 if (node->getValue(1))
422 strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
426 if (storeModulesCount)
428 strError = "Should be only one StoreModule.";
433 storeModuleSettings.moduleName = node->getValue(0);
434 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
437 if (strcasecmp(node->getName(), "Modules") == 0)
439 if (node->getValue(0))
441 strError = "Unexpected \'" + std::string(node->getValue(0)) + "\'.";
444 const DOTCONFDocumentNode * child = node->getChildNode();
447 if (strcasecmp(child->getName(), "Module") != 0)
449 child = child->getNextNode();
452 MODULE_SETTINGS modSettings;
453 modSettings.moduleParams.clear();
454 modSettings.moduleName = child->getValue();
456 ParseModuleSettings(child, &modSettings.moduleParams);
458 modulesSettings.push_back(modSettings);
460 child = child->getNextNode();
464 if (strcasecmp(node->getName(), "ScriptParams") == 0)
466 for (int i = 0; node->getValue(i) != NULL; ++i)
468 scriptParams.push_back(node->getValue(i));
471 node = node->getNextNode();
476 //-----------------------------------------------------------------------------
477 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const std::string & detailStatPeriodStr)
479 if (detailStatPeriodStr == "1")
481 detailStatWritePeriod = dsPeriod_1;
484 else if (detailStatPeriodStr == "1/2")
486 detailStatWritePeriod = dsPeriod_1_2;
489 else if (detailStatPeriodStr == "1/4")
491 detailStatWritePeriod = dsPeriod_1_4;
494 else if (detailStatPeriodStr == "1/6")
496 detailStatWritePeriod = dsPeriod_1_6;
502 //-----------------------------------------------------------------------------