OSDN Git Service

Fixed chkconfig option
[ultramonkey-l7/ultramonkey-l7-v2.git] / parameter / parameter_impl.cpp
1 #include <vector>
2 #include <fstream>
3 #include <stdexcept>
4 #include "parameter_impl.h"
5 #include "logger_wrapper.h"
6
7 //#include "lexical_cast.h"
8 #include <boost/lexical_cast.hpp>
9
10 #ifndef PARAMETER_FILE
11 #define PARAMETER_FILE "/etc/l7vs/l7vs.cf"
12 #endif //PARAMETER_FILE
13
14 #define LINE_LENGTH     4096
15
16 #if !defined(LOGGER_PROCESS_VSD) && !defined(LOGGER_PROCESS_ADM) && !defined(LOGGER_PROCESS_SNM)
17 #define LOGGER_PROCESS_VSD
18 #endif
19
20 #ifdef  LOGGER_PROCESS_SNM
21 LOG_CATEGORY_TAG parameter_cat = LOG_CAT_SNMPAGENT_PARAMETER;
22 #elif   LOGGER_PROCESS_ADM
23 LOG_CATEGORY_TAG parameter_cat = LOG_CAT_L7VSADM_PARAMETER;
24 #else
25 LOG_CATEGORY_TAG parameter_cat = LOG_CAT_L7VSD_PARAMETER;
26 #endif
27
28 void    loadLoggerConf(){
29         l7vs::Logger::getInstance().loadConf();
30 }
31
32 /*!
33  * ParameterImpl class Constructor
34  */
35 l7vs::ParameterImpl::ParameterImpl()
36 {
37 }
38
39 /*!
40  * Initialize ParameterImpl class
41  */
42 bool    l7vs::ParameterImpl::init()
43 {
44         bool    retbool = true;
45         //clrear map objects
46         compTable.clear();
47         stringMap.clear();
48         intMap.clear();
49
50         //add member to component table
51         compTable[PARAM_COMP_ALL]       = component("all", NULL);
52         compTable[PARAM_COMP_NOCAT]     = component("nocategory", NULL);
53         compTable[PARAM_COMP_L7VSD]     = component("l7vsd", NULL);
54         compTable[PARAM_COMP_IOMUX]     = component("iomux", NULL);
55         compTable[PARAM_COMP_LSOCK]     = component("lsock", NULL);
56         compTable[PARAM_COMP_CONN]      = component("conn", NULL);
57         compTable[PARAM_COMP_DEST]      = component("dest", NULL);
58         compTable[PARAM_COMP_SERVICE]   = component("service", NULL);
59         compTable[PARAM_COMP_MODULE]    = component("module", NULL);
60         compTable[PARAM_COMP_REPLICATION]       = component("replication", NULL);
61         compTable[PARAM_COMP_LOGGER]    = component("logger", loadLoggerConf);
62         compTable[PARAM_COMP_L7VSADM]   = component("l7vsadm", NULL);
63         compTable[PARAM_COMP_SNMPAGENT] = component("snmpagent", NULL);
64
65         //read all parameters
66         retbool = rereadFile( PARAM_COMP_ALL );
67
68         return  retbool;
69 }
70
71 /*!
72  * checks that an argument is a numeric.
73  * @param[in]   const std::string
74  * @return              is numeric = true / not numeric = false
75  */
76 bool    l7vs::ParameterImpl::isNumeric(const std::string& val)
77 {
78         try {
79 //              l7vs::lexical_cast<int>(val);
80                 boost::lexical_cast<int>(val);
81         }
82 //      catch (std::exception& ex) {
83         catch (boost::bad_lexical_cast& ex) {
84                 return false;
85         }
86         return true;
87 }
88
89 /*!
90  * delete blank before and behind a sentence
91  * @param[in]   const std::string
92  * @return              std::string
93  */
94 std::string     l7vs::ParameterImpl::removebrank( const std::string& val )
95 {
96         std::string     str = val;
97         //remove brank(head of the sentence and end of the sentence)
98         std::string::iterator its;
99         its = str.begin();
100         while( its != str.end() ){
101                 if( ( ' ' == *its ) || ( '\t' == *its ) ){
102                         str.erase(its);
103                 }else break;
104         }
105         if( "" == str )return str;
106         //remove brank(head of the sentence and end of the sentence)
107         its = str.end() - 1;
108         while( its != str.begin() ){
109                 if( ( ' ' == *its ) || ( '\t' == *its ) ){
110                         str.erase(its);
111                         its--;
112                 }else break;
113         }
114         return str;
115 }
116
117 /*!
118  * Read Parameter from file
119  * @param[in]   void
120  * @return              success = true / fail = false
121  */
122 bool    l7vs::ParameterImpl::readParameterFile( const std::string& val )
123 {
124         bool    retval = true;
125         char    readbuf[LINE_LENGTH];
126
127         LOGGER_PUT_LOG_INFO( parameter_cat,1, "read parameter file : %s", val.c_str() );
128
129         std::vector<std::string>        paramlines;
130         try{
131                 paramlines.clear();
132                 preparse.clear();
133
134                 std::ifstream   ifs( val.c_str() );
135                 if( ifs.fail() ){
136                         return false;
137                 }
138                 while( !ifs.eof() ){
139                         memset( readbuf, 0, LINE_LENGTH );
140                         ifs.getline( readbuf, ( LINE_LENGTH - 1 ) );
141                         std::string     str = readbuf;
142                         str = removebrank( str );
143                         //remove coment line
144                         if( '#' == str[0] )continue;
145                         //remove brank
146                         if( !str.empty() )paramlines.push_back( str );
147                 }
148                 std::string     sectionname = compTable[PARAM_COMP_NOCAT].section;
149                 for( std::vector<std::string>::iterator it = paramlines.begin();
150                         it != paramlines.end(); it++ ){
151                         //pre-parse
152                         std::string str = *it;
153                         if( '[' == str[0] ){
154                                 std::string     tmpstr = str.substr( str.rfind("]")+1 );
155                                 tmpstr = removebrank( tmpstr.substr( 0, tmpstr.find( "#" ) ) );
156                                 if( !tmpstr.empty() )continue;
157                                 //section string validity check
158                                 //invalid "#","[","]","\" character
159                                 tmpstr = removebrank( str.substr( str.find( "[" )+1, str.rfind( "]", str.length() )-1 ) );
160                                 if( std::string::npos != tmpstr.find( "[" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
161                                 if( std::string::npos != tmpstr.find( "]" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
162                                 if( std::string::npos != tmpstr.find( "\\" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
163                                 if( std::string::npos != tmpstr.find( "#" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
164                                 if( std::string::npos != tmpstr.find( "=" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
165                                 sectionname = tmpstr;
166                         }else{
167                                 preparse.insert( std::pair<std::string,std::string>( sectionname,str ) );
168                         }
169                 }
170         }
171         catch( ... ){
172                 preparse.clear();
173                 retval  = false;
174         }
175         if( preparse.empty() )retval = false;
176         return  retval;
177 }
178
179 /*!
180  * read parameter and parse
181  * @param[in]   void
182  * @return              success = true / fail = false
183  */
184 bool    l7vs::ParameterImpl::rereadFile(PARAMETER_COMPONENT_TAG comp)
185 {
186         bool    retbool = true;
187
188         LOGGER_PUT_LOG_INFO( parameter_cat,2, "read parameter : COMPONENT = %s", compTable[comp].section.c_str() );
189
190         if( !readParameterFile( PARAMETER_FILE ) )return false;
191
192         std::multimap<std::string,std::string>::iterator        it;
193         std::multimap<std::string,std::string>::iterator        ite;
194         if( PARAM_COMP_ALL == comp ){
195                 it = preparse.begin();
196         }else{
197                 it = preparse.find( compTable[comp].section );
198         }
199         ite = preparse.end();
200                 
201         for( std::multimap<std::string,std::string>::iterator its = it; its !=ite; its++ ){
202                 //be made an invalid line if there is no = in a setting line.
203                 if( std::string::npos == its->second.find( "=" ) )continue;
204                 std::string     key = removebrank( its->second.substr( 0, its->second.find( "=" ) ) );
205                 //Key string validity check
206                 if( key.empty() )continue;
207                 //invalid "#","[","]","\" character
208                 if( std::string::npos != key.find( "[" ))continue;
209                 if( std::string::npos != key.find( "]" ))continue;
210                 if( std::string::npos != key.find( "\\" ))continue;
211                 if( std::string::npos != key.find( "#" ) )continue;
212
213                 key     = its->first + "." + key;
214
215                 std::string     value = removebrank( its->second.substr( its->second.find( "=" )+1, std::string::npos ) );
216                 //Value string validity check
217                 if( '"' == value[0] ) {
218                         if( std::string::npos == value.find( "\"", 1 ) )continue;
219                         std::string tmpstr = removebrank( value.substr( value.find( "\"", 1 )+1, value.find( "#", value.find( "\"", 1 )+1  ) ) );
220                         if( !tmpstr.empty() )continue;
221                         tmpstr = removebrank( value.substr( 1, value.find( "\"", 1 )-1 ) );
222
223
224
225                         stringMap[key] = tmpstr;
226                 }else{
227                         value = value.substr( 0, value.find_first_of( "#", 0 ) );
228                         if( std::string::npos != value.find( "\\" ))continue;
229                         if( std::string::npos != value.find( "=" ))continue;
230                         if( value.empty() )continue;
231                         if( isNumeric( value ) ){
232                                 try{
233 //                                      intMap[key] = l7vs::lexical_cast<int>( value );
234                                         intMap[key] = boost::lexical_cast<int>( value );
235                                 }
236 //                              catch(std::exception& ex){
237                                 catch (boost::bad_lexical_cast& ex) {
238                                         stringMap[key] = value;
239                                 }
240                         }else{
241                                 stringMap[key] = value;
242                         }
243                 }
244         }
245
246         //set parameter
247         std::map<PARAMETER_COMPONENT_TAG, component>::iterator itr = compTable.find( comp );
248         if( itr != compTable.end() ){
249                 try{
250                         if( NULL != itr->second.function )itr->second.function();
251                 }
252                 catch( const std::logic_error& err ){
253                         LOGGER_PUT_LOG_ERROR( parameter_cat,1, "set parameter function failure at category: %s", itr->second.section.c_str() );
254                         retbool = false;
255                 }
256         }
257         if( PARAM_COMP_ALL == comp ){
258                 for( itr = compTable.begin(); itr != compTable.end(); ++itr ){
259                         try{
260                                 if( NULL != itr->second.function )itr->second.function();
261                         }
262                         catch( const std::logic_error& err ){
263                                 LOGGER_PUT_LOG_ERROR( parameter_cat,2, "set parameter function failure at category: %s", itr->second.section.c_str() );
264                                 retbool = false;
265                         }
266                 }
267         }
268
269         return  retbool;
270 }
271
272 /*!
273  * Existence of a integer parameter is checked
274  * @param[in]   PARAMETER_COMPONENT_TAG
275  * @param[in]   const std::string
276  * @return              exist = true / not exist = false
277  */
278 bool    l7vs::ParameterImpl::isIntExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
279 {
280         bool    retval = false;
281         std::string comp_key = compTable[comp].section + "." + key;
282         if( intMap.end() != intMap.find( comp_key ) ){
283                 retval = true;
284         }
285         return  retval;
286 }
287
288 /*!
289  * Existence of a string parameter is checked
290  * @param[in]   PARAMETER_COMPONENT_TAG
291  * @param[in]   const std::string
292  * @return              exist = true / not exist = false
293  */
294 bool    l7vs::ParameterImpl::isStringExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
295 {
296         bool    retval = false;
297         std::string comp_key = compTable[comp].section + "." + key;
298         if( stringMap.end() != stringMap.find( comp_key ) ){
299                 retval = true;
300         }
301         return  retval;
302 }
303
304 /*!
305  * get a integer parameter
306  * @param[in]   PARAMETER_COMPONENT_TAG
307  * @param[in]   const std::string
308  * @return              integer
309  */
310 int     l7vs::ParameterImpl::getIntValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
311 {
312         std::string comp_key = compTable[comp].section + "." + key;
313         return intMap.find( comp_key )->second;
314 }
315
316 /*!
317  * get a string parameter
318  * @param[in]   PARAMETER_COMPONENT_TAG
319  * @param[in]   const std::string
320  * @return              std::string
321  */
322 std::string     l7vs::ParameterImpl::getStringValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
323 {
324         std::string     retstr = "";
325         std::string     comp_key = compTable[comp].section + "." + key;
326         std::map<std::string,std::string>::iterator it = stringMap.find( comp_key );
327         if( it != stringMap.end() )
328                 retstr = stringMap.find( comp_key )->second;
329         return  retstr;
330 }
331
332 /*!
333  * set-parameter function pointer relates component-tag
334  * @param[in]   comp    section TAG
335  * @param[in]   p_func  function pointer
336  */
337 void    l7vs::ParameterImpl::registerFunctionPointer( const PARAMETER_COMPONENT_TAG comp, void (*p_func)() )
338 {
339         compTable[comp].function = p_func;
340 }
341