Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_TransportControl.cpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include "C_TransportControl.hpp"
00021 #include "Utils.hpp"
00022 
00023 #include "GeneratorTrace.hpp"
00024 #include "GeneratorError.h"
00025 
00026 #include "dlfcn_t.hpp"
00027 
00028 #define XML_TRANSPORT_SECTION      (char*)"configuration"
00029 #define XML_TRANSPORT_SUBSECTION   (char*)"define"
00030 #define XML_TRANSPORT_ENTITY       (char*)"entity"
00031 #define XML_TRANSPORT_ENTITY_VALUE (char*)"transport"
00032 #define XML_TRANSPORT_NAME         (char*)"name"
00033 #define XML_TRANSPORT_FILE         (char*)"file"
00034 #define XML_TRANSPORT_CREATE       (char*)"create_function"
00035 #define XML_TRANSPORT_DELETE       (char*)"delete_function"
00036 #define XML_TRANSPORT_INIT         (char*)"init-args"
00037 
00038 typedef struct _transport_instance_info {
00039   C_Transport *m_instance ;
00040   int          m_id       ;
00041   char        *m_init_args ;
00042   char        *m_library_file_name ;
00043   char        *m_instance_create_fct_name ;
00044   char        *m_instance_delete_fct_name ;
00045 } T_TransportInstanceInfo, *T_pTransportInstanceInfo ;
00046 typedef list_t<T_pTransportInstanceInfo> T_TransportInstList ;
00047 
00048 C_TransportControl::C_TransportControl(T_logFunction P_logError, 
00049                                        T_logFunction P_logInfo) {
00050   GEN_DEBUG(1, "C_TransportControl::C_TransportControl() start");
00051   NEW_VAR(m_name_map, T_TransportNameMap());
00052   m_name_map->clear() ;
00053   m_transport_table_size = 0 ;
00054   m_transport_context_table = NULL ;
00055   m_call_select = &select ;
00056   NEW_VAR(m_id_gen, C_IdGenerator());
00057   m_log_error = P_logError ;
00058   m_log_info = P_logInfo ;
00059   GEN_DEBUG(1, "C_TransportControl::C_TransportControl() end");
00060 }
00061 
00062 C_TransportControl::~C_TransportControl() {
00063   int L_i ;
00064 
00065   GEN_DEBUG(1, "C_TransportControl::~C_TransportControl() start");
00066   if (!m_name_map->empty()) {
00067     m_name_map->erase(m_name_map->begin(), m_name_map->end());
00068   }
00069   DELETE_VAR(m_name_map);
00070   if (m_transport_table_size != 0) {
00071     for (L_i = 0 ; L_i < m_transport_table_size; L_i++) {
00072       m_transport_context_table[L_i]
00073         ->m_ext_delete(&(m_transport_context_table[L_i]->m_instance));
00074       FREE_VAR(m_transport_context_table[L_i]);
00075     }
00076     FREE_TABLE(m_transport_context_table);
00077     m_transport_table_size = 0 ;
00078   }
00079   DELETE_VAR(m_id_gen) ;
00080   GEN_DEBUG(1, "C_TransportControl::~C_TransportControl() end");
00081 }
00082 
00083 C_Transport* C_TransportControl::get_transport (char *P_name) {
00084   C_Transport *L_ret = NULL ;
00085   int          L_id         ;
00086 
00087   L_id = get_transport_id (P_name) ;
00088   if (L_id != ERROR_TRANSPORT_UNKNOWN) {
00089     L_ret = m_transport_context_table[L_id]->m_instance ;
00090   }
00091 
00092   return (L_ret) ;
00093 }
00094 
00095 C_Transport* C_TransportControl::get_transport (int   P_id) {
00096   C_Transport *L_ret = NULL ;
00097   if ((P_id < m_transport_table_size) && (P_id >= 0)) {
00098     L_ret = m_transport_context_table[P_id]->m_instance ;
00099   }
00100   return (L_ret);
00101 }
00102 
00103 C_TransportControl::T_pTransportContext C_TransportControl::get_transport_context (int   P_id) {
00104   T_pTransportContext L_ret = NULL ;
00105   if ((P_id < m_transport_table_size) && (P_id >= 0)) {
00106     L_ret = m_transport_context_table[P_id] ;
00107   }
00108   return (L_ret);
00109 }
00110 
00111 int C_TransportControl::get_transport_id (char *P_name) {
00112   int                          L_ret = ERROR_TRANSPORT_UNKNOWN ;
00113   T_TransportNameMap::iterator L_it ;
00114 
00115   L_it = m_name_map->find(T_TransportNameMap::key_type(P_name)) ;
00116   if (L_it != m_name_map->end()) {
00117     L_ret = L_it->second ;
00118   }
00119   return (L_ret) ;
00120 }
00121 
00122 bool C_TransportControl::fromXml (C_XmlData *P_data) {
00123 
00124   bool                          L_ret = true ;
00125   T_pXmlData_List               L_sectionList, L_subSectionList            ;
00126   T_XmlData_List::iterator      L_sectionListIt, L_subSectionListIt ;
00127   C_XmlData                    *L_section, *L_subSection ;
00128   char                         *L_entity_value ;
00129   char                         *L_name, 
00130                                *L_init_args,
00131                                *L_library_file_name,
00132                                *L_instance_create_fct_name,
00133                                *L_instance_delete_fct_name ;
00134   int                           L_transport_id ;
00135   T_pTransportInstanceInfo      L_transport_info = NULL ;
00136   T_TransportInstList           L_transport_info_list ;
00137   T_TransportInstList::iterator L_it ;
00138 
00139 
00140   GEN_DEBUG(1, "C_TransportControl::fromXml() start");
00141   if (P_data != NULL) {
00142     if ((L_sectionList = P_data->get_sub_data()) != NULL) {
00143       // std::cerr << "L_sectionList->size() " << L_sectionList->size() << std::endl;
00144       for(L_sectionListIt  = L_sectionList->begin() ;
00145           L_sectionListIt != L_sectionList->end() ;
00146           L_sectionListIt++) {
00147 
00148         L_section = *L_sectionListIt ;
00149         if (L_section != NULL) {
00150           if (strcmp(L_section->get_name(), XML_TRANSPORT_SECTION) == 0) {
00151 
00152             L_subSectionList = L_section->get_sub_data() ;
00153             for(L_subSectionListIt  = L_subSectionList->begin() ;
00154                 L_subSectionListIt != L_subSectionList->end() ;
00155                 L_subSectionListIt++) {
00156               L_subSection = *L_subSectionListIt ;
00157               
00158               if (strcmp(L_subSection->get_name(), XML_TRANSPORT_SUBSECTION) == 0) {
00159 
00160                 L_entity_value = L_subSection->find_value(XML_TRANSPORT_ENTITY) ;
00161                 if (L_entity_value != NULL) {
00162                   if (strcmp(L_entity_value, XML_TRANSPORT_ENTITY_VALUE) == 0) {
00163 
00164                     L_name = L_subSection->find_value(XML_TRANSPORT_NAME); 
00165                     if (L_name == NULL) {
00166                       GEN_ERROR(E_GEN_FATAL_ERROR, "[" 
00167                                 << L_name << "] value is mandatory for section ["
00168                                 << XML_TRANSPORT_SECTION << "], sub section ["
00169                                 << XML_TRANSPORT_SUBSECTION << "]");
00170                       L_ret = false ;
00171                       break ;
00172                     }
00173                     L_init_args = L_subSection->find_value(XML_TRANSPORT_INIT);
00174                     if (L_init_args == NULL) {
00175                       GEN_ERROR(E_GEN_FATAL_ERROR, "[" 
00176                                 << L_init_args << "] value is mandatory for section ["
00177                                 << XML_TRANSPORT_SECTION << "], sub section ["
00178                                 << XML_TRANSPORT_SUBSECTION << "]");
00179                       L_ret = false ;
00180                       break ;
00181                     }
00182                     L_library_file_name = L_subSection->find_value(XML_TRANSPORT_FILE);
00183                     if (L_library_file_name == NULL) {
00184                       GEN_ERROR(E_GEN_FATAL_ERROR, "[" 
00185                                 << L_library_file_name << "] value is mandatory for section ["
00186                                 << XML_TRANSPORT_SECTION << "], sub section ["
00187                                 << XML_TRANSPORT_SUBSECTION << "]");
00188                       L_ret = false ;
00189                       break ;
00190                     }
00191                     L_instance_create_fct_name = L_subSection->find_value(XML_TRANSPORT_CREATE);
00192                     if (L_instance_create_fct_name == NULL) {
00193                       GEN_ERROR(E_GEN_FATAL_ERROR, "[" 
00194                                 << L_instance_create_fct_name << "] value is mandatory for section ["
00195                                 << XML_TRANSPORT_SECTION << "], sub section ["
00196                                 << XML_TRANSPORT_SUBSECTION << "]");
00197                       L_ret = false ;
00198                       break ;
00199                     }
00200                     L_instance_delete_fct_name = L_subSection->find_value(XML_TRANSPORT_DELETE);
00201                     if (L_instance_delete_fct_name == NULL) {
00202                       GEN_ERROR(E_GEN_FATAL_ERROR, "[" 
00203                                 << L_instance_delete_fct_name << "] value is mandatory for section ["
00204                                 << XML_TRANSPORT_SECTION << "], sub section ["
00205                                 << XML_TRANSPORT_SUBSECTION << "]");
00206                       L_ret = false ;
00207                       break ;
00208                     }
00209 
00210                     // check transport name unicity
00211                     if (m_name_map->find(T_TransportNameMap::key_type(L_name)) 
00212                         != m_name_map->end()) {
00213                       GEN_ERROR(E_GEN_FATAL_ERROR, 
00214                                 XML_TRANSPORT_SECTION << " with name ["
00215                                 << L_name << "] already defined");
00216                       L_ret = false ;
00217                       break ;
00218                     }
00219 
00220                     L_transport_id = m_id_gen->new_id() ;
00221                     ALLOC_VAR(L_transport_info,
00222                               T_pTransportInstanceInfo,
00223                               sizeof(T_TransportInstanceInfo));
00224 
00225                     L_transport_info->m_id = L_transport_id ;
00226                     L_transport_info->m_instance = NULL ;
00227                     L_transport_info->m_init_args = L_init_args ;
00228                     L_transport_info->m_library_file_name = L_library_file_name;
00229                     L_transport_info->m_instance_create_fct_name = L_instance_create_fct_name ;
00230                     L_transport_info->m_instance_delete_fct_name = L_instance_delete_fct_name;
00231                     L_transport_info_list.push_back(L_transport_info);
00232                     m_name_map
00233                       ->insert(T_TransportNameMap::value_type(L_name,
00234                                                               L_transport_id)) ;
00235                     
00236                   }
00237 
00238                 }
00239                 
00240               }
00241 
00242             }
00243 
00244           }
00245         } // if (L_section != NULL)
00246       } // for(L_sectionListIt
00247 
00248       if (L_ret != false) {
00249         if (!L_transport_info_list.empty()) {
00250           m_transport_table_size = L_transport_info_list.size () ;
00251           ALLOC_TABLE(m_transport_context_table,
00252                       T_pTransportContext*,
00253                       sizeof(T_pTransportContext),
00254                       m_transport_table_size);
00255           for (L_it  = L_transport_info_list.begin();
00256                L_it != L_transport_info_list.end()  ;
00257                L_it++) {
00258             L_transport_info = *L_it ;
00259             ALLOC_VAR(m_transport_context_table[L_transport_info->m_id], 
00260                       T_pTransportContext, 
00261                       sizeof(T_TransportContext));
00262             if (update_context (m_transport_context_table[L_transport_info->m_id],
00263                                 L_transport_info) == false) {
00264               L_ret = false ;
00265             }
00266           }
00267         } else {
00268           GEN_ERROR(E_GEN_FATAL_ERROR,
00269                     "No transport definition found");
00270           L_ret = false ;
00271         }
00272       }
00273 
00274       if (!L_transport_info_list.empty()) {
00275         for (L_it  = L_transport_info_list.begin();
00276              L_it != L_transport_info_list.end()  ;
00277              L_it++) {
00278           if (L_ret == false) { // error
00279             if (m_transport_table_size > 0) {
00280               L_transport_info = *L_it ;
00281               FREE_VAR(m_transport_context_table[L_transport_info->m_id]);
00282             }
00283           }
00284           FREE_VAR(*L_it);
00285         }
00286         L_transport_info_list.erase(L_transport_info_list.begin(),
00287                                     L_transport_info_list.end());
00288       }
00289 
00290       if (L_ret == false) {
00291         // delete structures
00292         if (m_transport_table_size > 0) {
00293           FREE_TABLE(m_transport_context_table);
00294           m_transport_table_size = 0 ;
00295         }
00296       }
00297     }
00298   }
00299   GEN_DEBUG(1, "C_TransportControl::fromXml() end ret=" << L_ret);
00300 
00301   return (L_ret);
00302 }
00303 
00304 bool C_TransportControl::update_context (T_pTransportContext P_context,
00305                                         void                *P_value) {
00306 
00307   bool                     L_ret  = true ;
00308 
00309   T_pTransportInstanceInfo L_info = (T_pTransportInstanceInfo) P_value ;
00310   T_SelectDef              L_select ;
00311 
00312   GEN_DEBUG(1, "C_TransportControl::update_context() start");
00313   
00314   P_context->m_lib_handle 
00315     = dlopen(L_info->m_library_file_name, RTLD_LAZY);
00316   if (P_context->m_lib_handle == NULL) {
00317     GEN_ERROR(E_GEN_FATAL_ERROR, 
00318               "Unable to open library file [" 
00319               << L_info->m_library_file_name
00320               << "] error [" << dlerror() << "]");
00321     L_ret = false ;
00322   }
00323   
00324   if (L_ret) {
00325 
00326     P_context->m_ext_create
00327       = (T_TransportCreateInstance) dlsym
00328       (P_context->m_lib_handle, 
00329        L_info->m_instance_create_fct_name);
00330     
00331     if (P_context->m_ext_create == NULL) {
00332       GEN_ERROR(E_GEN_FATAL_ERROR, "Symbol [" << dlerror() << "]");
00333       L_ret = false ;
00334     }
00335 
00336     P_context->m_ext_delete
00337       = (T_TransportDeleteInstance) dlsym
00338       (P_context->m_lib_handle, 
00339        L_info->m_instance_delete_fct_name);
00340     
00341     if (P_context->m_ext_delete == NULL) {
00342       GEN_ERROR(E_GEN_FATAL_ERROR, "Symbol [" << dlerror() << "]");
00343       L_ret = false ;
00344     }
00345     
00346   }
00347 
00348   if (L_ret) {
00349     P_context->m_instance = (*(P_context->m_ext_create))() ;
00350     if (P_context->m_instance == NULL) {
00351       GEN_ERROR(E_GEN_FATAL_ERROR, "Transport instance not created");
00352       L_ret = false ;
00353     } else {
00354       L_select = (P_context->m_instance)->select_definition() ;
00355       if (L_select != NULL) {
00356         m_call_select = L_select;
00357       }
00358 
00359       return ( ((P_context->m_instance)->init(L_info->m_init_args, m_log_error, m_log_info) != 0) ? false : true) ; 
00360     }
00361   }
00362   
00363   GEN_DEBUG(1, "C_TransportControl::update_context() end ret=" << L_ret);
00364   
00365   return (L_ret);
00366 
00367 }
00368 
00369 T_SelectDef  C_TransportControl::get_call_select () {
00370   return (m_call_select);
00371 }
00372 
00373 
00374 bool C_TransportControl::set_config_value(T_pConfigValueList P_config_value_list) {
00375   int                           L_i                   ;
00376   int                           L_config    = 0       ;
00377 
00378   GEN_DEBUG(1, "C_TransportControl::set_config_value() start");
00379 
00380   if (!P_config_value_list->empty()) {
00381     if (m_transport_table_size > 0) {
00382       for (L_i = 0 ; L_i < m_transport_table_size; L_i++) {
00383         L_config = ((m_transport_context_table[L_i])->m_instance)
00384           ->config(P_config_value_list) ; // check of init fct to be done ?
00385         if (L_config != 0) break ;
00386       }
00387       
00388     }
00389   }
00390 
00391   GEN_DEBUG(1, "C_TransportControl::set_config_value() end ret=" << L_config);
00392             
00393   return ((L_config ==0) ? true : false);
00394 
00395 }
00396 

Generated on Wed Mar 7 14:44:59 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002