]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / stgconfig.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #include "stgconfig.h"
22
23 #include "stg/plugin_creator.h"
24 #include "stg/common.h"
25
26 #include <algorithm>
27 #include <csignal>
28 #include <cstring>
29 #include <cerrno>
30
31 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 static PLUGIN_CREATOR<STG_CONFIG> stgc;
35 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 bool STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
39 {
40     PARAM_VALUE pv;
41     std::vector<PARAM_VALUE>::const_iterator pvi;
42     ///////////////////////////
43     pv.param = "Port";
44     pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
45     if (pvi == s.moduleParams.end() || pvi->value.empty())
46         {
47         errorStr = "Parameter \'Port\' is not found.";
48         printfd(__FILE__, "%s\n", errorStr.c_str());
49         return false;
50         }
51     int p;
52     if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
53         {
54         errorStr = "Parameter \'Port\' should be an integral value in range (2, 65535). Actual value: '" + pvi->value[0] + "'.";
55         printfd(__FILE__, "%s\n", errorStr.c_str());
56         return false;
57         }
58     m_port = static_cast<uint16_t>(p);
59
60     pv.param = "BindAddress";
61     pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
62     if (pvi != s.moduleParams.end() && !pvi->value.empty())
63         m_bindAddress = pvi->value[0];
64
65     return true;
66 }
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 extern "C" PLUGIN * GetPlugin()
71 {
72 return stgc.GetPlugin();
73 }
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 STG_CONFIG::STG_CONFIG()
78     : nonstop(false),
79       isRunning(false),
80       logger(GetPluginLogger(GetStgLogger(), "conf_sg")),
81       config(logger)
82 {
83 }
84 //-----------------------------------------------------------------------------
85 int STG_CONFIG::ParseSettings()
86 {
87     if (stgConfigSettings.ParseSettings(settings))
88         return 0;
89     errorStr = stgConfigSettings.GetStrError();
90     return -1;
91 }
92 //-----------------------------------------------------------------------------
93 int STG_CONFIG::Start()
94 {
95     if (isRunning)
96         return 0;
97
98     nonstop = true;
99
100     config.SetPort(stgConfigSettings.GetPort());
101     config.SetBindAddress(stgConfigSettings.GetBindAddress());
102
103     if (config.Prepare())
104     {
105         errorStr = config.GetStrError();
106         return -1;
107     }
108
109     if (pthread_create(&thread, NULL, Run, this))
110     {
111         errorStr = std::string("Cannot create thread: '") + strerror(errno) + "'.";
112         printfd(__FILE__, "%s\n", errorStr.c_str());
113         logger(errorStr);
114         return -1;
115     }
116
117     return 0;
118 }
119 //-----------------------------------------------------------------------------
120 int STG_CONFIG::Stop()
121 {
122     if (!isRunning)
123         return 0;
124
125     config.Stop();
126
127     //5 seconds to thread stops itself
128     for (size_t i = 0; i < 25; ++i)
129     {
130         if (!isRunning)
131             break;
132
133         struct timespec ts = {0, 200000000};
134         nanosleep(&ts, NULL);
135     }
136
137     if (isRunning)
138         return -1;
139
140     return 0;
141 }
142 //-----------------------------------------------------------------------------
143 void * STG_CONFIG::Run(void * d)
144 {
145     sigset_t signalSet;
146     sigfillset(&signalSet);
147     pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
148
149     STG_CONFIG & stgConf = *static_cast<STG_CONFIG *>(d);
150     stgConf.isRunning = true;
151
152     stgConf.config.Run();
153
154     stgConf.isRunning = false;
155
156     return NULL;
157 }