]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
Introduced logger for plugins.
[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     : 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     return -1;
108     }
109 errorStr = "";
110 return 0;
111 }
112 //-----------------------------------------------------------------------------
113 int STG_CONFIG::Stop()
114 {
115 if (!isRunning)
116     return 0;
117
118 config.Stop();
119
120 //5 seconds to thread stops itself
121 int i;
122 for (i = 0; i < 25; i++)
123     {
124     if (!isRunning)
125         break;
126
127     struct timespec ts = {0, 200000000};
128     nanosleep(&ts, NULL);
129     }
130
131 if (isRunning)
132     return -1;
133
134 return 0;
135 }
136 //-----------------------------------------------------------------------------
137 void * STG_CONFIG::Run(void * d)
138 {
139 sigset_t signalSet;
140 sigfillset(&signalSet);
141 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
142
143 STG_CONFIG * stgConf = (STG_CONFIG *)d;
144 stgConf->isRunning = true;
145
146 stgConf->config.Run();
147
148 stgConf->isRunning = false;
149 return NULL;
150 }
151 //-----------------------------------------------------------------------------