]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
Fix compilation issues
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / stgconfig.cpp
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <signal.h>
4
5 #include <algorithm>
6
7 #include "stgconfig.h"
8 #include "tariffs.h"
9 #include "admins.h"
10 #include "users.h"
11
12 class STGCONFIG_CREATOR
13 {
14 private:
15     STG_CONFIG * stgconfig;
16
17 public:
18     STGCONFIG_CREATOR()
19         : stgconfig(new STG_CONFIG())
20         {
21         };
22     ~STGCONFIG_CREATOR()
23         {
24         delete stgconfig;
25         };
26
27     STG_CONFIG * GetPlugin()
28         {
29         return stgconfig;
30         };
31 };
32 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 STGCONFIG_CREATOR stgc;
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 STG_CONFIG_SETTINGS::STG_CONFIG_SETTINGS()
40     : port(0)
41 {
42 }
43 //-----------------------------------------------------------------------------
44 const std::string & STG_CONFIG_SETTINGS::GetStrError() const
45 {
46 return errorStr;
47 }
48 //-----------------------------------------------------------------------------
49 int STG_CONFIG_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val)
50 {
51 if (str2x(str.c_str(), *val))
52     {
53     errorStr = "Incorrect value \'" + str + "\'.";
54     return -1;
55     }
56 if (*val < min || *val > max)
57     {
58     errorStr = "Value \'" + str + "\' out of range.";
59     return -1;
60     }
61 return 0;
62 }
63 //-----------------------------------------------------------------------------
64 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
65 {
66 int p;
67 PARAM_VALUE pv;
68 vector<PARAM_VALUE>::const_iterator pvi;
69 ///////////////////////////
70 pv.param = "Port";
71 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
72 if (pvi == s.moduleParams.end())
73     {
74     errorStr = "Parameter \'Port\' not found.";
75     printfd(__FILE__, "Parameter 'Port' not found\n");
76     return -1;
77     }
78 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
79     {
80     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
81     printfd(__FILE__, "%s\n", errorStr.c_str());
82     return -1;
83     }
84 port = p;
85
86 return 0;
87 }
88 //-----------------------------------------------------------------------------
89 uint16_t STG_CONFIG_SETTINGS::GetPort() const
90 {
91 return port;
92 }
93 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 PLUGIN * GetPlugin()
97 {
98 return stgc.GetPlugin();
99 }
100 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
103 const std::string STG_CONFIG::GetVersion() const
104 {
105 return "Stg configurator v.0.08";
106 }
107 //-----------------------------------------------------------------------------
108 STG_CONFIG::STG_CONFIG()
109     : nonstop(false),
110       isRunning(false),
111       users(NULL),
112       admins(NULL),
113       tariffs(NULL),
114       store(NULL),
115       stgSettings(NULL)
116 {
117 }
118 //-----------------------------------------------------------------------------
119 void STG_CONFIG::SetUsers(USERS * u)
120 {
121 users = u;
122 }
123 //-----------------------------------------------------------------------------
124 void STG_CONFIG::SetTariffs(TARIFFS * t)
125 {
126 tariffs = t;
127 }
128 //-----------------------------------------------------------------------------
129 void STG_CONFIG::SetAdmins(ADMINS * a)
130 {
131 admins = a;
132 }
133 //-----------------------------------------------------------------------------
134 void STG_CONFIG::SetStore(STORE * s)
135 {
136 store = s;
137 }
138 //-----------------------------------------------------------------------------
139 void STG_CONFIG::SetStgSettings(const SETTINGS * s)
140 {
141 stgSettings = s;
142 }
143 //-----------------------------------------------------------------------------
144 void STG_CONFIG::SetSettings(const MODULE_SETTINGS & s)
145 {
146 settings = s;
147 }
148 //-----------------------------------------------------------------------------
149 int STG_CONFIG::ParseSettings()
150 {
151 int ret = stgConfigSettings.ParseSettings(settings);
152 if (ret)
153     errorStr = stgConfigSettings.GetStrError();
154 return ret;
155 }
156 //-----------------------------------------------------------------------------
157 const std::string & STG_CONFIG::GetStrError() const
158 {
159 return errorStr;
160 }
161 //-----------------------------------------------------------------------------
162 int STG_CONFIG::Start()
163 {
164 if (isRunning)
165     return 0;
166
167 nonstop = true;
168
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);
175
176 if (config.Prepare())
177     {
178     errorStr = config.GetStrError();
179     return -1;
180     }
181
182 if (pthread_create(&thread, NULL, Run, this))
183     {
184     errorStr = "Cannot create thread.";
185     printfd(__FILE__, "Cannot create thread\n");
186     return -1;
187     }
188 errorStr = "";
189 return 0;
190 }
191 //-----------------------------------------------------------------------------
192 int STG_CONFIG::Stop()
193 {
194 if (!isRunning)
195     return 0;
196
197 config.Stop();
198
199 //5 seconds to thread stops itself
200 int i;
201 for (i = 0; i < 25; i++)
202     {
203     if (!isRunning)
204         break;
205
206     usleep(200000);
207     }
208
209 //after 5 seconds waiting thread still running. now killing it
210 if (isRunning)
211     {
212     //TODO pthread_cancel()
213     if (pthread_kill(thread, SIGINT))
214         {
215         errorStr = "Cannot kill thread.";
216         printfd(__FILE__, "Cannot kill thread\n");
217         return -1;
218         }
219     printfd(__FILE__, "STG_CONFIG killed\n");
220     }
221
222 return 0;
223 }
224 //-----------------------------------------------------------------------------
225 bool STG_CONFIG::IsRunning()
226 {
227 return isRunning;
228 }
229 //-----------------------------------------------------------------------------
230 void * STG_CONFIG::Run(void * d)
231 {
232 STG_CONFIG * stgConf = (STG_CONFIG *)d;
233 stgConf->isRunning = true;
234
235 stgConf->config.Run(&stgConf->config);
236
237 stgConf->isRunning = false;
238 return NULL;
239 }
240 //-----------------------------------------------------------------------------
241 uint16_t STG_CONFIG::GetStartPosition() const
242 {
243 return 220;
244 }
245 //-----------------------------------------------------------------------------
246 uint16_t STG_CONFIG::GetStopPosition() const
247 {
248 return 220;
249 }
250 //-----------------------------------------------------------------------------
251
252