Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_ProtocolControl.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_ProtocolControl.hpp"
00021 #include "Utils.hpp"
00022 
00023 #include "GeneratorTrace.hpp"
00024 #include "GeneratorError.h"
00025 
00026 #include "C_ProtocolBinary.hpp"
00027 #include "C_ProtocolExternal.hpp"
00028 #include "C_ProtocolBinaryBodyNotInterpreted.hpp"
00029 #include "C_ProtocolBinarySeparator.hpp"
00030 #include "C_ProtocolText.hpp"
00031 #include "C_ProtocolTlv.hpp"
00032 
00033 
00034 #define XML_PROTOCOL_SECTION (char*)"protocol"
00035 #define XML_PROTOCOL_NAME    (char*)"name"
00036 #define XML_PROTOCOL_TYPE    (char*)"type"
00037 
00038 
00039 C_ProtocolControl::C_ProtocolControl(C_TransportControl *P_transport_control) {
00040   GEN_DEBUG(1, "C_ProtocolControl::C_ProtocolControl() start");
00041   NEW_VAR(m_name_map, T_ProtocolNameMap());
00042   m_name_map->clear() ;
00043   m_protocol_table = NULL ;
00044   m_protocol_name_table = NULL ;
00045   m_protocol_table_size = 0 ;
00046   NEW_VAR(m_id_gen, C_IdGenerator());
00047   m_transport_control = P_transport_control ;
00048   GEN_DEBUG(1, "C_ProtocolControl::C_ProtocolControl() end");
00049 }
00050 
00051 C_ProtocolControl::~C_ProtocolControl() {
00052 
00053   int L_i ;
00054 
00055 
00056   GEN_DEBUG(1, "C_ProtocolControl::~C_ProtocolControl() start");
00057   if (!m_name_map->empty()) {
00058     m_name_map->erase(m_name_map->begin(), m_name_map->end());
00059   }
00060   DELETE_VAR(m_name_map);
00061 
00062   if (m_protocol_table_size != 0) {
00063     for (L_i = 0 ; L_i < m_protocol_table_size; L_i++) {
00064       DELETE_VAR(m_protocol_table[L_i]);
00065     }
00066 
00067     FREE_TABLE(m_protocol_table);
00068     FREE_TABLE(m_protocol_name_table);
00069     m_protocol_table_size = 0 ;
00070   }
00071   DELETE_VAR(m_id_gen) ;
00072   m_transport_control = NULL ;
00073   GEN_DEBUG(1, "C_ProtocolControl::~C_ProtocolControl() end");
00074 }
00075 
00076 char* C_ProtocolControl::get_protocol_name(C_XmlData *P_data) {
00077   return (P_data->find_value(XML_PROTOCOL_NAME)) ;
00078 }
00079 
00080 char* C_ProtocolControl::get_protocol_type(C_XmlData *P_data) {
00081   return (P_data->find_value(XML_PROTOCOL_TYPE)) ;
00082 }
00083 
00084 
00085 bool C_ProtocolControl::fromXml (C_XmlData *P_data,
00086                                  T_pConfigValueList P_config_value_list,
00087                                  bool               P_display_protocol_stats) {
00088   
00089   bool                          L_ret       = true   ;
00090   T_pXmlData_List               L_subList            ;
00091   T_XmlData_List::iterator      L_subListIt          ;
00092   C_XmlData                    *L_data               ;
00093   char                         *L_protocol_name, 
00094                                *L_protocol_type      ;
00095   int                           L_protocol_id        ;
00096   T_ProtocolInstList            L_protocol_inst_list ;
00097   T_pProtocolInstanceInfo       L_protocol_info      ;
00098   T_ProtocolInstList::iterator  L_it                 ;
00099 
00100   
00101 
00102   GEN_DEBUG(1, "C_ProtocolControl::fromXml() start");
00103   if (P_data != NULL) {
00104     if ((L_subList = P_data->get_sub_data()) != NULL) {
00105       for (L_subListIt = L_subList->begin() ;
00106            L_subListIt != L_subList->end() ;
00107            L_subListIt++) {
00108 
00109         L_data = *L_subListIt ;
00110 
00111         if (L_data != NULL) {
00112           if (strcmp(L_data->get_name(), XML_PROTOCOL_SECTION) == 0) {
00113             // protocol section definition found
00114             L_protocol_name = get_protocol_name (L_data) ;
00115             // check protocol type for creation
00116             L_protocol_type = get_protocol_type (L_data) ;
00117             
00118             // check name/type presence
00119             if (L_protocol_name == NULL) {
00120               GEN_ERROR(E_GEN_FATAL_ERROR, 
00121                         "name mandatory for section " << XML_PROTOCOL_SECTION);
00122               L_ret = false ;
00123               break ;
00124             }
00125             if (L_protocol_type == NULL) {
00126               GEN_ERROR(E_GEN_FATAL_ERROR, 
00127                         "type mandatory for section " << XML_PROTOCOL_SECTION);
00128               L_ret = false ;
00129               break ;
00130             }
00131 
00132             // check protocol name unicity
00133             if (m_name_map->find(T_ProtocolNameMap::key_type(L_protocol_name)) 
00134                 != m_name_map->end()) {
00135               GEN_ERROR(E_GEN_FATAL_ERROR, 
00136                         XML_PROTOCOL_SECTION << " with name ["
00137                         << L_protocol_name << "] already defined");
00138               L_ret = false ;
00139               break ;
00140             }
00141 
00142             // check protocol type or sub type
00143             if (strcmp(L_protocol_type, "binary") == 0) {
00144               // create protocol instance
00145               
00146               C_ProtocolBinary    *L_protocol_instance = NULL ;
00147               T_ConstructorResult  L_res ;
00148 
00149               NEW_VAR(L_protocol_instance,
00150                       C_ProtocolBinary());
00151               
00152               L_protocol_instance->construction_data(L_data, &L_protocol_name, &L_res) ;
00153 
00154               if (L_res != E_CONSTRUCTOR_OK) {
00155                 GEN_ERROR(E_GEN_FATAL_ERROR,
00156                           "Error found in protocol definition");
00157                 DELETE_VAR(L_protocol_instance);
00158                 L_ret = false ;
00159                 break ;
00160               } else {
00161                 // store new instance
00162                 if (P_display_protocol_stats == true) {
00163                   C_ProtocolStats  *L_protocol_stats ;
00164                   NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
00165                   L_protocol_instance->set_stats(L_protocol_stats);
00166                 }
00167 
00168                 L_protocol_id = m_id_gen->new_id() ;
00169                 ALLOC_VAR(L_protocol_info,
00170                           T_pProtocolInstanceInfo,
00171                           sizeof(T_ProtocolInstanceInfo));
00172 
00173                 L_protocol_info->m_instance = L_protocol_instance ;
00174                 L_protocol_info->m_id = L_protocol_id ;
00175                 L_protocol_info->m_name = L_protocol_name ;
00176                 L_protocol_inst_list.push_back(L_protocol_info);
00177                 m_name_map
00178                   ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
00179                                                          L_protocol_id)) ;
00180               }
00181             } else if (strcmp(L_protocol_type, "binary-tlv") == 0) {
00182               // create protocol instance
00183               C_ProtocolTlv    *L_protocol_instance = NULL ;
00184               T_ConstructorResult  L_res ;
00185 
00186               NEW_VAR(L_protocol_instance,
00187                       C_ProtocolTlv());
00188               
00189               L_protocol_instance->construction_data(L_data, &L_protocol_name, &L_res) ;
00190               
00191               if (L_res != E_CONSTRUCTOR_OK) {
00192                 GEN_ERROR(E_GEN_FATAL_ERROR,
00193                           "Error found in protocol definition");
00194                 DELETE_VAR(L_protocol_instance);
00195                 L_ret = false ;
00196                 break ;
00197               } else {
00198                 // store new instance
00199                 if (P_display_protocol_stats == true) {
00200                   C_ProtocolStats  *L_protocol_stats ;
00201                   NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
00202                   L_protocol_instance->set_stats(L_protocol_stats);
00203                 }
00204                 
00205                 L_protocol_id = m_id_gen->new_id() ;
00206                 ALLOC_VAR(L_protocol_info,
00207                           T_pProtocolInstanceInfo,
00208                           sizeof(T_ProtocolInstanceInfo));
00209                 
00210                 L_protocol_info->m_instance = L_protocol_instance ;
00211                 L_protocol_info->m_id = L_protocol_id ;
00212                 L_protocol_info->m_name = L_protocol_name ;
00213                 L_protocol_inst_list.push_back(L_protocol_info);
00214                 m_name_map
00215                   ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
00216                                                          L_protocol_id)) ;
00217               }
00218             } else if (strcmp(L_protocol_type, "binary-body-not-interpreted") == 0) {
00219               // create protocol instance
00220               C_ProtocolBinaryBodyNotInterpreted    *L_protocol_instance = NULL ;
00221 
00222               T_ConstructorResult  L_res ;
00223 
00224 
00225               NEW_VAR(L_protocol_instance,
00226                       C_ProtocolBinaryBodyNotInterpreted());
00227               
00228               L_protocol_instance->construction_data(L_data, &L_protocol_name, &L_res) ;
00229               
00230               if (L_res != E_CONSTRUCTOR_OK) {
00231                 GEN_ERROR(E_GEN_FATAL_ERROR,
00232                           "Error found in protocol definition");
00233                 DELETE_VAR(L_protocol_instance);
00234                 L_ret = false ;
00235                 break ;
00236               } else {
00237                 // store new instance
00238                 if (P_display_protocol_stats == true) {
00239                   C_ProtocolStats  *L_protocol_stats ;
00240                   NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
00241                   L_protocol_instance->set_stats(L_protocol_stats);
00242                 }
00243                 
00244                 L_protocol_id = m_id_gen->new_id() ;
00245                 ALLOC_VAR(L_protocol_info,
00246                           T_pProtocolInstanceInfo,
00247                           sizeof(T_ProtocolInstanceInfo));
00248                 
00249                 L_protocol_info->m_instance = L_protocol_instance ;
00250                 L_protocol_info->m_id = L_protocol_id ;
00251                 L_protocol_info->m_name = L_protocol_name ;
00252                 L_protocol_inst_list.push_back(L_protocol_info);
00253                 m_name_map
00254                   ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
00255                                                          L_protocol_id)) ;
00256               }
00257 
00258             } else if (strcmp(L_protocol_type, "binary-separator") == 0) {
00259 
00260               // create protocol instance
00261               C_ProtocolBinarySeparator    *L_protocol_instance = NULL ;
00262 
00263               T_ConstructorResult  L_res ;
00264 
00265 
00266               NEW_VAR(L_protocol_instance,
00267                       C_ProtocolBinarySeparator());
00268               
00269               L_protocol_instance
00270                 ->construction_data(L_data, &L_protocol_name, &L_res) ;
00271               
00272               if (L_res != E_CONSTRUCTOR_OK) {
00273                 GEN_ERROR(E_GEN_FATAL_ERROR,
00274                           "Error found in protocol definition");
00275                 DELETE_VAR(L_protocol_instance);
00276                 L_ret = false ;
00277                 break ;
00278               } else {
00279                 // store new instance
00280                 if (P_display_protocol_stats == true) {
00281                   C_ProtocolStats  *L_protocol_stats ;
00282                   NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
00283                   L_protocol_instance->set_stats(L_protocol_stats);
00284                 }
00285                 
00286                 L_protocol_id = m_id_gen->new_id() ;
00287                 ALLOC_VAR(L_protocol_info,
00288                           T_pProtocolInstanceInfo,
00289                           sizeof(T_ProtocolInstanceInfo));
00290                 
00291                 L_protocol_info->m_instance = L_protocol_instance ;
00292                 L_protocol_info->m_id = L_protocol_id ;
00293                 L_protocol_info->m_name = L_protocol_name ;
00294                 L_protocol_inst_list.push_back(L_protocol_info);
00295                 m_name_map
00296                   ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
00297                                                          L_protocol_id)) ;
00298               }
00299             } else if (strcmp(L_protocol_type, "external-library") == 0) {
00300               
00301               C_ProtocolExternal    *L_protocol_instance = NULL ;
00302               T_ConstructorResult    L_res = E_CONSTRUCTOR_OK ;
00303               NEW_VAR(L_protocol_instance,
00304                       C_ProtocolExternal(m_transport_control, 
00305                                          L_data, &L_protocol_name, 
00306                                          P_config_value_list,
00307                                          &L_res));
00308 
00309               if (L_res != E_CONSTRUCTOR_OK) {
00310                 GEN_ERROR(E_GEN_FATAL_ERROR,
00311                           "Error found in protocol definition");
00312                 DELETE_VAR(L_protocol_instance);
00313                 L_ret = false ;
00314                 break ;
00315               } else {
00316 
00317                 if (P_display_protocol_stats == true) {
00318                   C_ProtocolStats  *L_protocol_stats ;
00319                   NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
00320                   L_protocol_instance->set_stats(L_protocol_stats);
00321                 }
00322 
00323                 // store new instance
00324                 L_protocol_id = m_id_gen->new_id() ;
00325                 ALLOC_VAR(L_protocol_info,
00326                           T_pProtocolInstanceInfo,
00327                           sizeof(T_ProtocolInstanceInfo));
00328 
00329                 L_protocol_info->m_instance = L_protocol_instance ;
00330                 L_protocol_info->m_id = L_protocol_id ;
00331                 L_protocol_info->m_name = L_protocol_name ;
00332                 L_protocol_inst_list.push_back(L_protocol_info);
00333                 m_name_map
00334                   ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
00335                                                          L_protocol_id)) ;
00336               }
00337             }  else if (strcmp(L_protocol_type, "text") == 0) {
00338               
00339               C_ProtocolText    *L_protocol_instance = NULL ;
00340               T_ConstructorResult    L_res = E_CONSTRUCTOR_OK ;
00341 
00342               NEW_VAR(L_protocol_instance,
00343                       C_ProtocolText());
00344               
00345               L_protocol_instance->analyze_data(L_data, 
00346                                                 &L_protocol_name, 
00347                                                 P_config_value_list,
00348                                                 &L_res);
00349 
00350             if (L_res != E_CONSTRUCTOR_OK) {
00351                 GEN_ERROR(E_GEN_FATAL_ERROR,
00352                           "Error found in protocol definition");
00353                 DELETE_VAR(L_protocol_instance);
00354                 L_ret = false ;
00355                 break ;
00356               } else {
00357 
00358                 if (P_display_protocol_stats == true) {
00359                   C_ProtocolStats  *L_protocol_stats ;
00360                   NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
00361                   L_protocol_instance->set_stats(L_protocol_stats);
00362                 }
00363 
00364                 // store new instance
00365                 L_protocol_id = m_id_gen->new_id() ;
00366                 ALLOC_VAR(L_protocol_info,
00367                           T_pProtocolInstanceInfo,
00368                           sizeof(T_ProtocolInstanceInfo));
00369 
00370                 L_protocol_info->m_instance = L_protocol_instance ;
00371                 L_protocol_info->m_id = L_protocol_id ;
00372                 L_protocol_info->m_name = L_protocol_name ;
00373                 L_protocol_inst_list.push_back(L_protocol_info);
00374                 m_name_map
00375                   ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
00376                                                          L_protocol_id)) ;
00377               }
00378             } else {
00379               GEN_ERROR(E_GEN_FATAL_ERROR,
00380                         XML_PROTOCOL_SECTION 
00381                         << " [" 
00382                         << L_protocol_name
00383                         << "] with type ["
00384                         << L_protocol_type << "] unsupported");
00385               L_ret = false ;
00386             }
00387           }
00388         }
00389       }
00390       if (L_ret != false) {
00391         if (!L_protocol_inst_list.empty()) {
00392           m_protocol_table_size = L_protocol_inst_list.size() ;
00393           ALLOC_TABLE(m_protocol_table,
00394                       T_pC_ProtocolFrame*,
00395                       sizeof(T_pC_ProtocolFrame),
00396                       m_protocol_table_size) ;
00397           ALLOC_TABLE(m_protocol_name_table,
00398                       char**,
00399                       sizeof(char*),
00400                       m_protocol_table_size) ;
00401           for (L_it  = L_protocol_inst_list.begin();
00402                L_it != L_protocol_inst_list.end()  ;
00403                L_it++) {
00404             L_protocol_info = *L_it ;
00405             m_protocol_table[L_protocol_info->m_id] 
00406               = L_protocol_info->m_instance ;
00407             m_protocol_name_table[L_protocol_info->m_id]
00408               = L_protocol_info->m_name ;
00409           }
00410         } else {
00411           GEN_ERROR(E_GEN_FATAL_ERROR,
00412                     "No protocol definition found"); 
00413           L_ret = false ;
00414         }
00415       } // if L_ret != false 
00416       
00417       if (!L_protocol_inst_list.empty()) {
00418           for (L_it  = L_protocol_inst_list.begin();
00419                L_it != L_protocol_inst_list.end()  ;
00420                L_it++) {
00421             FREE_VAR(*L_it);
00422           }
00423           L_protocol_inst_list.erase(L_protocol_inst_list.begin(),
00424                                      L_protocol_inst_list.end());
00425       }
00426     } 
00427   }
00428 
00429   GEN_DEBUG(1, "C_ProtocolControl::fromXml() end ret=" << L_ret);
00430   return (L_ret);
00431 }
00432 
00433 C_ProtocolFrame* C_ProtocolControl::get_protocol (char *P_name) {
00434   C_ProtocolFrame            *L_ret = NULL ;
00435   int                         L_id         ;
00436 
00437   L_id = get_protocol_id (P_name) ;
00438   if (L_id != ERROR_PROTOCOL_UNKNOWN) {
00439     L_ret = m_protocol_table[L_id] ;
00440   }
00441 
00442   return (L_ret) ;
00443 }
00444 
00445 C_ProtocolFrame* C_ProtocolControl::get_protocol (int P_id) {
00446   C_ProtocolFrame *L_ret = NULL ;
00447   if ((P_id < m_protocol_table_size) && (P_id >= 0)) {
00448     L_ret = m_protocol_table[P_id] ;
00449   }
00450   return (L_ret);
00451 }
00452 
00453 int C_ProtocolControl::get_protocol_id (char *P_name) {
00454   int                         L_ret = ERROR_PROTOCOL_UNKNOWN ;
00455   T_ProtocolNameMap::iterator L_it ;
00456 
00457   L_it = m_name_map->find(T_ProtocolNameMap::key_type(P_name)) ;
00458   if (L_it != m_name_map->end()) {
00459     L_ret = L_it->second ;
00460   }
00461   return (L_ret) ;
00462 }
00463 
00464 
00465 int C_ProtocolControl::get_nb_protocol () {
00466   return (m_protocol_table_size);
00467 }
00468 
00469 char* C_ProtocolControl::get_protocol_name (int P_id) {
00470   char *L_ret = NULL ;
00471   if ((P_id < m_protocol_table_size) && (P_id >= 0)) {
00472     L_ret = m_protocol_name_table[P_id] ;
00473   }
00474   return (L_ret);
00475 }
00476 
00477 
00478 

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