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"
41 //-----------------------------------------------------------------------------
42 SETTINGS_IMPL::SETTINGS_IMPL()
45 modulesPath("/usr/lib/stg"),
47 confDir("/etc/stargazer"),
48 scriptsDir("/etc/stargazer"),
49 rules("/etc/stargazer/rules"),
50 logFile("/var/log/stargazer.log"),
51 pidFile("/var/run/stargazer.pid"),
52 monitorDir("/var/stargazer/monitoring"),
54 detailStatWritePeriod(dsPeriod_1_6),
62 freeMbAllowInet(false),
63 dayFeeIsLastDay(false),
64 writeFreeMbTraffCost(false),
68 reconnectOnTariffChange(false),
70 storeModuleSettings(),
71 logger(GetStgLogger())
74 //-----------------------------------------------------------------------------
75 SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
78 modulesPath("/usr/lib/stg"),
83 logFile("/var/log/stargazer.log"),
84 pidFile("/var/run/stargazer.pid"),
85 monitorDir("/var/stargazer/monitoring"),
87 detailStatWritePeriod(dsPeriod_1_6),
95 freeMbAllowInet(false),
96 dayFeeIsLastDay(false),
97 writeFreeMbTraffCost(false),
101 reconnectOnTariffChange(false),
103 storeModuleSettings(),
104 logger(GetStgLogger())
107 //-----------------------------------------------------------------------------
108 SETTINGS_IMPL::SETTINGS_IMPL(const SETTINGS_IMPL & rval)
111 modulesPath(rval.modulesPath),
112 dirName(rval.dirName),
113 confDir(rval.confDir),
114 scriptsDir(rval.scriptsDir),
116 logFile(rval.logFile),
117 pidFile(rval.pidFile),
118 monitorDir(rval.monitorDir),
119 monitoring(rval.monitoring),
120 detailStatWritePeriod(rval.detailStatWritePeriod),
121 statWritePeriod(rval.statWritePeriod),
122 stgExecMsgKey(rval.stgExecMsgKey),
123 executersNum(rval.executersNum),
124 fullFee(rval.fullFee),
126 dayResetTraff(rval.dayResetTraff),
127 spreadFee(rval.spreadFee),
128 freeMbAllowInet(rval.freeMbAllowInet),
129 dayFeeIsLastDay(rval.dayFeeIsLastDay),
130 writeFreeMbTraffCost(rval.writeFreeMbTraffCost),
131 showFeeInCash(rval.showFeeInCash),
132 messageTimeout(rval.messageTimeout),
133 feeChargeType(rval.feeChargeType),
134 reconnectOnTariffChange(rval.reconnectOnTariffChange),
135 modulesSettings(rval.modulesSettings),
136 storeModuleSettings(rval.storeModuleSettings),
137 logger(GetStgLogger())
140 //-----------------------------------------------------------------------------
141 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
143 const DOTCONFDocumentNode * childNode;
147 pv.param = node->getName();
149 if (node->getValue(1))
151 strError = "Unexpected value \'" + 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 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: \'" + 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: \'" + string(node->getValue(0)) + "\'";
265 if (strcasecmp(node->getName(), "ExecMsgKey") == 0)
267 if (ParseInt(node->getValue(0), &stgExecMsgKey) != 0)
269 strError = "Incorrect ExecMsgKey value: \'" + 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: \'" + 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: \'" + string(node->getValue(0)) + "\'";
292 if (strcasecmp(node->getName(), "FullFee") == 0)
294 if (ParseYesNo(node->getValue(0), &fullFee) != 0)
296 strError = "Incorrect FullFee value: \'" + 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: \'" + string(node->getValue(0)) + "\'";
310 if (strcasecmp(node->getName(), "SpreadFee") == 0)
312 if (ParseYesNo(node->getValue(0), &spreadFee) != 0)
314 strError = "Incorrect SpreadFee value: \'" + string(node->getValue(0)) + "\'";
319 if (strcasecmp(node->getName(), "FreeMbAllowInet") == 0)
321 if (ParseYesNo(node->getValue(0), &freeMbAllowInet) != 0)
323 strError = "Incorrect FreeMbAllowInet value: \'" + string(node->getValue(0)) + "\'";
328 if (strcasecmp(node->getName(), "DayFeeIsLastDay") == 0)
330 if (ParseYesNo(node->getValue(0), &dayFeeIsLastDay) != 0)
332 strError = "Incorrect DayFeeIsLastDay value: \'" + string(node->getValue(0)) + "\'";
337 if (strcasecmp(node->getName(), "WriteFreeMbTraffCost") == 0)
339 if (ParseYesNo(node->getValue(0), &writeFreeMbTraffCost) != 0)
341 strError = "Incorrect WriteFreeMbTraffCost value: \'" + string(node->getValue(0)) + "\'";
346 if (strcasecmp(node->getName(), "ShowFeeInCash") == 0)
348 if (ParseYesNo(node->getValue(0), &showFeeInCash) != 0)
350 strError = "Incorrect ShowFeeInCash value: \'" + 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: \'" + 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: \'" + string(node->getValue(0)) + "\'";
385 if (strcasecmp(node->getName(), "ReconnectOnTariffChange") == 0)
387 if (ParseYesNo(node->getValue(0), &reconnectOnTariffChange) != 0)
389 strError = "Incorrect ReconnectOnTariffChange value: \'" + string(node->getValue(0)) + "\'";
394 if (strcasecmp(node->getName(), "DirNames") == 0)
396 const DOTCONFDocumentNode * child = node->getChildNode();
399 const DOTCONFDocumentNode * dirNameNode;
400 dirName.reserve(DIR_NUM);
401 for (int i = 0; i < DIR_NUM; i++)
404 sprintf(strDirName, "DirName%d", i);
405 dirNameNode = conf.findNode(strDirName, node);
406 if (dirNameNode && dirNameNode->getValue(0))
408 dirName[i] = dirNameNode->getValue(0);
414 if (strcasecmp(node->getName(), "StoreModule") == 0)
416 if (node->getValue(1))
418 strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
422 if (storeModulesCount)
424 strError = "Should be only one StoreModule.";
429 storeModuleSettings.moduleName = node->getValue(0);
430 ParseModuleSettings(node, &storeModuleSettings.moduleParams);
433 if (strcasecmp(node->getName(), "Modules") == 0)
435 if (node->getValue(0))
437 strError = "Unexpected \'" + string(node->getValue(0)) + "\'.";
440 const DOTCONFDocumentNode * child = node->getChildNode();
443 if (strcasecmp(child->getName(), "Module") != 0)
445 child = child->getNextNode();
448 MODULE_SETTINGS modSettings;
449 modSettings.moduleParams.clear();
450 modSettings.moduleName = child->getValue();
452 ParseModuleSettings(child, &modSettings.moduleParams);
454 modulesSettings.push_back(modSettings);
456 child = child->getNextNode();
460 if (strcasecmp(node->getName(), "ScriptParams") == 0)
462 for (int i = 0; node->getValue(i) != NULL; ++i)
464 scriptParams.push_back(node->getValue(i));
467 node = node->getNextNode();
472 //-----------------------------------------------------------------------------
473 int SETTINGS_IMPL::ParseDetailStatWritePeriod(const string & detailStatPeriodStr)
475 if (detailStatPeriodStr == "1")
477 detailStatWritePeriod = dsPeriod_1;
480 else if (detailStatPeriodStr == "1/2")
482 detailStatWritePeriod = dsPeriod_1_2;
485 else if (detailStatPeriodStr == "1/4")
487 detailStatWritePeriod = dsPeriod_1_4;
490 else if (detailStatPeriodStr == "1/6")
492 detailStatWritePeriod = dsPeriod_1_6;
498 //-----------------------------------------------------------------------------