]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/xrconfig/xrconfig.cpp
856ae88689679c04baef6d2fae7e55b9e942e4dd
[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::ParseSettings(const MODULE_SETTINGS & s)
48 {
49
50 return 0;
51 }
52 //-----------------------------------------------------------------------------
53 uint16_t XR_CONFIG_SETTINGS::GetPort()
54 {
55 return port;
56 }
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 BASE_PLUGIN * GetPlugin()
61 {
62 return xrc.GetPlugin();
63 }
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 const string XR_CONFIG::GetVersion() const
68 {
69 return "XR_configurator v.0.01";
70 }
71 //-----------------------------------------------------------------------------
72 XR_CONFIG::XR_CONFIG()
73 {
74 isRunning = false;
75 nonstop = false;
76 }
77 //-----------------------------------------------------------------------------
78 void XR_CONFIG::SetUsers(USERS * u)
79 {
80 users = u;
81 }
82 //-----------------------------------------------------------------------------
83 void XR_CONFIG::SetTariffs(TARIFFS * t)
84 {
85 tariffs = t;
86 }
87 //-----------------------------------------------------------------------------
88 void XR_CONFIG::SetAdmins(ADMINS * a)
89 {
90 admins = a;
91 }
92 //-----------------------------------------------------------------------------
93 void XR_CONFIG::SetStore(BASE_STORE * s)
94 {
95 store = s;
96 }
97 //-----------------------------------------------------------------------------
98 void XR_CONFIG::SetStgSettings(const SETTINGS * s)
99 {
100 stgSettings = s;
101 }
102 //-----------------------------------------------------------------------------
103 void XR_CONFIG::SetSettings(const MODULE_SETTINGS & s)
104 {
105 settings = s;
106 }
107 //-----------------------------------------------------------------------------
108 int XR_CONFIG::ParseSettings()
109 {
110 int ret = xrConfigSettings.ParseSettings(settings);
111 if (ret)
112     errorStr = xrConfigSettings.GetStrError();
113 return ret;
114 }
115 //-----------------------------------------------------------------------------
116 const string & XR_CONFIG::GetStrError() const
117 {
118 return errorStr;
119 }
120 //-----------------------------------------------------------------------------
121 int XR_CONFIG::Start()
122 {
123 if (isRunning)
124     return 0;
125
126 nonstop = true;
127
128 config.SetPort(xrConfigSettings.GetPort());
129 config.SetAdmins(admins);
130 config.SetUsers(users);
131 config.SetTariffs(tariffs);
132 config.SetStgSettings(stgSettings);
133 config.SetStore(store);
134
135 if (config.Prepare())
136     {
137     errorStr = config.GetStrError();
138     return -1;
139     }
140
141 if (pthread_create(&thread, NULL, Run, this))
142     {
143     errorStr = "Cannot create thread.";
144     printfd(__FILE__, "Cannot create thread\n");
145     return -1;
146     }
147 errorStr = "";
148 return 0;
149 }
150 //-----------------------------------------------------------------------------
151 int XR_CONFIG::Stop()
152 {
153 if (!isRunning)
154     return 0;
155
156 config.Stop();
157
158 //5 seconds to thread stops itself
159 int i;
160 for (i = 0; i < 25; i++)
161     {
162     if (!isRunning)
163         break;
164
165     stgUsleep(200000);
166     }
167
168 //after 5 seconds waiting thread still running. now killing it
169 if (isRunning)
170     {
171     //TODO pthread_cancel()
172     if (pthread_kill(thread, SIGINT))
173         {
174         errorStr = "Cannot kill thread.";
175         printfd(__FILE__, "Cannot kill thread\n");
176         return -1;
177         }
178     printfd(__FILE__, "XR_CONFIG killed\n");
179     }
180
181 return 0;
182 }
183 //-----------------------------------------------------------------------------
184 bool XR_CONFIG::IsRunning()
185 {
186 return isRunning;
187 }
188 //-----------------------------------------------------------------------------
189 void * XR_CONFIG::Run(void * d)
190 {
191 XR_CONFIG * stgConf = (XR_CONFIG *)d;
192 stgConf->isRunning = true;
193
194 stgConf->config.Run(&stgConf->config);
195
196 stgConf->isRunning = false;
197 return NULL;
198 }
199 //-----------------------------------------------------------------------------
200 uint16_t XR_CONFIG::GetStartPosition() const
201 {
202 return 221;
203 }
204 //-----------------------------------------------------------------------------
205 uint16_t XR_CONFIG::GetStopPosition() const
206 {
207 return 221;
208 }
209 //-----------------------------------------------------------------------------
210 int XR_CONFIG::SetUserCash(const string & admLogin, const string & usrLogin, double cash) const
211 {
212 return 0;
213 }
214 //-----------------------------------------------------------------------------
215