8 #include "../../../tariffs.h"
9 #include "../../../admins.h"
10 #include "../../../users.h"
12 class STGCONFIG_CREATOR
15 STG_CONFIG * stgconfig;
19 : stgconfig(new STG_CONFIG())
27 STG_CONFIG * GetPlugin()
33 STGCONFIG_CREATOR stgc;
35 BASE_PLUGIN * GetPlugin()
37 return stgc.GetPlugin();
40 STG_CONFIG_SETTINGS::STG_CONFIG_SETTINGS()
45 const string& STG_CONFIG_SETTINGS::GetStrError() const
50 int STG_CONFIG_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
52 if (str2x(str.c_str(), *val))
54 errorStr = "Incorrect value \'" + str + "\'.";
57 if (*val < min || *val > max)
59 errorStr = "Value \'" + str + "\' out of range.";
65 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
69 vector<PARAM_VALUE>::const_iterator pvi;
70 ///////////////////////////
72 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
73 if (pvi == s.moduleParams.end())
75 errorStr = "Parameter \'Port\' not found.";
76 printfd(__FILE__, "Parameter 'Port' not found\n");
79 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
81 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
82 printfd(__FILE__, "%s\n", errorStr.c_str());
90 uint16_t STG_CONFIG_SETTINGS::GetPort()
95 STG_CONFIG::STG_CONFIG()
101 string STG_CONFIG::GetVersion() const
103 return "Stg configurator v.2.00";
106 int STG_CONFIG::ParseSettings()
108 int ret = stgConfigSettings.ParseSettings(settings);
110 errorStr = stgConfigSettings.GetStrError();
114 int STG_CONFIG::Start()
119 if (PrepareNetwork())
124 config.SetPort(stgConfigSettings.GetPort());
125 config.SetAdmins(admins);
126 config.SetUsers(users);
127 config.SetTariffs(tariffs);
128 config.SetStgSettings(stgSettings);
129 config.SetStore(store);
131 if (config.Prepare())
133 errorStr = config.GetStrError();
137 if (pthread_create(&thread, NULL, Run, this))
139 errorStr = "Cannot create thread.";
140 printfd(__FILE__, "Cannot create thread\n");
148 int STG_CONFIG::Stop()
157 //5 seconds to thread stops itself
159 for (i = 0; i < 25 && !stopped; i++)
164 //after 5 seconds waiting thread still running. now killing it
167 //TODO pthread_cancel()
168 if (pthread_kill(thread, SIGINT))
170 errorStr = "Cannot kill thread.";
171 printfd(__FILE__, "Cannot kill thread\n");
172 return FinalizeNetwork();
174 printfd(__FILE__, "STG_CONFIG killed\n");
177 return FinalizeNetwork();
180 void * STG_CONFIG::Run(void * d)
182 STG_CONFIG * stgConf = static_cast<STG_CONFIG *>(d);
183 stgConf->running = true;
187 stgConf->stopped = true;
191 uint16_t STG_CONFIG::GetStartPosition() const
196 uint16_t STG_CONFIG::GetStopPosition() const
201 bool PrepareNetwork()
203 struct sockaddr_in local;
205 local.sin_family = AF_INET;
206 local.sin_port = htons(port);
207 local.sin_addr.s_addr = INADDR_ANY;
209 sd = socket(AF_INET, SOCK_STREAM, 0);
212 errorStr = "Error creating socket: '";
213 errorStr += strerror(errno);
218 if (bind(sd, static_cast<struct sockaddr *>(&local), sizeof(local)) < 0)
220 errorStr = "Error binding socket: '";
221 errorStr += strerror(errno);
229 bool FinalizeNetwork()
233 errorStr = "Error closing socket: '";
234 errorStr += strerror(errno);
241 void STG_CONFIG::RealRun()
243 if (listen(sd, 64) < 0)
245 errorStr = "Error listening socket: '";
246 errorStr += strerror(errno);
263 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
275 if (FD_ISSET(sd, &rfds))
281 // Reorder: right part is done
282 std::list<ConnectionThread *>::iterator done(
286 std::not1(std::mem_fun(&ConnectionThread::isDone))
295 std::erase(done, connections.end());
301 void STG_CONFIG::AcceptConnection()
303 struct sockaddr_in remoteAddr;
304 socklen_t len = sizeof(struct sockaddr_in);
305 int rsd = accept(sd, &remoteAddr, &len);
312 connections.push_back(new ConnectionThread(this, rsd, remoteAddr, users, admins, tariffs, store, stgSettings));