]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
Added forgotten 'break'.
[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 static 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 std::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 = static_cast<uint16_t>(p);
40
41 return 0;
42 }
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 PLUGIN * GetPlugin()
47 {
48 return stgc.GetPlugin();
49 }
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 std::string STG_CONFIG::GetVersion() const
54 {
55 return "Stg configurator v.0.08";
56 }
57 //-----------------------------------------------------------------------------
58 STG_CONFIG::STG_CONFIG()
59     : errorStr(),
60       stgConfigSettings(),
61       thread(),
62       nonstop(false),
63       isRunning(false),
64       logger(GetPluginLogger(GetStgLogger(), "conf_sg")),
65       config(logger),
66       users(NULL),
67       admins(NULL),
68       tariffs(NULL),
69       store(NULL),
70       settings(),
71       stgSettings(NULL)
72 {
73 }
74 //-----------------------------------------------------------------------------
75 int STG_CONFIG::ParseSettings()
76 {
77 int ret = stgConfigSettings.ParseSettings(settings);
78 if (ret)
79     errorStr = stgConfigSettings.GetStrError();
80 return ret;
81 }
82 //-----------------------------------------------------------------------------
83 int STG_CONFIG::Start()
84 {
85 if (isRunning)
86     return 0;
87
88 nonstop = true;
89
90 config.SetPort(stgConfigSettings.GetPort());
91 config.SetAdmins(admins);
92 config.SetUsers(users);
93 config.SetTariffs(tariffs);
94 config.SetStgSettings(stgSettings);
95 config.SetStore(store);
96
97 if (config.Prepare())
98     {
99     errorStr = config.GetStrError();
100     return -1;
101     }
102
103 if (pthread_create(&thread, NULL, Run, this))
104     {
105     errorStr = "Cannot create thread.";
106     printfd(__FILE__, "Cannot create thread\n");
107     logger("Cannot create thread.");
108     return -1;
109     }
110 errorStr = "";
111 return 0;
112 }
113 //-----------------------------------------------------------------------------
114 int STG_CONFIG::Stop()
115 {
116 if (!isRunning)
117     return 0;
118
119 config.Stop();
120
121 //5 seconds to thread stops itself
122 int i;
123 for (i = 0; i < 25; i++)
124     {
125     if (!isRunning)
126         break;
127
128     struct timespec ts = {0, 200000000};
129     nanosleep(&ts, NULL);
130     }
131
132 if (isRunning)
133     return -1;
134
135 return 0;
136 }
137 //-----------------------------------------------------------------------------
138 void * STG_CONFIG::Run(void * d)
139 {
140 sigset_t signalSet;
141 sigfillset(&signalSet);
142 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
143
144 STG_CONFIG * stgConf = (STG_CONFIG *)d;
145 stgConf->isRunning = true;
146
147 stgConf->config.Run();
148
149 stgConf->isRunning = false;
150 return NULL;
151 }
152 //-----------------------------------------------------------------------------