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)
73 modulesPath(rval.modulesPath),
74 dirName(rval.dirName),
75 confDir(rval.confDir),
76 scriptsDir(rval.scriptsDir),
78 logFile(rval.logFile),
79 pidFile(rval.pidFile),
80 monitorDir(rval.monitorDir),
81 monitoring(rval.monitoring),
82 detailStatWritePeriod(rval.detailStatWritePeriod),
83 statWritePeriod(rval.statWritePeriod),
84 stgExecMsgKey(rval.stgExecMsgKey),
85 executersNum(rval.executersNum),
86 fullFee(rval.fullFee),
88 dayResetTraff(rval.dayResetTraff),
89 spreadFee(rval.spreadFee),
90 freeMbAllowInet(rval.freeMbAllowInet),
91 dayFeeIsLastDay(rval.dayFeeIsLastDay),
92 stopOnError(rval.stopOnError),
93 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
94 showFeeInCash(rval.showFeeInCash),
95 messageTimeout(rval.messageTimeout),
96 feeChargeType(rval.feeChargeType),
97 reconnectOnTariffChange(rval.reconnectOnTariffChange),
98 modulesSettings(rval.modulesSettings),
99 storeModuleSettings(rval.storeModuleSettings),
100 logger(GetStgLogger())
103 //-----------------------------------------------------------------------------
104 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
106 const DOTCONFDocumentNode * childNode;
110 pv.param = node->getName();
112 if (node->getValue(1))
114 strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'.";
118 value = node->getValue(0);
122 strError = "Module name expected.";
126 childNode = node->getChildNode();
129 pv.param = childNode->getName();
131 while ((value = childNode->getValue(i++)) != NULL)
133 pv.value.push_back(value);
135 params->push_back(pv);
137 childNode = childNode->getNextNode();
142 //-----------------------------------------------------------------------------
143 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
145 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
146 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
147 settings->logger("%s", buf);
149 //-----------------------------------------------------------------------------
150 int SETTINGS_IMPL::ReadSettings()
152 const char * requiredOptions[] = {
158 "DetailStatWritePeriod",
164 "WriteFreeMbTraffCost",
167 int storeModulesCount = 0;
168 modulesSettings.clear();
170 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
171 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
172 conf.setRequiredOptionNames(requiredOptions);
173 std::string confFile = confDir + "/stargazer.conf";
175 if(conf.setContent(confFile.c_str()) != 0)
177 strError = "Cannot read file " + confFile;
181 const DOTCONFDocumentNode * node = conf.getFirstNode();
185 if (strcasecmp(node->getName(), "ScriptDir") == 0)
187 scriptsDir = node->getValue(0);
190 if (strcasecmp(node->getName(), "LogFile") == 0)
192 logFile = node->getValue(0);
195 if (strcasecmp(node->getName(), "PIDFile") == 0)
197 pidFile = node->getValue(0);
200 if (strcasecmp(node->getName(), "ModulesPath") == 0)
202 modulesPath = node->getValue(0);
205 if (strcasecmp(node->getName(), "Rules") == 0)
207 rules = node->getValue(0);
210 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
212 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
214 strError = "Incorrect DetailStatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
219 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
221 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
223 strError = "Incorrect StatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
228 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
230 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
232 strError = "Incorrect ExecMsgKey value: \'" + std::string(node->getValue(0)) + "\'";
237 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
239 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
241 strError = "Incorrect ExecutersNum value: \'" + std::string(node->getValue(0)) + "\'";
246 if (strcasecmp(node->getName(), "DayFee") == 0)
248 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
250 strError = "Incorrect DayFee value: \'" + std::string(node->getValue(0)) + "\'";
255 if (strcasecmp(node->getName(), "FullFee") == 0)
257 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
259 strError = "Incorrect FullFee value: \'" + std::string(node->getValue(0)) + "\'";
264 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
266 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
268 strError = "Incorrect DayResetTraff value: \'" + std::string(node->getValue(0)) + "\'";
273 if (strcasecmp(node->getName(), "SpreadFee") == 0)
275 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
277 strError = "Incorrect SpreadFee value: \'" + std::string(node->getValue(0)) + "\'";
282 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
284 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
286 strError = "Incorrect FreeMbAllowInet value: \'" + std::string(node->getValue(0)) + "\'";
291 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
293 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
295 strError = "Incorrect DayFeeIsLastDay value: \'" + std::string(node->getValue(0)) + "\'";
300 if (strcasecmp(node->getName(), "StopOnError") == 0)
302 if (ParseYesNo(node->getValue(0), &stopOnError) != 0)
304 strError = "Incorrect StopOnError value: \'" + std::string(node->getValue(0)) + "\'";
309 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
311 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
313 strError = "Incorrect WriteFreeMbTraffCost value: \'" + std::string(node->getValue(0)) + "\'";
318 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
320 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
322 strError = "Incorrect ShowFeeInCash value: \'" + std::string(node->getValue(0)) + "\'";
327 if (strcasecmp(node->getName(), "MonitorDir") == 0)
329 monitorDir = node->getValue(0);
333 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
339 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
341 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
343 strError = "Incorrect MessageTimeout value: \'" + std::string(node->getValue(0)) + "\'";
348 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
350 if (ParseUnsignedInRange(node->getValue(0), 0, 3, &feeChargeType) != 0)
352 strError = "Incorrect FeeChargeType value: \'" + std::string(node->getValue(0)) + "\'";
357 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
359 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
361 strError = "Incorrect ReconnectOnTariffChange value: \'" + std::string(node->getValue(0)) + "\'";
366 if (strcasecmp(node->getName(), "DirNames") == 0)
368 const DOTCONFDocumentNode * child = node->getChildNode();
371 const DOTCONFDocumentNode * dirNameNode;
372 dirName.reserve(DIR_NUM);
373 for (int i = 0; i < DIR_NUM; i++)
376 sprintf(strDirName, "DirName%d", i);
377 dirNameNode = conf.findNode(strDirName, node);
378 if (dirNameNode && dirNameNode->getValue(0))
380 dirName[i] = dirNameNode->getValue(0);
386 if (strcasecmp(node->getName(), "StoreModule") == 0)
388 if (node->getValue(1))
390 strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
394 if (storeModulesCount)
396 strError = "Should be only one StoreModule.";
401 storeModuleSettings.moduleName = node->getValue(0);
402 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
405 if (strcasecmp(node->getName(), "Modules") == 0)
407 if (node->getValue(0))
409 strError = "Unexpected \'" + std::string(node->getValue(0)) + "\'.";
412 const DOTCONFDocumentNode * child = node->getChildNode();
415 if (strcasecmp(child->getName(), "Module") != 0)
417 child = child->getNextNode();
420 MODULE_SETTINGS modSettings;
421 modSettings.moduleParams.clear();
422 modSettings.moduleName = child->getValue();
424 ParseModuleSettings(child, &modSettings.moduleParams);
426 modulesSettings.push_back(modSettings);
428 child = child->getNextNode();
432 if (strcasecmp(node->getName(), "ScriptParams") == 0)
434 for (int i = 0; node->getValue(i) != NULL; ++i)
436 scriptParams.push_back(node->getValue(i));
439 node = node->getNextNode();
444 //-----------------------------------------------------------------------------
445 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const std::string & detailStatPeriodStr)
447 if (detailStatPeriodStr == "1")
449 detailStatWritePeriod = dsPeriod_1;
452 else if (detailStatPeriodStr == "1/2")
454 detailStatWritePeriod = dsPeriod_1_2;
457 else if (detailStatPeriodStr == "1/4")
459 detailStatWritePeriod = dsPeriod_1_4;
462 else if (detailStatPeriodStr == "1/6")
464 detailStatWritePeriod = dsPeriod_1_6;
470 //-----------------------------------------------------------------------------