7 #include "stg/tariffs.h"
8 #include "stg/admins.h"
10 #include "stgconfig.h"
12 class STGCONFIG_CREATOR
15 STG_CONFIG * stgconfig;
19 : stgconfig(new STG_CONFIG())
27 STG_CONFIG * GetPlugin()
32 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 STGCONFIG_CREATOR stgc;
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 STG_CONFIG_SETTINGS::STG_CONFIG_SETTINGS()
43 //-----------------------------------------------------------------------------
44 const std::string & STG_CONFIG_SETTINGS::GetStrError() const
48 //-----------------------------------------------------------------------------
49 int STG_CONFIG_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val)
51 if (str2x(str.c_str(), *val))
53 errorStr = "Incorrect value \'" + str + "\'.";
56 if (*val < min || *val > max)
58 errorStr = "Value \'" + str + "\' out of range.";
63 //-----------------------------------------------------------------------------
64 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
68 vector<PARAM_VALUE>::const_iterator pvi;
69 ///////////////////////////
71 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
72 if (pvi == s.moduleParams.end())
74 errorStr = "Parameter \'Port\' not found.";
75 printfd(__FILE__, "Parameter 'Port' not found\n");
78 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
80 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
81 printfd(__FILE__, "%s\n", errorStr.c_str());
88 //-----------------------------------------------------------------------------
89 uint16_t STG_CONFIG_SETTINGS::GetPort() const
93 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
98 return stgc.GetPlugin();
100 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
103 const std::string STG_CONFIG::GetVersion() const
105 return "Stg configurator v.0.08";
107 //-----------------------------------------------------------------------------
108 STG_CONFIG::STG_CONFIG()
118 //-----------------------------------------------------------------------------
119 void STG_CONFIG::SetUsers(USERS * u)
123 //-----------------------------------------------------------------------------
124 void STG_CONFIG::SetTariffs(TARIFFS * t)
128 //-----------------------------------------------------------------------------
129 void STG_CONFIG::SetAdmins(ADMINS * a)
133 //-----------------------------------------------------------------------------
134 void STG_CONFIG::SetStore(STORE * s)
138 //-----------------------------------------------------------------------------
139 void STG_CONFIG::SetStgSettings(const SETTINGS * s)
143 //-----------------------------------------------------------------------------
144 void STG_CONFIG::SetSettings(const MODULE_SETTINGS & s)
148 //-----------------------------------------------------------------------------
149 int STG_CONFIG::ParseSettings()
151 int ret = stgConfigSettings.ParseSettings(settings);
153 errorStr = stgConfigSettings.GetStrError();
156 //-----------------------------------------------------------------------------
157 const std::string & STG_CONFIG::GetStrError() const
161 //-----------------------------------------------------------------------------
162 int STG_CONFIG::Start()
169 config.SetPort(stgConfigSettings.GetPort());
170 config.SetAdmins(admins);
171 config.SetUsers(users);
172 config.SetTariffs(tariffs);
173 config.SetStgSettings(stgSettings);
174 config.SetStore(store);
176 if (config.Prepare())
178 errorStr = config.GetStrError();
182 if (pthread_create(&thread, NULL, Run, this))
184 errorStr = "Cannot create thread.";
185 printfd(__FILE__, "Cannot create thread\n");
191 //-----------------------------------------------------------------------------
192 int STG_CONFIG::Stop()
199 //5 seconds to thread stops itself
201 for (i = 0; i < 25; i++)
209 //after 5 seconds waiting thread still running. now killing it
212 //TODO pthread_cancel()
213 if (pthread_kill(thread, SIGINT))
215 errorStr = "Cannot kill thread.";
216 printfd(__FILE__, "Cannot kill thread\n");
219 printfd(__FILE__, "STG_CONFIG killed\n");
224 //-----------------------------------------------------------------------------
225 bool STG_CONFIG::IsRunning()
229 //-----------------------------------------------------------------------------
230 void * STG_CONFIG::Run(void * d)
232 STG_CONFIG * stgConf = (STG_CONFIG *)d;
233 stgConf->isRunning = true;
235 stgConf->config.Run(&stgConf->config);
237 stgConf->isRunning = false;
240 //-----------------------------------------------------------------------------
241 uint16_t STG_CONFIG::GetStartPosition() const
245 //-----------------------------------------------------------------------------
246 uint16_t STG_CONFIG::GetStopPosition() const
250 //-----------------------------------------------------------------------------