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