7 #include "stg/plugin_creator.h"
9 #include "../../../tariffs.h"
10 #include "../../../admins.h"
11 #include "../../../users.h"
13 PLUGIN_CREATOR<STG_CONFIG> stgc;
15 BASE_PLUGIN * GetPlugin()
17 return stgc.GetPlugin();
20 STG_CONFIG_SETTINGS::STG_CONFIG_SETTINGS()
25 const string& STG_CONFIG_SETTINGS::GetStrError() const
30 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
34 vector<PARAM_VALUE>::const_iterator pvi;
35 ///////////////////////////
37 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
38 if (pvi == s.moduleParams.end())
40 errorStr = "Parameter \'Port\' not found.";
41 printfd(__FILE__, "Parameter 'Port' not found\n");
44 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
46 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
47 printfd(__FILE__, "%s\n", errorStr.c_str());
55 uint16_t STG_CONFIG_SETTINGS::GetPort()
60 STG_CONFIG::STG_CONFIG()
66 string STG_CONFIG::GetVersion() const
68 return "Stg configurator v.2.00";
71 int STG_CONFIG::ParseSettings()
73 int ret = stgConfigSettings.ParseSettings(settings);
75 errorStr = stgConfigSettings.GetStrError();
79 int STG_CONFIG::Start()
89 config.SetPort(stgConfigSettings.GetPort());
90 config.SetAdmins(admins);
91 config.SetUsers(users);
92 config.SetTariffs(tariffs);
93 config.SetStgSettings(stgSettings);
94 config.SetStore(store);
98 errorStr = config.GetStrError();
102 if (pthread_create(&thread, NULL, Run, this))
104 errorStr = "Cannot create thread.";
105 printfd(__FILE__, "Cannot create thread\n");
113 int STG_CONFIG::Stop()
122 //5 seconds to thread stops itself
124 for (i = 0; i < 25 && !stopped; i++)
129 //after 5 seconds waiting thread still running. now killing it
132 //TODO pthread_cancel()
133 if (pthread_kill(thread, SIGINT))
135 errorStr = "Cannot kill thread.";
136 printfd(__FILE__, "Cannot kill thread\n");
137 return FinalizeNetwork();
139 printfd(__FILE__, "STG_CONFIG killed\n");
142 return FinalizeNetwork();
145 void * STG_CONFIG::Run(void * d)
147 STG_CONFIG * stgConf = static_cast<STG_CONFIG *>(d);
148 stgConf->running = true;
152 stgConf->stopped = true;
156 uint16_t STG_CONFIG::GetStartPosition() const
161 uint16_t STG_CONFIG::GetStopPosition() const
166 bool PrepareNetwork()
168 struct sockaddr_in local;
170 local.sin_family = AF_INET;
171 local.sin_port = htons(port);
172 local.sin_addr.s_addr = INADDR_ANY;
174 sd = socket(AF_INET, SOCK_STREAM, 0);
177 errorStr = "Error creating socket: '";
178 errorStr += strerror(errno);
183 if (bind(sd, static_cast<struct sockaddr *>(&local), sizeof(local)) < 0)
185 errorStr = "Error binding socket: '";
186 errorStr += strerror(errno);
194 bool FinalizeNetwork()
198 errorStr = "Error closing socket: '";
199 errorStr += strerror(errno);
206 void STG_CONFIG::RealRun()
208 if (listen(sd, 64) < 0)
210 errorStr = "Error listening socket: '";
211 errorStr += strerror(errno);
228 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
240 if (FD_ISSET(sd, &rfds))
246 // Reorder: right part is done
247 std::list<ConnectionThread *>::iterator done(
251 std::not1(std::mem_fun(&ConnectionThread::isDone))
260 std::erase(done, connections.end());
266 void STG_CONFIG::AcceptConnection()
268 struct sockaddr_in remoteAddr;
269 socklen_t len = sizeof(struct sockaddr_in);
270 int rsd = accept(sd, &remoteAddr, &len);
277 connections.push_back(new ConnectionThread(this, rsd, remoteAddr, users, admins, tariffs, store, stgSettings));