4 #include "parameter_impl.h"
5 #include "logger_wrapper.h"
8 //#include "lexical_cast.h"
9 #include <boost/lexical_cast.hpp>
11 #ifndef PARAMETER_FILE
12 #define PARAMETER_FILE "/etc/l7vs/l7vs.cf"
13 #endif //PARAMETER_FILE
15 #define LINE_LENGTH 4096
17 #if !defined(LOGGER_PROCESS_VSD) && !defined(LOGGER_PROCESS_ADM) && !defined(LOGGER_PROCESS_SNM)
18 #define LOGGER_PROCESS_VSD
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;
26 LOG_CATEGORY_TAG parameter_cat = LOG_CAT_L7VSD_PARAMETER;
29 void loadLoggerConf(){
30 l7vs::Logger::getInstance().loadConf();
34 * ParameterImpl class Constructor
36 l7vs::ParameterImpl::ParameterImpl()
41 * Initialize ParameterImpl class
43 bool l7vs::ParameterImpl::init()
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);
67 retbool = rereadFile( PARAM_COMP_ALL );
73 * checks that an argument is a numeric.
74 * @param[in] const std::string
75 * @return is numeric = true / not numeric = false
77 bool l7vs::ParameterImpl::isNumeric(const std::string& val)
80 // l7vs::lexical_cast<int>(val);
81 boost::lexical_cast<int>(val);
83 // catch (std::exception& ex) {
84 catch (boost::bad_lexical_cast& ex) {
91 * delete blank before and behind a sentence
92 * @param[in] const std::string
95 std::string l7vs::ParameterImpl::removebrank( const std::string& val )
97 std::string str = val;
98 //remove brank(head of the sentence and end of the sentence)
99 std::string::iterator its;
101 while( its != str.end() ){
102 if( ( ' ' == *its ) || ( '\t' == *its ) ){
106 if( "" == str )return str;
107 //remove brank(head of the sentence and end of the sentence)
109 while( its != str.begin() ){
110 if( ( ' ' == *its ) || ( '\t' == *its ) ){
119 * Read Parameter from file
121 * @return success = true / fail = false
123 bool l7vs::ParameterImpl::readParameterFile( const std::string& val )
126 char readbuf[LINE_LENGTH];
128 LOGGER_PUT_LOG_INFO( parameter_cat,1, "read parameter file : %s", val.c_str() );
130 std::vector<std::string> paramlines;
135 std::ifstream ifs( val.c_str() );
140 memset( readbuf, 0, LINE_LENGTH );
141 ifs.getline( readbuf, ( LINE_LENGTH - 1 ) );
142 std::string str = readbuf;
143 str = removebrank( str );
145 if( '#' == str[0] )continue;
147 if( !str.empty() )paramlines.push_back( str );
149 std::string sectionname = compTable[PARAM_COMP_NOCAT].section;
150 for( std::vector<std::string>::iterator it = paramlines.begin();
151 it != paramlines.end(); it++ ){
153 std::string str = *it;
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;
168 preparse.insert( std::pair<std::string,std::string>( sectionname,str ) );
176 if( preparse.empty() )retval = false;
181 * read parameter and parse
183 * @return success = true / fail = false
185 bool l7vs::ParameterImpl::rereadFile(PARAMETER_COMPONENT_TAG comp)
189 LOGGER_PUT_LOG_INFO( parameter_cat,2, "read parameter : COMPONENT = %s", compTable[comp].section.c_str() );
191 if( !readParameterFile( PARAMETER_FILE ) )return false;
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();
198 it = preparse.find( compTable[comp].section );
200 ite = preparse.end();
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;
214 key = its->first + "." + key;
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 ) );
226 stringMap[key] = tmpstr;
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 ) ){
234 // intMap[key] = l7vs::lexical_cast<int>( value );
235 intMap[key] = boost::lexical_cast<int>( value );
237 // catch(std::exception& ex){
238 catch (boost::bad_lexical_cast& ex) {
239 stringMap[key] = value;
242 stringMap[key] = value;
248 std::map<PARAMETER_COMPONENT_TAG, component>::iterator itr = compTable.find( comp );
249 if( itr != compTable.end() ){
251 if( NULL != itr->second.function )itr->second.function();
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() );
258 if( PARAM_COMP_ALL == comp ){
259 for( itr = compTable.begin(); itr != compTable.end(); ++itr ){
261 if( NULL != itr->second.function )itr->second.function();
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() );
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
279 bool l7vs::ParameterImpl::isIntExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
282 std::string comp_key = compTable[comp].section + "." + key;
283 if( intMap.end() != intMap.find( comp_key ) ){
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
295 bool l7vs::ParameterImpl::isStringExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
298 std::string comp_key = compTable[comp].section + "." + key;
299 if( stringMap.end() != stringMap.find( comp_key ) ){
306 * get a integer parameter
307 * @param[in] PARAMETER_COMPONENT_TAG
308 * @param[in] const std::string
311 int l7vs::ParameterImpl::getIntValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
313 std::string comp_key = compTable[comp].section + "." + key;
314 return intMap.find( comp_key )->second;
318 * get a string parameter
319 * @param[in] PARAMETER_COMPONENT_TAG
320 * @param[in] const std::string
321 * @return std::string
323 std::string l7vs::ParameterImpl::getStringValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
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;
334 * set-parameter function pointer relates component-tag
335 * @param[in] comp section TAG
336 * @param[in] p_func function pointer
338 void l7vs::ParameterImpl::registerFunctionPointer( const PARAMETER_COMPONENT_TAG comp, void (*p_func)() )
340 compTable[comp].function = p_func;