OSDN Git Service

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