]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/xrconfig/xrconfig.cpp
USER_STAT refactoring.
[stg.git] / projects / stargazer / plugins / configuration / xrconfig / xrconfig.cpp
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <signal.h>
4
5 #include "stg/plugin_creator.h"
6 #include "xrconfig.h"
7 #include "../../../tariff2.h"
8 #include "../../../admins.h"
9 #include "../../../users.h"
10
11 //-----------------------------------------------------------------------------
12 //-----------------------------------------------------------------------------
13 //-----------------------------------------------------------------------------
14 PLUGIN_CREATOR<XR_CONFIG> xrc;
15 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 BASE_PLUGIN * GetPlugin()
19 {
20 return xrc.GetPlugin();
21 }
22 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
25 XR_CONFIG_SETTINGS::XR_CONFIG_SETTINGS()
26     : port(0)
27 {
28 }
29 //-----------------------------------------------------------------------------
30 const string & XR_CONFIG_SETTINGS::GetStrError() const
31 {
32 return errorStr;
33 }
34 //-----------------------------------------------------------------------------
35 int XR_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
36 {
37
38 return 0;
39 }
40 //-----------------------------------------------------------------------------
41 uint16_t XR_CONFIG_SETTINGS::GetPort()
42 {
43 return port;
44 }
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 const string XR_CONFIG::GetVersion() const
49 {
50 return "XR_configurator v.0.01";
51 }
52 //-----------------------------------------------------------------------------
53 XR_CONFIG::XR_CONFIG()
54 {
55 isRunning = false;
56 nonstop = false;
57 }
58 //-----------------------------------------------------------------------------
59 void XR_CONFIG::SetUsers(USERS * u)
60 {
61 users = u;
62 }
63 //-----------------------------------------------------------------------------
64 void XR_CONFIG::SetTariffs(TARIFFS * t)
65 {
66 tariffs = t;
67 }
68 //-----------------------------------------------------------------------------
69 void XR_CONFIG::SetAdmins(ADMINS * a)
70 {
71 admins = a;
72 }
73 //-----------------------------------------------------------------------------
74 void XR_CONFIG::SetStore(BASE_STORE * s)
75 {
76 store = s;
77 }
78 //-----------------------------------------------------------------------------
79 void XR_CONFIG::SetStgSettings(const SETTINGS * s)
80 {
81 stgSettings = s;
82 }
83 //-----------------------------------------------------------------------------
84 void XR_CONFIG::SetSettings(const MODULE_SETTINGS & s)
85 {
86 settings = s;
87 }
88 //-----------------------------------------------------------------------------
89 int XR_CONFIG::ParseSettings()
90 {
91 int ret = xrConfigSettings.ParseSettings(settings);
92 if (ret)
93     errorStr = xrConfigSettings.GetStrError();
94 return ret;
95 }
96 //-----------------------------------------------------------------------------
97 const string & XR_CONFIG::GetStrError() const
98 {
99 return errorStr;
100 }
101 //-----------------------------------------------------------------------------
102 int XR_CONFIG::Start()
103 {
104 if (isRunning)
105     return 0;
106
107 nonstop = true;
108
109 config.SetPort(xrConfigSettings.GetPort());
110 config.SetAdmins(admins);
111 config.SetUsers(users);
112 config.SetTariffs(tariffs);
113 config.SetStgSettings(stgSettings);
114 config.SetStore(store);
115
116 if (config.Prepare())
117     {
118     errorStr = config.GetStrError();
119     return -1;
120     }
121
122 if (pthread_create(&thread, NULL, Run, this))
123     {
124     errorStr = "Cannot create thread.";
125     printfd(__FILE__, "Cannot create thread\n");
126     return -1;
127     }
128 errorStr = "";
129 return 0;
130 }
131 //-----------------------------------------------------------------------------
132 int XR_CONFIG::Stop()
133 {
134 if (!isRunning)
135     return 0;
136
137 config.Stop();
138
139 //5 seconds to thread stops itself
140 int i;
141 for (i = 0; i < 25; i++)
142     {
143     if (!isRunning)
144         break;
145
146     stgUsleep(200000);
147     }
148
149 //after 5 seconds waiting thread still running. now killing it
150 if (isRunning)
151     {
152     //TODO pthread_cancel()
153     if (pthread_kill(thread, SIGINT))
154         {
155         errorStr = "Cannot kill thread.";
156         printfd(__FILE__, "Cannot kill thread\n");
157         return -1;
158         }
159     printfd(__FILE__, "XR_CONFIG killed\n");
160     }
161
162 return 0;
163 }
164 //-----------------------------------------------------------------------------
165 bool XR_CONFIG::IsRunning()
166 {
167 return isRunning;
168 }
169 //-----------------------------------------------------------------------------
170 void * XR_CONFIG::Run(void * d)
171 {
172 XR_CONFIG * stgConf = (XR_CONFIG *)d;
173 stgConf->isRunning = true;
174
175 stgConf->config.Run(&stgConf->config);
176
177 stgConf->isRunning = false;
178 return NULL;
179 }
180 //-----------------------------------------------------------------------------
181 uint16_t XR_CONFIG::GetStartPosition() const
182 {
183 return 20;
184 }
185 //-----------------------------------------------------------------------------
186 uint16_t XR_CONFIG::GetStopPosition() const
187 {
188 return 20;
189 }
190 //-----------------------------------------------------------------------------
191 int XR_CONFIG::SetUserCash(const string & admLogin, const string & usrLogin, double cash) const
192 {
193 return 0;
194 }
195 //-----------------------------------------------------------------------------
196