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),
60 writeFreeMbTraffCost(false),
64 reconnectOnTariffChange(false),
65 disableSessionLog(false),
66 logger(GetStgLogger())
68 filterParamsLog.push_back("*");
70 //-----------------------------------------------------------------------------
71 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
74 modulesPath(rval.modulesPath),
75 dirName(rval.dirName),
76 confDir(rval.confDir),
77 scriptsDir(rval.scriptsDir),
79 logFile(rval.logFile),
80 pidFile(rval.pidFile),
81 monitorDir(rval.monitorDir),
82 monitoring(rval.monitoring),
83 detailStatWritePeriod(rval.detailStatWritePeriod),
84 statWritePeriod(rval.statWritePeriod),
85 stgExecMsgKey(rval.stgExecMsgKey),
86 executersNum(rval.executersNum),
87 fullFee(rval.fullFee),
89 dayResetTraff(rval.dayResetTraff),
90 spreadFee(rval.spreadFee),
91 freeMbAllowInet(rval.freeMbAllowInet),
92 dayFeeIsLastDay(rval.dayFeeIsLastDay),
93 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
94 showFeeInCash(rval.showFeeInCash),
95 messageTimeout(rval.messageTimeout),
96 feeChargeType(rval.feeChargeType),
97 reconnectOnTariffChange(rval.reconnectOnTariffChange),
98 disableSessionLog(rval.disableSessionLog),
99 filterParamsLog(rval.filterParamsLog),
100 modulesSettings(rval.modulesSettings),
101 storeModuleSettings(rval.storeModuleSettings),
102 logger(GetStgLogger())
105 //-----------------------------------------------------------------------------
106 SETTINGS_IMPL & SETTINGS_IMPL::operator=(const SETTINGS_IMPL & rhs)
108 modulesPath = rhs.modulesPath;
109 dirName = rhs.dirName;
110 confDir = rhs.confDir;
111 scriptsDir = rhs.scriptsDir;
113 logFile = rhs.logFile;
114 pidFile = rhs.pidFile;
115 monitorDir = rhs.monitorDir;
116 scriptParams = rhs.scriptParams;
117 monitoring = rhs.monitoring;
118 detailStatWritePeriod = rhs.detailStatWritePeriod;
119 statWritePeriod = rhs.statWritePeriod;
120 stgExecMsgKey = rhs.stgExecMsgKey;
121 executersNum = rhs.executersNum;
122 fullFee = rhs.fullFee;
124 dayResetTraff = rhs.dayResetTraff;
125 spreadFee = rhs.spreadFee;
126 freeMbAllowInet = rhs.freeMbAllowInet;
127 dayFeeIsLastDay = rhs.dayFeeIsLastDay;
128 writeFreeMbTraffCost = rhs.writeFreeMbTraffCost;
129 showFeeInCash = rhs.showFeeInCash;
130 messageTimeout = rhs.messageTimeout;
131 feeChargeType = rhs.feeChargeType;
132 reconnectOnTariffChange = rhs.reconnectOnTariffChange;
133 disableSessionLog = rhs.disableSessionLog;
134 filterParamsLog = rhs.filterParamsLog;
136 modulesSettings = rhs.modulesSettings;
137 storeModuleSettings = rhs.storeModuleSettings;
140 //-----------------------------------------------------------------------------
141 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
143 const DOTCONFDocumentNode * childNode;
147 pv.param = node->getName();
149 if (node->getValue(1))
151 strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'.";
155 value = node->getValue(0);
159 strError = "Module name expected.";
163 childNode = node->getChildNode();
166 pv.param = childNode->getName();
168 while ((value = childNode->getValue(i++)) != NULL)
170 pv.value.push_back(value);
172 params->push_back(pv);
174 childNode = childNode->getNextNode();
179 //-----------------------------------------------------------------------------
180 void SETTINGS_IMPL::ErrorCallback(void * data, const char * buf)
182 printfd(__FILE__, "SETTINGS_IMPL::ErrorCallback() - %s\n", buf);
183 SETTINGS_IMPL * settings = static_cast<SETTINGS_IMPL *>(data);
184 settings->logger("%s", buf);
186 //-----------------------------------------------------------------------------
187 int SETTINGS_IMPL::ReadSettings()
189 const char * requiredOptions[] = {
195 "DetailStatWritePeriod",
201 "WriteFreeMbTraffCost",
204 int storeModulesCount = 0;
205 modulesSettings.clear();
207 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
208 conf.setErrorCallback(SETTINGS_IMPL::ErrorCallback, this);
209 conf.setRequiredOptionNames(requiredOptions);
210 std::string confFile = confDir + "/stargazer.conf";
212 if(conf.setContent(confFile.c_str()) != 0)
214 strError = "Cannot read file " + confFile;
218 const DOTCONFDocumentNode * node = conf.getFirstNode();
222 if (strcasecmp(node->getName(), "ScriptDir") == 0)
224 scriptsDir = node->getValue(0);
227 if (strcasecmp(node->getName(), "LogFile") == 0)
229 logFile = node->getValue(0);
232 if (strcasecmp(node->getName(), "PIDFile") == 0)
234 pidFile = node->getValue(0);
237 if (strcasecmp(node->getName(), "ModulesPath") == 0)
239 modulesPath = node->getValue(0);
242 if (strcasecmp(node->getName(), "Rules") == 0)
244 rules = node->getValue(0);
247 if (strcasecmp(node->getName(), "DetailStatWritePeriod") == 0)
249 if (ParseDetailStatWritePeriod(node->getValue(0)) != 0)
251 strError = "Incorrect DetailStatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
256 if (strcasecmp(node->getName(), "StatWritePeriod") == 0)
258 if (ParseUnsignedInRange(node->getValue(0), 1, 1440, &statWritePeriod) != 0)
260 strError = "Incorrect StatWritePeriod value: \'" + std::string(node->getValue(0)) + "\'";
265 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
267 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
269 strError = "Incorrect ExecMsgKey value: \'" + std::string(node->getValue(0)) + "\'";
274 if (strcasecmp(node->getName(), "ExecutersNum") == 0)
276 if (ParseUnsignedInRange(node->getValue(0), 1, 1024, &executersNum) != 0)
278 strError = "Incorrect ExecutersNum value: \'" + std::string(node->getValue(0)) + "\'";
283 if (strcasecmp(node->getName(), "DayFee") == 0)
285 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayFee) != 0)
287 strError = "Incorrect DayFee value: \'" + std::string(node->getValue(0)) + "\'";
292 if (strcasecmp(node->getName(), "FullFee") == 0)
294 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
296 strError = "Incorrect FullFee value: \'" + std::string(node->getValue(0)) + "\'";
301 if (strcasecmp(node->getName(), "DayResetTraff") == 0)
303 if (ParseUnsignedInRange(node->getValue(0), 0, 31, &dayResetTraff) != 0)
305 strError = "Incorrect DayResetTraff value: \'" + std::string(node->getValue(0)) + "\'";
310 if (strcasecmp(node->getName(), "SpreadFee") == 0)
312 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
314 strError = "Incorrect SpreadFee value: \'" + std::string(node->getValue(0)) + "\'";
319 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
321 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
323 strError = "Incorrect FreeMbAllowInet value: \'" + std::string(node->getValue(0)) + "\'";
328 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
330 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
332 strError = "Incorrect DayFeeIsLastDay value: \'" + std::string(node->getValue(0)) + "\'";
337 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
339 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
341 strError = "Incorrect WriteFreeMbTraffCost value: \'" + std::string(node->getValue(0)) + "\'";
346 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
348 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
350 strError = "Incorrect ShowFeeInCash value: \'" + std::string(node->getValue(0)) + "\'";
355 if (strcasecmp(node->getName(), "MonitorDir") == 0)
357 monitorDir = node->getValue(0);
361 if (!lstat(monitorDir.c_str(), &stat) && S_ISDIR(stat.st_mode))
367 if (strcasecmp(node->getName(), "MessageTimeout") == 0)
369 if (ParseUnsigned(node->getValue(0), &messageTimeout) != 0)
371 strError = "Incorrect MessageTimeout value: \'" + std::string(node->getValue(0)) + "\'";
376 if (strcasecmp(node->getName(), "FeeChargeType") == 0)
378 if (ParseUnsignedInRange(node->getValue(0), 0, 3, &feeChargeType) != 0)
380 strError = "Incorrect FeeChargeType value: \'" + std::string(node->getValue(0)) + "\'";
385 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
387 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
389 strError = "Incorrect ReconnectOnTariffChange value: \'" + std::string(node->getValue(0)) + "\'";
394 if (strcasecmp(node->getName(), "DisableSessionLog") == 0)
396 if (ParseYesNo(node->getValue(0), &disableSessionLog) != 0)
398 strError = "Incorrect DisableSessionLog value: \'" + std::string(node->getValue(0)) + "\'";
403 if (strcasecmp(node->getName(), "FilterParamsLog") == 0)
405 filterParamsLog.clear();
406 for (int i = 0; node->getValue(i) != NULL; ++i)
407 filterParamsLog.push_back(node->getValue(i));
410 if (strcasecmp(node->getName(), "DirNames") == 0)
412 const DOTCONFDocumentNode * child = node->getChildNode();
415 const DOTCONFDocumentNode * dirNameNode;
416 dirName.reserve(DIR_NUM);
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 \'" + std::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 \'" + std::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 if (strcasecmp(node->getName(), "ScriptParams") == 0)
478 for (int i = 0; node->getValue(i) != NULL; ++i)
479 scriptParams.push_back(node->getValue(i));
481 node = node->getNextNode();
486 //-----------------------------------------------------------------------------
487 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const std::string & detailStatPeriodStr)
489 if (detailStatPeriodStr == "1")
491 detailStatWritePeriod = dsPeriod_1;
494 else if (detailStatPeriodStr == "1/2")
496 detailStatWritePeriod = dsPeriod_1_2;
499 else if (detailStatPeriodStr == "1/4")
501 detailStatWritePeriod = dsPeriod_1_4;
504 else if (detailStatPeriodStr == "1/6")
506 detailStatWritePeriod = dsPeriod_1_6;
512 //-----------------------------------------------------------------------------