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