4 #include "parameter_impl.h"
5 #include "logger_wrapper.h"
7 //#include "lexical_cast.h"
8 #include <boost/lexical_cast.hpp>
10 #ifndef PARAMETER_FILE
11 #define PARAMETER_FILE "/etc/l7vs/l7vs.cf"
12 #endif //PARAMETER_FILE
14 #define LINE_LENGTH 4096
16 #if !defined(LOGGER_PROCESS_VSD) && !defined(LOGGER_PROCESS_ADM) && !defined(LOGGER_PROCESS_SNM)
17 #define LOGGER_PROCESS_VSD
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;
25 LOG_CATEGORY_TAG parameter_cat = LOG_CAT_L7VSD_PARAMETER;
28 void loadLoggerConf(){
29 l7vs::Logger::getInstance().loadConf();
33 * ParameterImpl class Constructor
35 l7vs::ParameterImpl::ParameterImpl()
40 * Initialize ParameterImpl class
42 bool l7vs::ParameterImpl::init()
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);
66 retbool = rereadFile( PARAM_COMP_ALL );
72 * checks that an argument is a numeric.
73 * @param[in] const std::string
74 * @return is numeric = true / not numeric = false
76 bool l7vs::ParameterImpl::isNumeric(const std::string& val)
79 // l7vs::lexical_cast<int>(val);
80 boost::lexical_cast<int>(val);
82 // catch (std::exception& ex) {
83 catch (boost::bad_lexical_cast& ex) {
90 * delete blank before and behind a sentence
91 * @param[in] const std::string
94 std::string l7vs::ParameterImpl::removebrank( const std::string& val )
96 std::string str = val;
97 //remove brank(head of the sentence and end of the sentence)
98 std::string::iterator its;
100 while( its != str.end() ){
101 if( ( ' ' == *its ) || ( '\t' == *its ) ){
105 if( "" == str )return str;
106 //remove brank(head of the sentence and end of the sentence)
108 while( its != str.begin() ){
109 if( ( ' ' == *its ) || ( '\t' == *its ) ){
118 * Read Parameter from file
120 * @return success = true / fail = false
122 bool l7vs::ParameterImpl::readParameterFile( const std::string& val )
125 char readbuf[LINE_LENGTH];
127 LOGGER_PUT_LOG_INFO( parameter_cat,1, "read parameter file : %s", val.c_str() );
129 std::vector<std::string> paramlines;
134 std::ifstream ifs( val.c_str() );
139 memset( readbuf, 0, LINE_LENGTH );
140 ifs.getline( readbuf, ( LINE_LENGTH - 1 ) );
141 std::string str = readbuf;
142 str = removebrank( str );
144 if( '#' == str[0] )continue;
146 if( !str.empty() )paramlines.push_back( str );
148 std::string sectionname = compTable[PARAM_COMP_NOCAT].section;
149 for( std::vector<std::string>::iterator it = paramlines.begin();
150 it != paramlines.end(); it++ ){
152 std::string str = *it;
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;
167 preparse.insert( std::pair<std::string,std::string>( sectionname,str ) );
175 if( preparse.empty() )retval = false;
180 * read parameter and parse
182 * @return success = true / fail = false
184 bool l7vs::ParameterImpl::rereadFile(PARAMETER_COMPONENT_TAG comp)
188 LOGGER_PUT_LOG_INFO( parameter_cat,2, "read parameter : COMPONENT = %s", compTable[comp].section.c_str() );
190 if( !readParameterFile( PARAMETER_FILE ) )return false;
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();
197 it = preparse.find( compTable[comp].section );
199 ite = preparse.end();
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;
213 key = its->first + "." + key;
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 ) );
225 stringMap[key] = tmpstr;
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 ) ){
233 // intMap[key] = l7vs::lexical_cast<int>( value );
234 intMap[key] = boost::lexical_cast<int>( value );
236 // catch(std::exception& ex){
237 catch (boost::bad_lexical_cast& ex) {
238 stringMap[key] = value;
241 stringMap[key] = value;
247 std::map<PARAMETER_COMPONENT_TAG, component>::iterator itr = compTable.find( comp );
248 if( itr != compTable.end() ){
250 if( NULL != itr->second.function )itr->second.function();
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() );
257 if( PARAM_COMP_ALL == comp ){
258 for( itr = compTable.begin(); itr != compTable.end(); ++itr ){
260 if( NULL != itr->second.function )itr->second.function();
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() );
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
278 bool l7vs::ParameterImpl::isIntExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
281 std::string comp_key = compTable[comp].section + "." + key;
282 if( intMap.end() != intMap.find( comp_key ) ){
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
294 bool l7vs::ParameterImpl::isStringExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
297 std::string comp_key = compTable[comp].section + "." + key;
298 if( stringMap.end() != stringMap.find( comp_key ) ){
305 * get a integer parameter
306 * @param[in] PARAMETER_COMPONENT_TAG
307 * @param[in] const std::string
310 int l7vs::ParameterImpl::getIntValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
312 std::string comp_key = compTable[comp].section + "." + key;
313 return intMap.find( comp_key )->second;
317 * get a string parameter
318 * @param[in] PARAMETER_COMPONENT_TAG
319 * @param[in] const std::string
320 * @return std::string
322 std::string l7vs::ParameterImpl::getStringValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
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;
333 * set-parameter function pointer relates component-tag
334 * @param[in] comp section TAG
335 * @param[in] p_func function pointer
337 void l7vs::ParameterImpl::registerFunctionPointer( const PARAMETER_COMPONENT_TAG comp, void (*p_func)() )
339 compTable[comp].function = p_func;