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