]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
06e915c37e873e11461453f4a02f836405f4d74f
[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 "stg/tariffs.h"
8 #include "stg/admins.h"
9 #include "stg/users.h"
10 #include "stgconfig.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::ParseSettings(const MODULE_SETTINGS & s)
50 {
51 int p;
52 PARAM_VALUE pv;
53 vector<PARAM_VALUE>::const_iterator pvi;
54 ///////////////////////////
55 pv.param = "Port";
56 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
57 if (pvi == s.moduleParams.end())
58     {
59     errorStr = "Parameter \'Port\' not found.";
60     printfd(__FILE__, "Parameter 'Port' not found\n");
61     return -1;
62     }
63 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
64     {
65     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
66     printfd(__FILE__, "%s\n", errorStr.c_str());
67     return -1;
68     }
69 port = p;
70
71 return 0;
72 }
73 //-----------------------------------------------------------------------------
74 uint16_t STG_CONFIG_SETTINGS::GetPort() const
75 {
76 return port;
77 }
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 PLUGIN * GetPlugin()
82 {
83 return stgc.GetPlugin();
84 }
85 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 const std::string STG_CONFIG::GetVersion() const
89 {
90 return "Stg configurator v.0.08";
91 }
92 //-----------------------------------------------------------------------------
93 STG_CONFIG::STG_CONFIG()
94     : nonstop(false),
95       isRunning(false),
96       users(NULL),
97       admins(NULL),
98       tariffs(NULL),
99       store(NULL),
100       stgSettings(NULL)
101 {
102 }
103 //-----------------------------------------------------------------------------
104 void STG_CONFIG::SetUsers(USERS * u)
105 {
106 users = u;
107 }
108 //-----------------------------------------------------------------------------
109 void STG_CONFIG::SetTariffs(TARIFFS * t)
110 {
111 tariffs = t;
112 }
113 //-----------------------------------------------------------------------------
114 void STG_CONFIG::SetAdmins(ADMINS * a)
115 {
116 admins = a;
117 }
118 //-----------------------------------------------------------------------------
119 void STG_CONFIG::SetStore(STORE * s)
120 {
121 store = s;
122 }
123 //-----------------------------------------------------------------------------
124 void STG_CONFIG::SetStgSettings(const SETTINGS * s)
125 {
126 stgSettings = s;
127 }
128 //-----------------------------------------------------------------------------
129 void STG_CONFIG::SetSettings(const MODULE_SETTINGS & s)
130 {
131 settings = s;
132 }
133 //-----------------------------------------------------------------------------
134 int STG_CONFIG::ParseSettings()
135 {
136 int ret = stgConfigSettings.ParseSettings(settings);
137 if (ret)
138     errorStr = stgConfigSettings.GetStrError();
139 return ret;
140 }
141 //-----------------------------------------------------------------------------
142 const std::string & STG_CONFIG::GetStrError() const
143 {
144 return errorStr;
145 }
146 //-----------------------------------------------------------------------------
147 int STG_CONFIG::Start()
148 {
149 if (isRunning)
150     return 0;
151
152 nonstop = true;
153
154 config.SetPort(stgConfigSettings.GetPort());
155 config.SetAdmins(admins);
156 config.SetUsers(users);
157 config.SetTariffs(tariffs);
158 config.SetStgSettings(stgSettings);
159 config.SetStore(store);
160
161 if (config.Prepare())
162     {
163     errorStr = config.GetStrError();
164     return -1;
165     }
166
167 if (pthread_create(&thread, NULL, Run, this))
168     {
169     errorStr = "Cannot create thread.";
170     printfd(__FILE__, "Cannot create thread\n");
171     return -1;
172     }
173 errorStr = "";
174 return 0;
175 }
176 //-----------------------------------------------------------------------------
177 int STG_CONFIG::Stop()
178 {
179 if (!isRunning)
180     return 0;
181
182 config.Stop();
183
184 //5 seconds to thread stops itself
185 int i;
186 for (i = 0; i < 25; i++)
187     {
188     if (!isRunning)
189         break;
190
191     usleep(200000);
192     }
193
194 //after 5 seconds waiting thread still running. now killing it
195 if (isRunning)
196     {
197     //TODO pthread_cancel()
198     if (pthread_kill(thread, SIGINT))
199         {
200         errorStr = "Cannot kill thread.";
201         printfd(__FILE__, "Cannot kill thread\n");
202         return -1;
203         }
204     printfd(__FILE__, "STG_CONFIG killed\n");
205     }
206
207 return 0;
208 }
209 //-----------------------------------------------------------------------------
210 bool STG_CONFIG::IsRunning()
211 {
212 return isRunning;
213 }
214 //-----------------------------------------------------------------------------
215 void * STG_CONFIG::Run(void * d)
216 {
217 STG_CONFIG * stgConf = (STG_CONFIG *)d;
218 stgConf->isRunning = true;
219
220 stgConf->config.Run(&stgConf->config);
221
222 stgConf->isRunning = false;
223 return NULL;
224 }
225 //-----------------------------------------------------------------------------
226 uint16_t STG_CONFIG::GetStartPosition() const
227 {
228 return 220;
229 }
230 //-----------------------------------------------------------------------------
231 uint16_t STG_CONFIG::GetStopPosition() const
232 {
233 return 220;
234 }
235 //-----------------------------------------------------------------------------
236
237