]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
7a0bf323f9425c4a433913e4f88fa7abee33da28
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / stgconfig.cpp
1 #include "stgconfig.h"
2
3 #include "stg/plugin_creator.h"
4 #include "stg/common.h"
5
6 #include <algorithm>
7 #include <csignal>
8
9 #include <unistd.h>
10
11 //-----------------------------------------------------------------------------
12 //-----------------------------------------------------------------------------
13 //-----------------------------------------------------------------------------
14 static PLUGIN_CREATOR<STG_CONFIG> stgc;
15 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
19 {
20 int p;
21 PARAM_VALUE pv;
22 std::vector<PARAM_VALUE>::const_iterator pvi;
23 ///////////////////////////
24 pv.param = "Port";
25 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
26 if (pvi == s.moduleParams.end())
27     {
28     errorStr = "Parameter \'Port\' not found.";
29     printfd(__FILE__, "Parameter 'Port' not found\n");
30     return -1;
31     }
32 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
33     {
34     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
35     printfd(__FILE__, "%s\n", errorStr.c_str());
36     return -1;
37     }
38 port = static_cast<uint16_t>(p);
39
40 return 0;
41 }
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 PLUGIN * GetPlugin()
46 {
47 return stgc.GetPlugin();
48 }
49 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 std::string STG_CONFIG::GetVersion() const
53 {
54 return "Stg configurator v.0.08";
55 }
56 //-----------------------------------------------------------------------------
57 STG_CONFIG::STG_CONFIG()
58     : errorStr(),
59       stgConfigSettings(),
60       thread(),
61       nonstop(false),
62       isRunning(false),
63       logger(GetPluginLogger(GetStgLogger(), "conf_sg")),
64       config(logger),
65       settings()
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
86 if (config.Prepare())
87     {
88     errorStr = config.GetStrError();
89     return -1;
90     }
91
92 if (pthread_create(&thread, NULL, Run, this))
93     {
94     errorStr = "Cannot create thread.";
95     printfd(__FILE__, "Cannot create thread\n");
96     logger("Cannot create thread.");
97     return -1;
98     }
99 errorStr = "";
100 return 0;
101 }
102 //-----------------------------------------------------------------------------
103 int STG_CONFIG::Stop()
104 {
105 if (!isRunning)
106     return 0;
107
108 config.Stop();
109
110 //5 seconds to thread stops itself
111 for (int i = 0; i < 25; i++)
112     {
113     if (!isRunning)
114         break;
115
116     struct timespec ts = {0, 200000000};
117     nanosleep(&ts, NULL);
118     }
119
120 if (isRunning)
121     return -1;
122
123 return 0;
124 }
125 //-----------------------------------------------------------------------------
126 void * STG_CONFIG::Run(void * d)
127 {
128 sigset_t signalSet;
129 sigfillset(&signalSet);
130 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
131
132 STG_CONFIG * stgConf = static_cast<STG_CONFIG *>(d);
133 stgConf->isRunning = true;
134
135 stgConf->config.Run();
136
137 stgConf->isRunning = false;
138 return NULL;
139 }
140 //-----------------------------------------------------------------------------