]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
19d68f98da37c4a446f52dc7c1d599ba323ff325
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / stgconfig.cpp
1 #include <unistd.h>
2
3 #include <csignal>
4 #include <algorithm>
5
6 #include "stg/tariffs.h"
7 #include "stg/admins.h"
8 #include "stg/users.h"
9 #include "stg/plugin_creator.h"
10 #include "stgconfig.h"
11
12 //-----------------------------------------------------------------------------
13 //-----------------------------------------------------------------------------
14 //-----------------------------------------------------------------------------
15 PLUGIN_CREATOR<STG_CONFIG> stgc;
16 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
19 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
20 {
21 int p;
22 PARAM_VALUE pv;
23 vector<PARAM_VALUE>::const_iterator pvi;
24 ///////////////////////////
25 pv.param = "Port";
26 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
27 if (pvi == s.moduleParams.end())
28     {
29     errorStr = "Parameter \'Port\' not found.";
30     printfd(__FILE__, "Parameter 'Port' not found\n");
31     return -1;
32     }
33 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
34     {
35     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
36     printfd(__FILE__, "%s\n", errorStr.c_str());
37     return -1;
38     }
39 port = p;
40
41 return 0;
42 }
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 PLUGIN * GetPlugin()
47 {
48 return stgc.GetPlugin();
49 }
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 const std::string STG_CONFIG::GetVersion() const
54 {
55 return "Stg configurator v.0.08";
56 }
57 //-----------------------------------------------------------------------------
58 STG_CONFIG::STG_CONFIG()
59     : nonstop(false),
60       isRunning(false),
61       users(NULL),
62       admins(NULL),
63       tariffs(NULL),
64       store(NULL),
65       stgSettings(NULL)
66 {
67 }
68 //-----------------------------------------------------------------------------
69 int STG_CONFIG::ParseSettings()
70 {
71 int ret = stgConfigSettings.ParseSettings(settings);
72 if (ret)
73     errorStr = stgConfigSettings.GetStrError();
74 return ret;
75 }
76 //-----------------------------------------------------------------------------
77 int STG_CONFIG::Start()
78 {
79 if (isRunning)
80     return 0;
81
82 nonstop = true;
83
84 config.SetPort(stgConfigSettings.GetPort());
85 config.SetAdmins(admins);
86 config.SetUsers(users);
87 config.SetTariffs(tariffs);
88 config.SetStgSettings(stgSettings);
89 config.SetStore(store);
90
91 if (config.Prepare())
92     {
93     errorStr = config.GetStrError();
94     return -1;
95     }
96
97 if (pthread_create(&thread, NULL, Run, this))
98     {
99     errorStr = "Cannot create thread.";
100     printfd(__FILE__, "Cannot create thread\n");
101     return -1;
102     }
103 errorStr = "";
104 return 0;
105 }
106 //-----------------------------------------------------------------------------
107 int STG_CONFIG::Stop()
108 {
109 if (!isRunning)
110     return 0;
111
112 config.Stop();
113
114 //5 seconds to thread stops itself
115 int i;
116 for (i = 0; i < 25; i++)
117     {
118     if (!isRunning)
119         break;
120
121     usleep(200000);
122     }
123
124 //after 5 seconds waiting thread still running. now killing it
125 if (isRunning)
126     {
127     //TODO pthread_cancel()
128     if (pthread_kill(thread, SIGINT))
129         {
130         errorStr = "Cannot kill thread.";
131         printfd(__FILE__, "Cannot kill thread\n");
132         return -1;
133         }
134     printfd(__FILE__, "STG_CONFIG killed\n");
135     }
136
137 return 0;
138 }
139 //-----------------------------------------------------------------------------
140 void * STG_CONFIG::Run(void * d)
141 {
142 STG_CONFIG * stgConf = (STG_CONFIG *)d;
143 stgConf->isRunning = true;
144
145 stgConf->config.Run();
146
147 stgConf->isRunning = false;
148 return NULL;
149 }
150 //-----------------------------------------------------------------------------