Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_ProtocolText.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_ProtocolText.hpp"
00021 
00022 #include "GeneratorError.h"
00023 #include "C_MessageText.hpp"
00024 #include "BufferUtils.hpp"
00025 #include <cstdlib> // for strtoul
00026 #include "GeneratorTrace.hpp"
00027 #include "ProtocolData.hpp"
00028 #include "dlfcn_t.hpp"
00029 #include "ParserFrame.hpp"
00030 #include "FilterFrame.hpp"
00031 #include "TextUtils.hpp"
00032 
00033 #include <regex.h>
00034 
00035 
00036 C_ProtocolText::C_ProtocolText() : C_ProtocolTextFrame() {
00037 
00038   m_header_fields_list          = NULL ;
00039   m_body_fields_list            = NULL ;
00040   m_fields_name_map             = NULL ;
00041   m_id_counter                  = -1   ;
00042   m_fields_desc_table           = NULL ;
00043   m_header_fields_dico          = NULL ;
00044   m_value_sessions_table        = NULL ;
00045   NEW_VAR(m_message_decode_map, T_DecodeMap());
00046   m_message_decode_map->clear();
00047 
00048   m_body_separator              = NULL ;
00049   m_body_separator_size         = 0    ;
00050 
00051   m_field_separator             = NULL ;
00052   m_field_separator_size        = 0    ;
00053 
00054   m_message_type_field_id       = -1   ;
00055   m_session_id_id               = -1   ;
00056   m_session_method              = &C_MessageText::getSessionFromField ;
00057 
00058   m_nb_methods                  = 0    ;
00059   m_methods                     = NULL ; 
00060 
00061   m_nb_fields_desc_table        = 0    ;
00062 
00063   NEW_VAR (m_message_name_list, T_NameAndIdList()) ;
00064   m_names_fields            = NULL    ;
00065   m_def_method_list         = NULL    ;  
00066   m_filter_function         = NULL    ;
00067 
00068   m_def_method_extern_list   = NULL    ;  
00069   m_method_external_table    = NULL   ;
00070   m_nb_method_external_table = 0      ;
00071   m_config_value_list    = NULL ;
00072 }
00073 
00074 void  C_ProtocolText::analyze_data( C_XmlData            *P_def, 
00075                                     char                **P_name,
00076                                     T_pConfigValueList    P_config_value_list,
00077                                     T_pContructorResult   P_res) {
00078 
00079   T_ConstructorResult  L_res               = E_CONSTRUCTOR_OK ;
00080 
00081   char                *L_body_separator    = NULL             ;
00082   char                *L_field_separator   = NULL             ;
00083   char                *L_filter_method     = NULL             ;
00084 
00085 
00086   GEN_DEBUG(1, "C_ProtocolText::C_ProtocolText() start");
00087 
00088   m_config_value_list = P_config_value_list ;
00089 
00090   if (P_def) {
00091     *P_name = P_def->find_value((char*)"name") ;
00092     if (*P_name == NULL) {
00093       GEN_ERROR(E_GEN_FATAL_ERROR, 
00094                 "No name for protocol definition");
00095       L_res = E_CONSTRUCTOR_KO ;
00096     }
00097     
00098     if (L_res == E_CONSTRUCTOR_OK) {
00099       m_name = *P_name ;
00100 
00101       L_body_separator = P_def->find_value((char *)"body-separator");
00102       if (L_body_separator == NULL) {
00103         GEN_ERROR(E_GEN_FATAL_ERROR, 
00104                   "No body separator for protocol definition");
00105         L_res = E_CONSTRUCTOR_KO ;
00106       } else {
00107         if (strlen(L_body_separator) == 0) {
00108           GEN_ERROR(E_GEN_FATAL_ERROR, 
00109                     "Empty body separator for protocol definition");
00110           L_res = E_CONSTRUCTOR_KO ;
00111         } else {
00112           set_body_separator(L_body_separator);
00113         }
00114       }
00115 
00116       if (L_res == E_CONSTRUCTOR_OK) {
00117         // field-separator
00118         L_field_separator = P_def->find_value((char *)"field-separator");
00119         if (L_field_separator == NULL) {
00120           GEN_ERROR(E_GEN_FATAL_ERROR, 
00121                     "No field separator for protocol definition");
00122           L_res = E_CONSTRUCTOR_KO ;
00123         } else {
00124           // std::cerr << "L_field_separator " << strlen(L_field_separator)
00125           //          << std::endl;
00126           if (strlen(L_field_separator) != 0) {
00127             set_field_separator(L_field_separator);
00128           }
00129         }
00130       }
00131 
00132       if (L_res == E_CONSTRUCTOR_OK) {
00133         m_field_body_separator_size = m_body_separator_size + m_field_separator_size ;
00134         if (m_field_body_separator_size != 0 ) {
00135           ALLOC_TABLE(m_field_body_separator, 
00136                       char*, 
00137                       sizeof(char), 
00138                       (m_field_body_separator_size+1));
00139 
00140           m_field_body_separator[0] = 0 ;
00141 
00142           if (m_field_separator_size != 0 ) {
00143             strcat(m_field_body_separator, m_field_separator);
00144           }
00145 
00146           if (m_body_separator_size != 0) {
00147             strcat(m_field_body_separator, m_body_separator);
00148           }
00149 
00150         }
00151       }
00152   
00153 
00154       if (L_res == E_CONSTRUCTOR_OK) {
00155         // filter method
00156         L_filter_method = P_def->find_value((char *)"filter");
00157         if (L_filter_method != NULL) {
00158           L_res =  (set_filter_method (L_filter_method) == 0) 
00159             ? E_CONSTRUCTOR_OK : E_CONSTRUCTOR_KO ;
00160         }
00161       }
00162 
00163       if (L_res == E_CONSTRUCTOR_OK) {
00164         *P_res = (xml_interpretor(P_def,P_name,P_config_value_list) == 0) 
00165           ? E_CONSTRUCTOR_OK : E_CONSTRUCTOR_KO ;
00166         if (*P_res == E_CONSTRUCTOR_OK) {
00167           *P_res = (set_body_method () == 0) 
00168             ? E_CONSTRUCTOR_OK : E_CONSTRUCTOR_KO ;
00169         }
00170       } else {
00171         *P_res = L_res ;
00172       }
00173     } else {
00174       *P_res = L_res ;
00175     }
00176   } else {
00177     *P_res = E_CONSTRUCTOR_KO ;
00178   }
00179 
00180   GEN_DEBUG(1, "C_ProtocolText::C_ProtocolText() end");
00181 
00182 }
00183 
00184 int C_ProtocolText::set_filter_method (char *P_filter_method) {
00185   int                         L_ret          = 0             ;
00186   char                       *L_lib_name     = NULL          ;
00187   char                       *L_fun_name     = NULL          ;
00188   void                       *L_library_handle               ; 
00189   void                       *L_function                     ;
00190 
00191   L_lib_name = find_text_value(P_filter_method,(char*)"lib")  ;
00192   if (L_lib_name == NULL ) {
00193     GEN_ERROR(E_GEN_FATAL_ERROR,
00194               "no name for the library for the filter (lib=...)");
00195     L_ret = -1;
00196   } else {
00197     L_library_handle = dlopen(L_lib_name, RTLD_LAZY);
00198     if (L_library_handle == NULL) {
00199       GEN_ERROR(E_GEN_FATAL_ERROR, 
00200                 "Unable to open library file [" 
00201                 << L_lib_name
00202                 << "] error [" << dlerror() << "]");
00203       L_ret = -1 ;
00204     }
00205   }
00206 
00207   L_fun_name = find_text_value(P_filter_method,(char*)"function") ;
00208   if (L_fun_name == NULL ) {
00209     GEN_ERROR(E_GEN_FATAL_ERROR,
00210               "no name for the function for the parser (function=...)");
00211     L_ret = -1;
00212   } else {
00213     if (L_library_handle) { 
00214       L_function = dlsym(L_library_handle, L_fun_name);
00215       if (L_function == NULL) {
00216         GEN_ERROR(E_GEN_FATAL_ERROR, "Error [" << dlerror() << "]");
00217         L_ret = -1 ;
00218       } else {
00219         m_filter_function = (T_FilterFunction) L_function ;
00220       }
00221     }
00222   }
00223 
00224   return (L_ret);
00225 
00226 }
00227 
00228 int C_ProtocolText::analyze_body_method_param (int  P_index,
00229                                                char *P_body_method_param) {
00230 
00231   int                         L_ret          = 0             ;
00232   char                       *L_lib_name     = NULL          ;
00233   char                       *L_fun_name     = NULL          ;
00234 
00235   void                       *L_library_handle               ; 
00236   void                       *L_function                     ;
00237 
00238 
00239   L_lib_name = find_text_value(P_body_method_param,(char*)"lib")  ;
00240   if (L_lib_name == NULL ) {
00241     GEN_ERROR(E_GEN_FATAL_ERROR,
00242               "no name for the library for the parser (lib=...)");
00243     L_ret = -1;
00244   } else {
00245     L_library_handle = dlopen(L_lib_name, RTLD_LAZY);
00246     if (L_library_handle == NULL) {
00247       GEN_ERROR(E_GEN_FATAL_ERROR, 
00248                 "Unable to open library file [" 
00249                 << L_lib_name
00250                 << "] error [" << dlerror() << "]");
00251       L_ret = -1 ;
00252     }
00253   }
00254 
00255   L_fun_name = find_text_value(P_body_method_param,(char*)"function") ;
00256   if (L_fun_name == NULL ) {
00257     GEN_ERROR(E_GEN_FATAL_ERROR,
00258               "no name for the function for the parser (function=...)");
00259     L_ret = -1;
00260   } else {
00261     if (L_library_handle) { 
00262       L_function = dlsym(L_library_handle, L_fun_name);
00263       if (L_function == NULL) {
00264         GEN_ERROR(E_GEN_FATAL_ERROR, "Error [" << dlerror() << "]");
00265         L_ret = -1 ;
00266       } else {
00267 
00268         set_body_decode_method(P_index,
00269                                E_BODY_METHOD_PARSE,
00270                                &C_MessageText::DecodeBodyWithParser,
00271                                (void*)L_function);
00272 
00273         set_encode_method(P_index,
00274                           E_BODY_METHOD_PARSE,
00275                           &C_MessageText::NoEncode,
00276                           (void*)L_function);
00277 
00278       }
00279     }
00280   }
00281 
00282   return (L_ret);
00283 
00284 }
00285 
00286 
00287 int C_ProtocolText::set_body_method() {
00288 
00289   int                       L_ret          = 0 ;
00290   T_DefMethodList::iterator L_def_method_it    ;
00291   T_FieldNameMap::iterator  L_it               ;
00292   
00293   
00294 
00295   if (!m_def_method_list->empty()) {
00296     if (m_def_method_list->size() > 0) {
00297 
00298       int L_i ;
00299 
00300       set_number_methods((int)m_def_method_list->size());
00301       L_i = 0 ;
00302 
00303       for (L_def_method_it = m_def_method_list->begin();
00304            L_def_method_it != m_def_method_list->end();
00305            L_def_method_it++) {
00306 
00307         switch (L_def_method_it->m_method) {
00308         case E_BODY_METHOD_LENGTH:
00309           // analyze id to decode
00310           L_it = m_fields_name_map->find(T_FieldNameMap::key_type(L_def_method_it->m_param));
00311           if (L_it == m_fields_name_map->end()) {
00312             GEN_ERROR(E_GEN_FATAL_ERROR, 
00313                       "field [" << L_def_method_it->m_param << "] not defined");
00314             L_ret = -1 ;
00315           } else {
00316             set_body_decode_method(L_i,
00317                                    L_def_method_it->m_method,
00318                                    &C_MessageText::DecodeBodyWithContentLength,
00319                                    (void*)&((L_it->second)->m_id));
00320             set_encode_method(L_i,
00321                               L_def_method_it->m_method,
00322                               &C_MessageText::EncodeWithContentLength,
00323                               (void*)&((L_it->second)->m_id));
00324           }
00325           break ;
00326         case E_BODY_METHOD_PARSE:
00327           // load the library
00328           L_ret = analyze_body_method_param(L_i, L_def_method_it->m_param) ;
00329           break ;
00330         }
00331         if (L_ret != 0) break ;
00332         L_i ++ ;
00333       }
00334     } else {
00335       GEN_ERROR(E_GEN_FATAL_ERROR, 
00336                 "no body decode method defined");
00337       L_ret = -1 ;
00338     }
00339   } else {
00340     GEN_ERROR(E_GEN_FATAL_ERROR, 
00341               "no body decode method defined");
00342     L_ret = -1 ;
00343   }
00344 
00345   return (L_ret);
00346 }
00347 
00348 
00349 C_ProtocolText::~C_ProtocolText() {
00350 
00351   T_FieldDefList::iterator     L_it             ;
00352   int                          L_i              ;
00353   T_pFieldDef                  L_field_def ;
00354 
00355   m_id_counter = -1 ;
00356 
00357   if ((m_header_fields_list != NULL ) && (!m_header_fields_list->empty())) {
00358     for (L_it = m_header_fields_list->begin();
00359          L_it != m_header_fields_list->end() ;
00360          L_it++) {
00361       L_field_def = *L_it;
00362       if (!(L_field_def->m_expr_list)->empty()) {
00363         (L_field_def->m_expr_list)->erase((L_field_def->m_expr_list)->begin(),
00364                                           (L_field_def->m_expr_list)->end());
00365       }
00366     }
00367     m_header_fields_list->erase(m_header_fields_list->begin(),
00368                                 m_header_fields_list->end());
00369     DELETE_VAR (m_header_fields_list) ;
00370   }
00371 
00372   if ((m_body_fields_list != NULL) && (!m_body_fields_list->empty())) {
00373     for (L_it = m_body_fields_list->begin();
00374          L_it != m_body_fields_list->end() ;
00375          L_it++) {
00376       L_field_def = *L_it;
00377       if (!(L_field_def->m_expr_list)->empty()) {
00378         (L_field_def->m_expr_list)->erase((L_field_def->m_expr_list)->begin(),
00379                                           (L_field_def->m_expr_list)->end());
00380       }
00381     }
00382     m_body_fields_list->erase(m_body_fields_list->begin(),
00383                               m_body_fields_list->end());
00384     DELETE_VAR (m_body_fields_list) ;
00385   }
00386 
00387 
00388   // delete m_field_body_name_map
00389   if (m_fields_name_map != NULL) {
00390     if (!m_fields_name_map->empty()) {
00391       m_fields_name_map->erase(m_fields_name_map->begin(),
00392                                m_fields_name_map->end());
00393     }
00394     DELETE_VAR(m_fields_name_map);
00395   }
00396 
00397   if (m_header_fields_dico != NULL ) {
00398     if (!m_header_fields_dico->empty()) {
00399       m_header_fields_dico->erase(m_header_fields_dico->begin(),
00400                                   m_header_fields_dico->end());
00401     }
00402     DELETE_VAR(m_header_fields_dico);
00403   }
00404 
00405   if (m_message_names_table != NULL) {
00406     for(L_i = 0; L_i < m_nb_message_names; L_i++) {
00407       FREE_TABLE(m_message_names_table[L_i]);
00408     }
00409     FREE_TABLE(m_message_names_table);
00410   }
00411 
00412   if (!m_message_decode_map->empty()) {
00413     m_message_decode_map
00414       ->erase(m_message_decode_map->begin(),
00415               m_message_decode_map->end());
00416   }
00417   DELETE_VAR(m_message_decode_map);
00418 
00419   if ((m_names_fields != NULL) && (m_nb_fields_desc_table > 0)) {
00420     for(L_i = 0; L_i < m_nb_fields_desc_table ; L_i++) {
00421       FREE_TABLE(m_names_fields[L_i]);
00422     }
00423     FREE_TABLE(m_names_fields);
00424   }
00425 
00426   FREE_TABLE(m_fields_desc_table);
00427   m_nb_fields_desc_table        = 0    ;
00428 
00429   if (m_value_sessions_table_size != 0) {
00430     FREE_TABLE(m_value_sessions_table);
00431     m_value_sessions_table_size = 0 ;
00432   }
00433 
00434   m_body_separator              = NULL ;
00435   m_body_separator_size         = 0    ;
00436   m_field_separator             = NULL ;
00437   m_field_separator_size        = 0    ;
00438 
00439   m_field_body_separator        = NULL ;
00440   m_field_body_separator_size   = 0    ;
00441 
00442   if (m_field_body_separator_size != 0 ) {
00443     FREE_TABLE(m_field_body_separator)   ;
00444     m_field_body_separator_size   = 0    ;
00445   }
00446 
00447   m_message_type_field_id       = -1   ;
00448   m_session_id_id               = -1   ;
00449   m_session_method              = &C_MessageText::getSessionFromField ;
00450 
00451 
00452   destroy_methods();
00453 
00454   m_nb_methods                  = 0    ;
00455   m_methods                     = NULL ; 
00456 
00457   if(! m_message_name_list -> empty()) {
00458     T_NameAndIdList::iterator  L_elt_it ;
00459     T_NameAndId                L_elt    ;
00460 
00461     for(L_elt_it=m_message_name_list->begin();
00462         L_elt_it != m_message_name_list->end();
00463         L_elt_it++) {
00464       L_elt = *L_elt_it ;
00465       FREE_TABLE(L_elt.m_name);
00466     }
00467     m_message_name_list -> erase (m_message_name_list->begin(), m_message_name_list->end());
00468   }
00469   DELETE_VAR (m_message_name_list) ;
00470 
00471   DELETE_VAR(m_stats);
00472 
00473 
00474   if((m_def_method_list) && (!m_def_method_list -> empty())) {
00475     m_def_method_list -> erase (m_def_method_list->begin(), m_def_method_list->end());
00476   }
00477   DELETE_VAR (m_def_method_list) ;
00478   
00479   if((m_def_method_extern_list) && (!m_def_method_extern_list -> empty())) {
00480   
00481     m_def_method_extern_list-> erase (m_def_method_extern_list->begin(), 
00482                                       m_def_method_extern_list->end());
00483   }
00484   DELETE_VAR (m_def_method_extern_list) ;
00485 
00486   if (m_nb_method_external_table) {
00487     FREE_TABLE(m_method_external_table);
00488     m_nb_method_external_table  = 0    ;
00489   }
00490 
00491   m_config_value_list    = NULL ;
00492 }
00493 
00494 
00495 C_MessageFrame* C_ProtocolText::decode_message(unsigned char *P_buffer, 
00496                                                size_t        *P_size, 
00497                                                C_ProtocolFrame::T_pMsgError P_error) {
00498 
00499   C_MessageText                   *L_msg             ;
00500   int                              L_msg_id          ;
00501 
00502   L_msg = (C_MessageText*)create_new_message(NULL);
00503 
00504 
00505   (*P_size) = L_msg -> decode (P_buffer, *P_size, P_error);
00506 
00507   switch (*P_error) {
00508   case C_ProtocolFrame::E_MSG_OK: {
00509 
00510     if (m_stats) {
00511       L_msg_id = L_msg -> get_id_message () ;
00512       m_stats->updateStats (E_MESSAGE,
00513                             E_RECEIVE,
00514                             L_msg_id);
00515     }
00516 
00517     // GEN_LOG_EVENT(LOG_LEVEL_MSG, 
00518     //    "Received [" << *L_msg << "]");
00519   }
00520     break ;
00521   case C_ProtocolFrame::E_MSG_ERROR_DECODING_SIZE_LESS:
00522     DELETE_VAR(L_msg);
00523     break ;
00524   default:
00525     GEN_ERROR(E_GEN_FATAL_ERROR, 
00526               "==> Unrecognized message received") ;
00527     DELETE_VAR(L_msg);
00528     break ;
00529   }
00530   return (L_msg);
00531 }
00532 
00533 
00534 C_ProtocolFrame::T_MsgError 
00535 C_ProtocolText::encode_message(C_MessageFrame *P_msg,
00536                                unsigned char  *P_buffer,
00537                                size_t         *P_buffer_size) {
00538 
00539   C_ProtocolFrame::T_MsgError      L_error = C_ProtocolFrame::E_MSG_ERROR_ENCODING ;
00540   C_MessageText                   *L_msg   = dynamic_cast<C_MessageText*>(P_msg)   ;
00541 
00542   int                              L_msg_id                                        ;
00543 
00544 
00545   if (L_msg != NULL) {
00546 
00547     L_msg_id = L_msg -> get_id_message () ;
00548     L_error = L_msg->encode(P_buffer, P_buffer_size);
00549     if (L_error == C_ProtocolFrame::E_MSG_OK ) {
00550       if (m_stats) {
00551         m_stats->updateStats(E_MESSAGE,
00552                              E_SEND,
00553                              L_msg_id);
00554       }
00555       GEN_LOG_EVENT(LOG_LEVEL_MSG, 
00556                     "Send [" << *L_msg << "]");
00557     }
00558   }
00559   
00560   return (L_error);
00561 }
00562 
00563 void C_ProtocolText::log_buffer(char* P_header, 
00564                                 unsigned char *P_buffer, 
00565                                 size_t P_buffer_size) {
00566 
00567   if (genTraceLevel & gen_mask_table[LOG_LEVEL_BUF]) {
00568     static char L_hexa_buf [50] ;
00569     const  size_t L_cNum = 16 ;
00570     size_t L_i, L_nb ;
00571     unsigned char*L_cur ;
00572     size_t L_buffer_size = P_buffer_size ;
00573     
00574     GEN_LOG_EVENT(LOG_LEVEL_BUF,
00575                   "Buffer " << P_header << " size ["
00576                   << L_buffer_size
00577                   << "]");
00578     L_nb = L_buffer_size / L_cNum ;
00579     L_cur = P_buffer ;
00580     for (L_i = 0 ; L_i < L_nb; L_i++) {
00581       pretty_binary_buffer(L_cur, L_cNum, L_hexa_buf);
00582       GEN_LOG_EVENT_NO_DATE(LOG_LEVEL_BUF, 
00583                             L_hexa_buf) ;
00584       L_cur += L_cNum ;
00585     }
00586     L_nb = L_buffer_size % L_cNum ;
00587     if (L_nb != 0) {
00588       pretty_binary_buffer(L_cur, L_nb, L_hexa_buf);
00589       GEN_LOG_EVENT_NO_DATE(LOG_LEVEL_BUF, 
00590                                 L_hexa_buf) ;
00591     }
00592   }
00593 }
00594 
00595 char* C_ProtocolText::message_name() {
00596   return (m_header_name);
00597 }
00598 
00599 char* C_ProtocolText::message_component_name() {
00600   return (m_body_name);
00601 }
00602 
00603 T_pNameAndIdList C_ProtocolText::message_name_list     () {
00604 
00605   T_NameAndId             L_elt                   ;
00606   int                     L_i                     ;
00607   
00608   
00609   for (L_i = 0 ; L_i <  m_nb_message_names ; L_i ++) {
00610     ALLOC_TABLE(L_elt.m_name,
00611                 char*, sizeof(char),
00612                 (strlen(m_message_names_table[L_i])+1));
00613     
00614     strcpy(L_elt.m_name, m_message_names_table[L_i]);
00615     L_elt.m_id = L_i ;
00616     m_message_name_list->push_back(L_elt);
00617   }
00618   
00619   return (m_message_name_list);
00620 }
00621 
00622 T_pNameAndIdList C_ProtocolText::message_component_name_list    () {
00623 
00624   return (NULL) ;
00625 }
00626 
00627 
00628 char* C_ProtocolText::message_name(int P_id) {
00629   if ((P_id < 0) || (P_id > m_nb_message_names)) {
00630     return (NULL);
00631   } else {
00632     return (m_message_names_table[P_id]);
00633   }
00634 }
00635 
00636 
00637 int C_ProtocolText::find_field(char *P_name) {
00638   int L_id ;
00639   
00640   for(L_id = 0 ; L_id < m_nb_fields_desc_table; L_id++) {
00641     if (strcmp(m_fields_desc_table[L_id]->m_name, P_name)==0) {
00642       return (L_id);
00643     }
00644   }
00645   return (-1);
00646 }
00647 
00648 
00649 T_TypeType C_ProtocolText::get_field_type (int P_id,
00650                                            int P_sub_id) {
00651   T_TypeType L_type = E_TYPE_STRING ;
00652   return (L_type);
00653 }
00654 
00655 bool C_ProtocolText::check_sub_entity_needed (int P_id) {
00656   return (false);
00657 }
00658 
00659 bool C_ProtocolText::find_present_session (int P_msg_id,int P_id) {
00660   return (true);
00661 }
00662 
00663 
00664 C_MessageFrame* C_ProtocolText::create_new_message (C_MessageFrame *P_msg) {
00665   C_MessageText *L_dest = NULL ;
00666 
00667   C_MessageText *L_source = dynamic_cast<C_MessageText *>(P_msg) ;
00668   
00669   if (L_source != NULL) {
00670     // L_source->dump(std::cerr);
00671     NEW_VAR(L_dest, C_MessageText(*L_source));
00672   } else {
00673     NEW_VAR(L_dest, C_MessageText(this));
00674   }
00675 
00676   return (L_dest);
00677 }
00678 
00679 
00680 int C_ProtocolText::analyze_message_scen (C_XmlData            *P_data, 
00681                                            T_pCDATAValueList     P_cdata_value_list) {
00682 
00683   int                         L_ret                = 0    ;
00684   T_pXmlData_List             L_data_scen          = NULL ;
00685   T_XmlData_List::iterator    L_it                        ;
00686   T_CDATAValue                L_cdata_val                 ;
00687 
00688   L_data_scen = P_data->get_sub_data();
00689   if (L_data_scen == NULL){
00690     GEN_ERROR(E_GEN_FATAL_ERROR, "C_ProtocolText::analyze_message_scen() "
00691               << "no value found for this message [" << m_header_name << "]");
00692     L_ret = -1 ;
00693   } 
00694 
00695   if (L_ret != -1) {
00696     for (L_it = L_data_scen->begin();
00697          L_it != L_data_scen->end();
00698          L_it++) {
00699       L_cdata_val.m_value = (*L_it)->find_value((char*)"value");
00700       P_cdata_value_list->push_back(L_cdata_val);
00701     }
00702   }
00703 
00704   return (L_ret);
00705 
00706 }
00707 
00708 C_MessageFrame* C_ProtocolText::create_new_message(void                *P_xml, 
00709                                                    T_pInstanceDataList  P_list,
00710                                                    int                 *P_nb_value) {
00711 
00712   C_MessageText                  *L_msg                = NULL               ;
00713   C_XmlData                      *P_data               = (C_XmlData *)P_xml ;
00714   int                             L_ret                = 0                  ;
00715   T_pCDATAValueList               L_list_value         = NULL               ;
00716 
00717   T_CDATAValueList::iterator      L_cdata_it                                ;
00718   char                           *L_header             = NULL               ;
00719   char                           *L_body               = NULL               ;
00720   int                             L_i                  = 0                  ;
00721 
00722   NEW_VAR(L_list_value, T_CDATAValueList());
00723 
00724   // analyze message from scenario 
00725   L_ret = analyze_message_scen(P_data, L_list_value) ;
00726 
00727   if (L_ret != -1) {
00728     if ((L_list_value != NULL ) && (!(L_list_value)->empty())) {  
00729       for (L_cdata_it = (L_list_value)->begin();
00730            L_cdata_it != (L_list_value)->end();
00731            L_cdata_it++) {
00732         if (L_i == 0) {
00733           L_header = (*L_cdata_it).m_value ;
00734         } else if (L_i == 1) {
00735           L_body = (*L_cdata_it).m_value ;
00736         } else {
00737           // To Do if we have many value
00738           break ;
00739         }
00740         L_i++ ;
00741       }
00742     } else {
00743       GEN_ERROR(E_GEN_FATAL_ERROR, "C_ProtocolText::create_new_message() "
00744                 << "no value found for this message [" << m_header_name << "]");
00745       L_ret = -1 ;
00746     }
00747   }
00748     
00749   if (L_ret != -1) {
00750     L_msg = create(this,L_header,L_body);
00751     if (L_msg == NULL) {
00752       GEN_ERROR(E_GEN_FATAL_ERROR, "C_ProtocolText::create_new_message() "
00753                 << "Error while creating message with header [" 
00754                 << L_header 
00755                 << "]: message not in dictionary?");
00756     }
00757   }
00758   
00759   // delete L_list_value 
00760   if (L_list_value != NULL ) {
00761     if (!L_list_value->empty()) {
00762       L_list_value->erase(L_list_value->begin(),
00763                           L_list_value->end());
00764     }
00765     DELETE_VAR(L_list_value);
00766   }
00767 
00768   return (L_msg);
00769 
00770 }
00771 
00772 
00773 C_ProtocolText::T_FieldHeaderList* 
00774 C_ProtocolText::analyze_header_value (C_XmlData          *P_data, 
00775                                       T_FieldHeaderList  *P_fielddef_header_list,
00776                                       int                *P_ret) {
00777 
00778   char                     *L_value              ;
00779   T_FieldHeader             L_fielddef_header    ;
00780 
00781   *P_ret = 0 ;
00782   
00783   GEN_DEBUG(1, "C_ProtocolText::analyze_header_value() start");
00784 
00785   // ctrl fields 
00786   L_value = P_data->find_value((char *)"name");
00787 
00788   GEN_DEBUG(1, "C_ProtocolText::analyze_header_value() "
00789             << "[" << m_header_name << "] = [" << L_value << "]");
00790 
00791   if (L_value == NULL) {
00792     GEN_ERROR(E_GEN_FATAL_ERROR,
00793               "name definition value is mandatory for this section");
00794     *P_ret = -1 ;
00795   } else {
00796     L_fielddef_header.m_name = L_value ;
00797   }
00798 
00799   if (*P_ret != -1) {
00800     if (P_fielddef_header_list == NULL) {
00801       NEW_VAR(P_fielddef_header_list, T_FieldHeaderList());
00802     }
00803     P_fielddef_header_list->push_back(L_fielddef_header);
00804   }
00805 
00806   GEN_DEBUG(1, "C_ProtocolText::analyze_header_value() end");
00807 
00808   return (P_fielddef_header_list);
00809 }
00810 
00811 
00812 int C_ProtocolText::analyze_dictionnary_from_xml (C_XmlData *P_def) {
00813   
00814   int                       L_ret = 0                      ;
00815   int                       L_i                            ;
00816   
00817   T_pXmlData_List           L_subListDico                  ;  
00818   T_XmlData_List::iterator  L_listItDico                   ; 
00819 
00820 
00821   GEN_DEBUG(1, "C_ProtocolText::analyze_dictionnary_from_xml() start");
00822 
00823   // analyze session-id
00824   L_ret = analyze_sessions_id_from_xml(P_def);
00825   
00826   if (L_ret != -1) {
00827     L_subListDico = P_def->get_sub_data();
00828     if (L_subListDico != NULL) {
00829       for (L_listItDico = L_subListDico->begin() ;
00830            L_listItDico != L_subListDico->end() ;
00831            L_listItDico++) {
00832         if (((*L_listItDico)->get_name() != NULL) && 
00833             (strcmp((*L_listItDico)->get_name(), m_header_name) == 0)) {
00834           m_header_fields_dico = analyze_header_value(*L_listItDico, m_header_fields_dico, &L_ret);
00835           if (L_ret == -1) break ;
00836         }
00837         
00838         // TO DO Add m_body_name...
00839         //  if (((*L_listItDico)->get_name() != NULL) && 
00840         //              (strcmp((*L_listItDico)->get_name(), m_body_name) == 0)) {
00841         //   m_header_fields_dico = analyze_body_value(*L_listItDico, m_header_fields_dico, &L_ret);
00842         //   if (L_ret == -1) break ;
00843         // }
00844 
00845       } // for (L_listItDico = L_subListDico->begin()
00846     } // if (L_subListDico != NULL)
00847   }
00848 
00849   if (L_ret != -1) {
00850     if (!m_header_fields_dico->empty()) {
00851       m_nb_message_names =  m_header_fields_dico->size();
00852       ALLOC_TABLE(m_message_names_table, char**, sizeof(char*),m_nb_message_names);
00853       for (L_i = 0; L_i < m_nb_message_names ; L_i++) {
00854         m_message_names_table[L_i] = NULL ;
00855       } 
00856     } else {
00857       GEN_ERROR(E_GEN_FATAL_ERROR, 
00858                 "messages not found in dictionnary"); 
00859       L_ret = -1 ;
00860     }
00861   }
00862 
00863   GEN_DEBUG(1, "C_ProtocolText::analyze_dictionnary_from_xml() end");
00864 
00865   return (L_ret);
00866 }
00867 
00868 int C_ProtocolText::analyze_sessions_id_from_xml (C_XmlData *P_def) {
00869 
00870 
00871   int                                     L_ret = 0                 ;
00872   C_XmlData::T_pXmlField_List             L_fields_list             ;
00873   C_XmlData::T_XmlField_List::iterator    L_fieldIt                 ;
00874 
00875   T_ManagementSessionTextId               L_management_session      ;
00876   T_ManagementSessionTextIdList           L_list_management_session ;
00877   T_ManagementSessionTextIdList::iterator L_it                      ;
00878 
00879   char                                   *L_name_value = NULL       ;
00880   int                                     L_msg_id_id               ;
00881   int                                     L_id                      ;
00882   bool                                    L_use_open_id = false     ;
00883   int                                     L_session_id_id           ;
00884 
00885   GEN_DEBUG(1, "C_ProtocolText::analyze_sessions_id_from_xml() start");  
00886 
00887   L_fields_list = P_def->get_fields();
00888   if (!L_fields_list->empty()) {
00889     for (L_fieldIt  = L_fields_list->begin() ;
00890         L_fieldIt != L_fields_list->end() ;
00891         L_fieldIt++) {
00892 
00893       if (strcmp((*L_fieldIt)->get_name() , (char*)"session-method") == 0 ) {
00894         L_name_value = (*L_fieldIt)->get_value();
00895         if (L_name_value == NULL) {
00896           GEN_ERROR(E_GEN_FATAL_ERROR,
00897                     (*L_fieldIt)->get_name()
00898                     << " value is mandatory for ["
00899                     << m_header_name 
00900                     << "] section");
00901           L_ret = -1 ;
00902           break ;
00903         }
00904         if (strcmp(L_name_value, (char*)"open-id") == 0 ) {
00905           L_use_open_id = true ;
00906           L_session_id_id = new_id();
00907           set_session_id_id(L_session_id_id);
00908           use_open_id();
00909         } else if (strcmp(L_name_value, (char*)"field") != 0 ) {
00910           GEN_ERROR(E_GEN_FATAL_ERROR,
00911                     (*L_fieldIt)->get_name()
00912                     << " value is mandatory for ["
00913                     << m_header_name 
00914                     << "] section");
00915           L_ret = -1 ;
00916           break ;
00917         }
00918       }
00919       
00920       if((strcmp((*L_fieldIt)->get_name() , (char*)"session-id") == 0 ) || 
00921          (strcmp((*L_fieldIt)->get_name() , (char*)"out-of-session-id") == 0 )) {
00922 
00923         if (L_use_open_id == false) {
00924 
00925           L_name_value = (*L_fieldIt)->get_value();
00926           if (L_name_value == NULL) {
00927             GEN_ERROR(E_GEN_FATAL_ERROR,
00928                       (*L_fieldIt)->get_name()
00929                       << " is mandatory for ["
00930                       << m_header_name 
00931                       << "] section");
00932             L_ret = -1 ;
00933             break ;
00934           }
00935           
00936           // retrieve informations for session_id
00937           L_msg_id_id = find_field_id (L_name_value) ;
00938           GEN_DEBUG(1, "C_ProtocolText::analyze_sessions_id_from_xml() " 
00939                     << "L_msg_id_id is " << L_msg_id_id );
00940           if (L_msg_id_id != -1) {
00941             L_management_session.m_msg_id_id = L_msg_id_id ;
00942             L_management_session.m_msg_id_value_type = E_TYPE_STRING ;
00943             L_list_management_session.push_back(L_management_session);
00944           } else {
00945             GEN_ERROR(E_GEN_FATAL_ERROR,
00946                       "No definition found for "
00947                       << (*L_fieldIt)->get_name() 
00948                       << " ["
00949                       << L_name_value << "]");
00950             L_ret = -1 ;
00951             break ;
00952           }
00953         } else { // if L_use_open_id == true
00954           GEN_ERROR(E_GEN_FATAL_ERROR,
00955                     "Definition found for "
00956                     << (*L_fieldIt)->get_name() 
00957                     << " with session-method=open-id");
00958             L_ret = -1 ;
00959             break ;
00960         }
00961         
00962       } // if((strcmp((*L_fieldIt)..)))
00963     } // for (L_fieldIt  =...)
00964 
00965     if (L_ret != -1) {
00966       if (L_use_open_id == false) {
00967 
00968         if (!L_list_management_session.empty()) {
00969           m_value_sessions_table_size = L_list_management_session.size() ;
00970           ALLOC_TABLE(m_value_sessions_table,
00971                       T_pManagementSessionTextId,
00972                       sizeof(T_ManagementSessionTextId),
00973                       m_value_sessions_table_size);
00974           
00975           L_id = 0 ;
00976           for (L_it  = L_list_management_session.begin();
00977                L_it != L_list_management_session.end()  ;
00978                L_it++) {
00979             m_value_sessions_table[L_id] = *L_it ;
00980             L_id ++ ;
00981           }
00982           
00983           L_list_management_session.erase(L_list_management_session.begin(),
00984                                           L_list_management_session.end());
00985           
00986 //            for (L_id = 0 ; L_id < m_value_sessions_table_size ; L_id++) {
00987 //              std::cerr << "m_value_sessions_table[" 
00988 //                        << L_id 
00989 //                        << "]" 
00990 //                        << m_value_sessions_table[L_id].m_msg_id_id
00991 //                        << " " 
00992 //                        << m_value_sessions_table[L_id].m_msg_id_value_type
00993 //                        << std::endl;
00994             
00995 //            }
00996           
00997         } else {
00998           GEN_ERROR(E_GEN_FATAL_ERROR,
00999                     "No session-id nor out-of-session definition found"); 
01000           L_ret = -1 ;
01001         }
01002       } // if L_use_open_id == false
01003     } // if (L_ret != -1)
01004   } else { // if (!L_fields_list->empty())
01005     GEN_ERROR(E_GEN_FATAL_ERROR,
01006               "No session-id nor out-of-session found in dictionary definition");
01007     L_ret = -1 ;
01008   }
01009   
01010   
01011   GEN_DEBUG(1, "C_ProtocolBinaryBodyNotInterpreted::analyze_sessions_id_from_xml() end");
01012 
01013   return (L_ret);
01014 }
01015 
01016 int C_ProtocolText::analyze_from_xml (C_XmlData *P_def, 
01017                                       T_FieldDefList *P_fielddef_list,
01018                                       char **P_section_name,
01019                                       char **P_type,
01020                                       bool P_header_body) {
01021 
01022   int                                  L_ret = 0                  ;
01023   T_pXmlData_List                      L_fielddef_xml_list = NULL ;
01024   C_XmlData                           *L_fielddef = NULL          ;
01025   T_XmlData_List::iterator             L_fielddef_it              ;
01026   T_pXmlData_List                      L_regexp_xml_list = NULL ;
01027   C_XmlData                           *L_regexp = NULL          ;
01028   T_XmlData_List::iterator             L_regexp_it              ;
01029 
01030   T_RegExpStr                          *L_regexp_data            ;
01031   T_FieldDef                           *L_fielddef_data         ;
01032 
01033 
01034   GEN_DEBUG(1, "C_ProtocolText::analyze_from_xml() start");
01035 
01036   *P_section_name = P_def->find_value((char *)"name");
01037   if (*P_section_name == NULL) {
01038     GEN_ERROR(E_GEN_FATAL_ERROR,
01039               "name definition value is mandatory for header section");
01040     L_ret = -1 ;
01041   }
01042 
01043   *P_type   = P_def->find_value((char *)"type");
01044   if (*P_type == NULL) {
01045     GEN_ERROR(E_GEN_FATAL_ERROR,
01046               "type definition value is mandatory for header section");
01047     L_ret = -1 ;
01048   }
01049 
01050   if (L_ret != -1) {
01051 
01052     L_fielddef_xml_list = P_def->get_sub_data() ;
01053     for(L_fielddef_it  = L_fielddef_xml_list->begin() ;
01054         L_fielddef_it != L_fielddef_xml_list->end() ;
01055         L_fielddef_it++) {
01056       L_fielddef = *L_fielddef_it ;
01057       if (strcmp(L_fielddef->get_name(), (char*)"fielddef") == 0) {
01058         NEW_VAR(L_fielddef_data, T_FieldDef());
01059         NEW_VAR(L_fielddef_data->m_expr_list, T_ExpStrLst());
01060         L_fielddef_data->m_name = L_fielddef->find_value((char*)"name") ;
01061         if (L_fielddef_data->m_name == NULL) {
01062           GEN_ERROR(E_GEN_FATAL_ERROR, 
01063                     "fielddef name value is mandatory");
01064           L_ret = -1 ;
01065           break ;
01066         } else {
01067           L_fielddef_data->m_format = L_fielddef->find_value((char*)"format") ;
01068           L_regexp_xml_list = L_fielddef->get_sub_data() ;
01069           for(L_regexp_it  = L_regexp_xml_list->begin() ;
01070               L_regexp_it != L_regexp_xml_list->end() ;
01071               L_regexp_it++) {
01072 
01073             L_regexp = *L_regexp_it ;
01074             if (strcmp(L_regexp->get_name(), (char*)"regexp") == 0) {
01075               
01076               NEW_VAR(L_regexp_data, T_RegExpStr());
01077 
01078               L_regexp_data->m_name = L_regexp->find_value((char*)"name") ;
01079               L_regexp_data->m_expr = L_regexp->find_value((char*)"expr") ;
01080 
01081               // default values ?
01082               L_regexp_data->m_nb_match = (L_regexp->find_value((char*)"nbexpr") == NULL) ?
01083                 1 : atoi(L_regexp->find_value((char*)"nbexpr")) ;
01084               L_regexp_data->m_sub_match = (L_regexp->find_value((char*)"subexpr") == NULL) ? 
01085                 0 : atoi(L_regexp->find_value((char*)"subexpr")) ;
01086               L_regexp_data->m_line = (L_regexp->find_value((char*)"line") == NULL) ?
01087                 -1 : atoi(L_regexp->find_value((char*)"line")) ;
01088 
01089               (L_fielddef_data->m_expr_list)->push_back(L_regexp_data);
01090               
01091             } // if (strcmp(L_regexp->get_name(), (char*)"regexp") == 0)
01092           } // for(L_regexp_it...
01093 
01094           P_fielddef_list->push_back(L_fielddef_data);
01095         }
01096       }
01097     }
01098   } // if (L_ret != -1)
01099 
01100   GEN_DEBUG(1, "C_ProtocolText::analyze_from_xml() end");
01101   return (L_ret);
01102 }
01103 
01104 
01105 C_ProtocolText::T_pFieldDesc 
01106 C_ProtocolText::check_field(T_pFieldDef P_field_def, bool P_header) {
01107 
01108   T_pFieldDesc                  L_ret = NULL ;
01109   T_FieldNameMap::iterator      L_it ;
01110   size_t                        L_size = 0 ;
01111 
01112   T_ExpStrLst::iterator         L_list_it ;
01113   T_pRegExpStr                  L_reg_str ;
01114   int                           L_error_comp = 0 ;
01115   char                         *L_value = NULL ;
01116   int                           L_start = -1 ;
01117   int                           L_end = -1 ;
01118   int                           L_result = -1 ;
01119 
01120 
01121   GEN_DEBUG(1, "C_ProtocolText::check_field() start");
01122 
01123   L_it = m_fields_name_map->find(T_FieldNameMap::key_type(P_field_def->m_name));
01124   if (L_it != m_fields_name_map->end()) {
01125     GEN_ERROR(E_GEN_FATAL_ERROR, "field [" << P_field_def->m_name << "] already defined");
01126     return (L_ret);
01127   }
01128 
01129   ALLOC_VAR(L_ret, T_pFieldDesc, sizeof(T_FieldDesc));
01130 
01131   L_size = strlen(P_field_def->m_name) ;
01132   ALLOC_TABLE(L_ret->m_name, char*, sizeof(char), L_size+1);
01133   strcpy(L_ret->m_name, P_field_def->m_name);
01134   
01135   L_ret->m_id = new_id();
01136   L_ret->m_header_body = P_header ;
01137   L_ret->m_format = NULL ;
01138 
01139   NEW_VAR(L_ret->m_reg_exp_lst, T_RegExpLst());
01140 
01141   for (L_list_it = P_field_def->m_expr_list->begin() ;
01142        L_list_it != P_field_def->m_expr_list->end();
01143        L_list_it ++) {
01144     L_reg_str = *L_list_it ;
01145     C_RegExp *L_cRegExp = NULL ;
01146     NEW_VAR(L_cRegExp, C_RegExp(L_reg_str->m_expr, &L_error_comp,
01147                                 L_reg_str->m_nb_match,
01148                                 L_reg_str->m_sub_match,
01149                                 L_reg_str->m_line));
01150     if (L_error_comp == 0) {
01151       (L_ret->m_reg_exp_lst)->push_back(L_cRegExp);
01152 
01153       if (P_field_def->m_format) {
01154         if (L_value == NULL) {
01155           L_value = L_cRegExp->execute(P_field_def->m_format);
01156           if (L_value != NULL) {
01157             if (strcmp(L_value, (char*)"$(field-value)") != 0) {
01158               // format ok found => create a T_pValueData for 
01159               FREE_TABLE(L_value);
01160             } else {
01161               L_result = L_cRegExp->execute(P_field_def->m_format,
01162                                             &L_start, 
01163                                             &L_end);
01164             }
01165           }
01166         }
01167       }
01168 
01169     } else {
01170       GEN_ERROR(E_GEN_FATAL_ERROR, "In field [" 
01171                 << P_field_def->m_name 
01172                 << "] bad regular expression ["
01173                 << L_reg_str->m_expr << "]");
01174       return (NULL);
01175     }
01176   }
01177 
01178   if (P_field_def->m_format != NULL) {
01179     if (L_value == NULL) {
01180       GEN_ERROR(E_GEN_FATAL_ERROR, "In field [" 
01181                 << P_field_def->m_name 
01182                 << "] the format expression ["
01183                 << P_field_def->m_format
01184                 << "] is not matched by the regexp ["
01185                 << L_reg_str->m_expr
01186                 << "]");
01187       return (NULL);
01188     } else {
01189       ALLOC_VAR(L_ret->m_format, T_pValueData, sizeof(T_ValueData));
01190       L_ret->m_format->m_type = E_TYPE_STRING ;
01191       L_ret->m_format->m_value.m_val_binary.m_size = 0 ;
01192       L_ret->m_format->m_value.m_val_binary.m_value = NULL ;
01193 
01194       *(L_ret->m_format) = valueFromString(P_field_def->m_format,
01195                                         E_TYPE_STRING,
01196                                         L_result);
01197       if (L_result == -1) {
01198         GEN_ERROR(E_GEN_FATAL_ERROR, "In field [" 
01199                   << P_field_def->m_name 
01200                   << "] bad format expression");
01201         FREE_TABLE(L_value);
01202         return (NULL);
01203       } 
01204 
01205       L_ret->m_format_value_start = L_start ;
01206       L_ret->m_format_value_end = L_end ;
01207 
01208       FREE_TABLE(L_value);
01209     }
01210   }
01211 
01212 
01213 
01214   m_fields_name_map->insert(T_FieldNameMap::value_type(P_field_def->m_name, L_ret));
01215   
01216   GEN_DEBUG(1, "C_ProtocolText::check_field() end");
01217 
01218   return (L_ret);
01219 }
01220 
01221 
01222 
01223 int C_ProtocolText::xml_interpretor(C_XmlData *P_def,
01224                                     char **P_name,
01225                                     T_pConfigValueList P_config_value_list) {
01226 
01227   int                           L_ret          = 0           ;
01228   T_pXmlData_List               L_data         = NULL        ;
01229   T_XmlData_List::iterator      L_it                         ;
01230   C_XmlData                    *L_dataDico     = NULL        ;
01231   bool                          L_headerFound  = false       ;
01232 
01233   T_FieldDefList::iterator      L_fielddef_it                ;
01234   T_pFieldDesc                  L_field_desc   = NULL        ;
01235   int                           L_id                         ;
01236   T_FieldHeaderList::iterator   L_message_it                 ;
01237   int                           L_i                          ;
01238   T_DefMethodExternList::iterator L_def_method_extern_it     ;
01239   T_DefMethodExtern               L_defmethod_extern_data    ;
01240 
01241   T_pParamDefList               L_config_params_dico = NULL  ;
01242   T_ParamDefList::iterator      L_config_params_dico_it      ;
01243   
01244   GEN_DEBUG(1, "C_ProtocolText::xml_interpretor() start");
01245 
01246   NEW_VAR(m_fields_name_map, T_FieldNameMap());
01247   m_fields_name_map->clear();
01248 
01249   L_data = P_def->get_sub_data() ;
01250   if (L_data == NULL) {
01251     GEN_ERROR(E_GEN_FATAL_ERROR, "No protocol definition");
01252     L_ret = -1 ;
01253   } 
01254 
01255   if (L_ret != -1) {
01256     for (L_it = L_data->begin();
01257          L_it != L_data->end();
01258          L_it++) {
01259       if (strcmp((*L_it)->get_name(), "configuration-parameters") == 0) {
01260         NEW_VAR(L_config_params_dico, T_ParamDefList());
01261         L_ret = xml_configuration_parameters (*L_it, L_config_params_dico);
01262         break ;
01263       }
01264     }
01265 
01266     if (L_ret != -1) {
01267       if ((L_config_params_dico != NULL ) && (!L_config_params_dico->empty())) {
01268         if (L_config_params_dico->size() > 0 ) {
01269           // ctrl between parameters from config and dico
01270           for (L_config_params_dico_it = L_config_params_dico->begin();
01271                L_config_params_dico_it != L_config_params_dico->end();
01272                L_config_params_dico_it++) {
01273             L_ret = update_config_params (*L_config_params_dico_it, P_config_value_list) ;
01274             if (L_ret == -1) { break;}
01275           }
01276         }
01277       } else {
01278         GEN_WARNING("No definition found for configuration parameters definition for protocol ["
01279                     << *P_name << "]");
01280       }
01281     } else {
01282       GEN_ERROR(E_GEN_FATAL_ERROR, 
01283                 "Error in configuration parameters definition found for protocol ["
01284                 << *P_name << "]");
01285     }
01286     DELETE_VAR(L_config_params_dico);
01287   }
01288 
01289   if (L_ret != -1) {
01290     for (L_it = L_data->begin();
01291          L_it != L_data->end();
01292          L_it++) {
01293       if (strcmp((*L_it)->get_name(), "body-method") == 0) {
01294         NEW_VAR(m_def_method_list, T_DefMethodList());
01295         L_ret = analyze_body_method_from_xml (*L_it,m_def_method_list); 
01296         break ;
01297       }
01298     }
01299   }
01300 
01301   if (L_ret != -1) {
01302     for (L_it = L_data->begin();
01303          L_it != L_data->end();
01304          L_it++) {
01305       if (strcmp((*L_it)->get_name(), "external-method") == 0) {
01306         NEW_VAR(m_def_method_extern_list, T_DefMethodExternList());
01307         L_ret = analyze_extern_method_from_xml (*L_it,m_def_method_extern_list); 
01308         break ;
01309       }
01310     }
01311 
01312     if (L_ret != -1) {
01313       L_id = 0;
01314       if((m_def_method_extern_list) &&
01315          (!m_def_method_extern_list-> empty()) && 
01316          (m_def_method_extern_list->size()> 0)) {
01317        
01318         m_nb_method_external_table = m_def_method_extern_list->size() ;
01319 
01320         ALLOC_TABLE(m_method_external_table,
01321                     T_pDefMethodExternal,
01322                     sizeof(T_DefMethodExternal),
01323                     m_nb_method_external_table);
01324   
01325         for (L_def_method_extern_it = m_def_method_extern_list->begin();
01326              L_def_method_extern_it != m_def_method_extern_list->end();
01327              L_def_method_extern_it++) {
01328          
01329           L_defmethod_extern_data = *L_def_method_extern_it ;
01330 
01331           m_method_external_table[L_id].m_id = L_id ;
01332           m_method_external_table[L_id].m_method
01333             = create_external_method (L_defmethod_extern_data.m_param) ;
01334           L_id++ ;
01335         }
01336       }
01337     }
01338   }
01339 
01340   if (L_ret != -1) {
01341     for(L_it  = L_data->begin() ;
01342         L_it != L_data->end() ;
01343         L_it++) {
01344       
01345       // Message Header definition
01346       if (strcmp((*L_it)->get_name(), "header") ==0) {
01347         L_headerFound = true ;
01348         NEW_VAR(m_header_fields_list, T_FieldDefList());
01349         L_ret = analyze_from_xml (*L_it, 
01350                                   m_header_fields_list,
01351                                   &m_header_name,
01352                                   &m_header_type,
01353                                   false) ;
01354         if (L_ret == -1) break ;
01355       }
01356     }
01357     
01358     if (L_headerFound == false) {
01359       GEN_ERROR(E_GEN_FATAL_ERROR, "No header definition found for protocol ["
01360                 << *P_name << "]");
01361       L_ret = -1 ;
01362     }
01363   } // if (L_ret != -1)
01364   
01365   if (L_ret != -1) {
01366     for (L_it = L_data->begin();
01367          L_it != L_data->end();
01368          L_it++) {
01369       if (strcmp((*L_it)->get_name(), "body") == 0) {
01370         NEW_VAR(m_body_fields_list, T_FieldDefList());
01371         L_ret = analyze_from_xml (*L_it, 
01372                                   m_body_fields_list, 
01373                                   &m_body_name,
01374                                   &m_body_type,
01375                                   true) ;
01376         break ;
01377       }
01378     }
01379   } // if (L_ret != -1)
01380   
01381   if (L_ret != -1) {
01382     if (!m_header_fields_list->empty()) {
01383       m_nb_header_fields = m_header_fields_list->size()  ;
01384       if (m_nb_header_fields > 0) {
01385         for (L_fielddef_it  = m_header_fields_list->begin();
01386              L_fielddef_it != m_header_fields_list->end();
01387              L_fielddef_it++) {
01388           L_field_desc = check_field ((*L_fielddef_it), true) ;
01389           if (L_field_desc == NULL) { L_ret = -1 ; break ; }
01390         }
01391       } 
01392     } 
01393   } // if (L_ret != -1)
01394 
01395   if (L_ret != -1) {
01396     if (!m_body_fields_list->empty()) {
01397       m_nb_body_fields = m_body_fields_list->size()  ;
01398       if (m_nb_body_fields > 0) {
01399         for (L_fielddef_it = m_body_fields_list->begin();
01400              L_fielddef_it != m_body_fields_list->end();
01401              L_fielddef_it++) {
01402           L_field_desc = check_field ((*L_fielddef_it), false) ; 
01403           if (L_field_desc == NULL) { L_ret = -1 ; break ; }
01404         }
01405       } 
01406     } 
01407   } // if (L_ret != -1)
01408 
01409   if (L_ret != -1) {
01410     for (L_it = L_data->begin();
01411          L_it != L_data->end();
01412          L_it++) {
01413       
01414       if (strcmp((*L_it)->get_name(), "dictionary") == 0) {
01415         L_dataDico = *L_it ;
01416         if (L_dataDico == NULL) {
01417           GEN_ERROR(E_GEN_FATAL_ERROR, "No dictionnary found (or no value) in protocol ["
01418                     << *P_name << "]");
01419           L_ret = -1 ;
01420         } else {
01421           L_ret = analyze_dictionnary_from_xml (L_dataDico);
01422         }
01423         break ;
01424       } 
01425     } 
01426   } // if (L_ret != -1)
01427 
01428 
01429 
01430   if (L_ret != -1) {
01431     L_id = 0 ;
01432     for (L_message_it = m_header_fields_dico->begin();
01433          L_message_it != m_header_fields_dico->end();
01434          L_message_it++) {
01435 
01436       ALLOC_TABLE(m_message_names_table[L_id],
01437                   char*, sizeof(char),
01438                   (strlen(L_message_it->m_name))+1);
01439       
01440       strcpy(m_message_names_table[L_id],L_message_it->m_name) ;
01441 
01442       m_message_decode_map->insert(T_DecodeMap::value_type(L_message_it->m_name,
01443                                                            L_id));
01444 
01445       L_id ++ ;      
01446     }
01447   }
01448 
01449   if (L_ret != -1) {
01450     L_id = find_field_id(m_header_type);
01451     if (L_id != -1) {
01452       set_message_type_field_id(L_id);
01453     } else {
01454       GEN_ERROR(E_GEN_FATAL_ERROR, "No definition found for field [" 
01455                 << m_header_type << "] used for type definition");
01456       L_ret = -1 ;
01457     }
01458   }
01459 
01460   if (L_ret != -1) {
01461     T_FieldNameMap::iterator L_field_it;
01462     m_nb_fields_desc_table        = m_nb_header_fields + m_nb_body_fields    ;
01463     if (m_nb_fields_desc_table > 0) {
01464 
01465       ALLOC_TABLE(m_names_fields, char**, sizeof(char*),m_nb_fields_desc_table);
01466       for (L_i = 0; L_i < m_nb_fields_desc_table ; L_i++) {
01467         m_names_fields[L_i] = NULL ;
01468       } 
01469 
01470       ALLOC_TABLE(m_fields_desc_table,
01471                   T_pFieldDesc*,
01472                   sizeof(T_pFieldDesc),
01473                   m_nb_fields_desc_table);
01474       for (L_field_it = m_fields_name_map->begin();
01475            L_field_it != m_fields_name_map->end();
01476            L_field_it++) {
01477 
01478         L_id = L_field_it->second->m_id ;
01479 
01480         ALLOC_TABLE(m_names_fields[L_id],
01481                     char*, sizeof(char),
01482                     strlen((L_field_it->second)->m_name)+1);
01483         
01484         strcpy(m_names_fields[L_id],(L_field_it->second)->m_name) ;
01485 
01486         m_fields_desc_table[L_id]=L_field_it->second;
01487       }
01488     }
01489   }
01490   
01491   GEN_DEBUG(1, "C_ProtocolText::xml_interpretor() end");
01492   
01493   return (L_ret);
01494 }
01495 
01496 
01497 int C_ProtocolText::new_id() {
01498   m_id_counter ++ ;
01499   return(m_id_counter);
01500 }
01501 
01502 
01503 int C_ProtocolText::find_field_id (char*P_name) {
01504 
01505   int L_id = -1 ;
01506   T_FieldNameMap::iterator     L_it                      ;
01507 
01508   GEN_DEBUG(1, "C_ProtocolText::find_field_id() start");  
01509 
01510   // retrieve id and type of session id
01511   if (P_name != NULL ) {
01512     // retrieve session id in header field map (m_fields_name_map)
01513     L_it = m_fields_name_map->find(T_FieldNameMap::key_type(P_name));
01514     if (L_it != m_fields_name_map->end()) {
01515       L_id = (L_it->second)->m_id ;
01516     } 
01517   } 
01518   GEN_DEBUG(1, "C_ProtocolText::find_field_id() end");  
01519 
01520   return (L_id);
01521 }
01522 
01523 int   C_ProtocolText::set_field_value(C_MessageText *P_msg, 
01524                                       int            P_id, 
01525                                       T_pValueData   P_value) {
01526 
01527   int                            L_ret        = 0    ;
01528   T_pFieldDesc                   L_field_desc = NULL ;
01529   T_RegExpLst::iterator          L_regexp_it         ; 
01530   C_RegExp                      *L_cRegExp    = NULL ; 
01531   int                            L_start      = -1   ;
01532   int                            L_end        = -1   ;
01533 
01534   char                          *L_string_value = NULL ;
01535   bool                           L_match = false ;
01536 
01537 
01538   GEN_DEBUG(1, "C_ProtocolText::set_field_value() start");  
01539 
01540   if (P_id <= (m_nb_body_fields + m_nb_header_fields)) {
01541     L_field_desc = m_fields_desc_table[P_id] ;
01542     
01543     L_string_value = P_msg->get_text_value(L_field_desc->m_header_body) ;
01544 
01545     for (L_regexp_it = (L_field_desc->m_reg_exp_lst)->begin();
01546          L_regexp_it != (L_field_desc->m_reg_exp_lst)->end();
01547          L_regexp_it++) {
01548       L_cRegExp = *L_regexp_it ;
01549       
01550       
01551       L_ret = L_cRegExp->execute(L_string_value,
01552                                  &L_start, 
01553                                  &L_end);
01554       
01555       if (L_ret == 0) { 
01556         L_match = true ;  
01557         break ; 
01558       }
01559     } // for rege
01560 
01561     if (L_match == false) {
01562       // first, insert format
01563       T_pValueData L_value = P_msg->get_data_value(L_field_desc->m_header_body) ;
01564             
01565       if (L_field_desc->m_format != NULL) {
01566         copyBinaryVal(*L_value,  
01567                       L_value->m_value.m_val_binary.m_size, 
01568                       (L_field_desc->m_format)->m_value.m_val_binary.m_size,
01569                       *(L_field_desc->m_format));
01570       
01571 
01572       // re-execute match 
01573         L_ret = L_cRegExp->execute((char*)L_value->m_value.m_val_binary.m_value,
01574                                    &L_start,
01575                                    &L_end);
01576       } else {
01577         GEN_LOG_EVENT(LOG_LEVEL_TRAFFIC_ERR, 
01578                       "no match regexp for your field ["
01579                       << L_field_desc->m_name << "]");
01580         L_ret = -1 ;
01581       }
01582     }
01583 
01584     if (L_ret != -1) {
01585       L_ret = P_msg->set_text_value(L_field_desc->m_header_body,P_value,L_start,L_end);
01586     }
01587   }
01588 
01589   FREE_TABLE(L_string_value);
01590 
01591   GEN_DEBUG(1, "C_ProtocolText::set_field_value() end");  
01592   return (L_ret);
01593 }
01594 
01595 char* C_ProtocolText::get_field_value(C_MessageText *P_msg, 
01596                                       int P_id,
01597                                       C_RegExp  *P_regexp_data) {
01598 
01599   char                      *L_value        = NULL ;
01600   T_pFieldDesc               L_field_desc   = NULL ;
01601   char                      *L_string_value = NULL ;
01602 
01603   GEN_DEBUG(1, "C_ProtocolText::get_field_value() start");  
01604 
01605   if (P_id <= (m_nb_body_fields + m_nb_header_fields)) {
01606     L_field_desc = m_fields_desc_table[P_id] ;
01607     L_string_value = P_msg->get_text_value(L_field_desc->m_header_body) ;
01608     if (L_string_value != NULL ) {
01609       L_value = P_regexp_data->execute(L_string_value);
01610     }
01611   }
01612 
01613   FREE_TABLE(L_string_value);
01614 
01615   GEN_DEBUG(1, "C_ProtocolText::get_field_value() end");  
01616 
01617   return (L_value);
01618   
01619 }
01620 
01621 
01622 char* C_ProtocolText::get_field_value(C_MessageText *P_msg, int P_id) {
01623 
01624   char                      *L_value         = NULL ;
01625   T_pFieldDesc               L_field_desc    = NULL ;
01626   T_RegExpLst::iterator      L_regexp_it            ; 
01627   C_RegExp*                  L_cRegExp       = NULL ; 
01628   char                       *L_string_value = NULL ;
01629 
01630 
01631 
01632   GEN_DEBUG(1, "C_ProtocolText::get_field_value() start");  
01633 
01634   if (P_id <= (m_nb_body_fields + m_nb_header_fields)) {
01635     L_field_desc = m_fields_desc_table[P_id] ;
01636     for (L_regexp_it = (L_field_desc->m_reg_exp_lst)->begin();
01637          L_regexp_it != (L_field_desc->m_reg_exp_lst)->end();
01638          L_regexp_it++) {
01639       L_cRegExp = *L_regexp_it ;
01640 
01641       L_string_value = P_msg->get_text_value(L_field_desc->m_header_body) ;
01642       if (L_string_value != NULL ) {
01643         L_value = L_cRegExp->execute(L_string_value);
01644         FREE_TABLE(L_string_value);
01645         if (L_value != NULL) break ;
01646       }
01647     }
01648   }
01649 
01650   GEN_DEBUG(1, "C_ProtocolText::get_field_value() end");  
01651 
01652   return (L_value);
01653   
01654 }
01655 
01656 char* C_ProtocolText::get_field_value_to_check(C_MessageText *P_msg, 
01657                                                int P_id,
01658                                                int *P_size) {
01659 
01660   char                          *L_value        = NULL ;
01661   T_pFieldDesc                   L_field_desc   = NULL ;
01662   T_RegExpLst::iterator          L_regexp_it           ; 
01663   C_RegExp*                      L_cRegExp      = NULL ;
01664   int                            L_size         = -1   ;
01665   char                          *L_string_value = NULL ;
01666 
01667   GEN_DEBUG(1, "C_ProtocolText::get_field_value_to_check() start");  
01668 
01669   if (P_id <= (m_nb_body_fields + m_nb_header_fields)) {
01670     L_field_desc = m_fields_desc_table[P_id] ;
01671     for (L_regexp_it = (L_field_desc->m_reg_exp_lst)->begin();
01672          L_regexp_it != (L_field_desc->m_reg_exp_lst)->end();
01673          L_regexp_it++) {
01674       L_cRegExp = *L_regexp_it ;
01675 
01676       L_string_value = P_msg->get_text_value(L_field_desc->m_header_body) ;
01677       L_value = L_cRegExp->execute(L_string_value,
01678                                    &L_size);
01679 
01680       FREE_TABLE(L_string_value);
01681       if (L_value != NULL) {
01682         *P_size = L_size ;
01683         break ;
01684       }
01685     }
01686   }
01687 
01688   GEN_DEBUG(1, "C_ProtocolText::get_field_value_to_check() end");  
01689 
01690   return (L_value);
01691   
01692 }
01693 
01694 C_ProtocolText::T_pManagementSessionTextId 
01695 C_ProtocolText::get_manage_session_elt(int P_id) {
01696   
01697   T_ManagementSessionTextId  *L_ret = NULL ;
01698   
01699   if ((P_id < m_value_sessions_table_size) && (P_id >= 0)) {
01700     L_ret = &m_value_sessions_table[P_id] ;
01701   }
01702   return (L_ret) ;
01703 }
01704 
01705 
01706 int C_ProtocolText::get_nb_management_session () {
01707   return (m_value_sessions_table_size);
01708 }
01709 
01710 int C_ProtocolText::get_message_id (char *P_name) {
01711   int L_ret = -1 ;
01712   T_DecodeMap::iterator L_it ;
01713   L_it = m_message_decode_map->find(T_DecodeMap::key_type(P_name)) ;
01714   if (L_it != m_message_decode_map->end()) {
01715     L_ret = L_it->second ;
01716   }
01717   return (L_ret);
01718 }
01719 
01720 iostream_output& C_ProtocolText::print_header_msg(iostream_output& P_stream,
01721                                                   int              P_id,
01722                                                   T_pValueData     P_header) {
01723 
01724   P_stream << m_message_names_table[P_id] ;
01725   print_data_msg(P_stream,P_header);
01726   return (P_stream);
01727 
01728 }
01729 
01730 iostream_output& C_ProtocolText::print_data_msg(iostream_output& P_stream,
01731                                             T_pValueData     P_data) {
01732 
01733  
01734   static char L_hexa_buf [50] ;
01735   static char L_ascii_buf [50] ;
01736   static char L_line_buf [100] ;
01737   const  size_t L_cNum = 16 ; 
01738   size_t L_j, L_nb ;
01739   unsigned char*L_cur ;
01740   size_t L_buffer_size = P_data->m_value.m_val_binary.m_size;
01741 
01742   GEN_DEBUG(1, "C_ProtocolText::print_data() start");
01743 
01744 
01745 
01746   L_nb = L_buffer_size / L_cNum ;
01747   L_cur = P_data->m_value.m_val_binary.m_value ;
01748   if (L_cur != NULL) {
01749     for (L_j = 0 ; L_j < L_nb; L_j++) {
01750       P_stream << iostream_endl ;
01751       P_stream << GEN_HEADER_LOG << GEN_HEADER_LEVEL(LOG_LEVEL_MSG) ;
01752 
01753       pretty_binary_printable_buffer(L_cur, L_cNum, L_hexa_buf, L_ascii_buf);
01754       sprintf(L_line_buf, "[%-48.48s] <=> [%-16.16s]", L_hexa_buf, L_ascii_buf);
01755       P_stream << L_line_buf;
01756 
01757       L_cur += L_cNum ;
01758     }
01759 
01760     L_nb = L_buffer_size % L_cNum ;
01761     if (L_nb != 0) {
01762       P_stream << iostream_endl ;
01763       P_stream << GEN_HEADER_LOG << GEN_HEADER_LEVEL(LOG_LEVEL_MSG) ;
01764 
01765       pretty_binary_printable_buffer(L_cur, L_nb, L_hexa_buf, L_ascii_buf);
01766       sprintf(L_line_buf, "[%-48.48s] <=> [%-16.16s]", L_hexa_buf, L_ascii_buf);
01767       P_stream << L_line_buf;
01768     }
01769     
01770     
01771     P_stream << iostream_endl ;
01772 
01773   } else {
01774     P_stream << "NULL(empty)";
01775   }
01776   
01777   GEN_DEBUG(1, "C_ProtocolText::print_data() end");
01778 
01779   return (P_stream);
01780 
01781 }
01782 
01783 int C_ProtocolText::analyze_extern_method_from_xml(C_XmlData             *P_data, 
01784                                                    T_DefMethodExternList *P_def_method_list) {
01785 
01786   int                       L_ret                = 0        ;
01787   T_pXmlData_List           L_defmethod_xml_list = NULL     ;
01788   C_XmlData                *L_defmethod          = NULL     ;
01789   T_XmlData_List::iterator  L_defmethod_it                  ;
01790 
01791   T_DefMethodExtern         L_defmethod_data                ;
01792 
01793 
01794   GEN_DEBUG(1, "C_ProtocolText::analyze_extern_method_from_xml() start");
01795 
01796   L_defmethod_xml_list = P_data->get_sub_data() ;
01797 
01798   for(L_defmethod_it  = L_defmethod_xml_list->begin() ;
01799       L_defmethod_it != L_defmethod_xml_list->end() ;
01800       L_defmethod_it++) {
01801 
01802     L_defmethod = *L_defmethod_it ;
01803     
01804     if (strcmp(L_defmethod->get_name(), (char*)"defmethod") == 0) {
01805       L_defmethod_data.m_name = L_defmethod->find_value((char*)"name") ;
01806       if (L_defmethod_data.m_name == NULL) {
01807         GEN_ERROR(E_GEN_FATAL_ERROR, 
01808                   "defmehod name value is mandatory");
01809         L_ret = -1 ;
01810         break ;
01811       }
01812       
01813       L_defmethod_data.m_param = L_defmethod->find_value((char*)"param") ;
01814       if (L_defmethod_data.m_param == NULL) {
01815         GEN_ERROR(E_GEN_FATAL_ERROR, 
01816                   "defmehod param value is mandatory");
01817         L_ret = -1 ;
01818         break ;
01819       }
01820 
01821       if (L_ret == -1) { 
01822         break ; 
01823       } else {
01824         P_def_method_list->push_back(L_defmethod_data);
01825       }
01826 
01827     } // strcmp defmethod
01828   }
01829 
01830   GEN_DEBUG(1, "C_ProtocolText::analyze_extern_method_from_xml() end");
01831 
01832   return (L_ret);
01833 }
01834 
01835 int C_ProtocolText::analyze_body_method_from_xml(C_XmlData *P_data, 
01836                                                  T_DefMethodList *P_def_method_list) { 
01837 
01838 
01839 
01840   int                       L_ret                = 0        ;
01841 
01842   T_pXmlData_List           L_defmethod_xml_list = NULL     ;
01843   C_XmlData                *L_defmethod          = NULL     ;
01844   T_XmlData_List::iterator  L_defmethod_it                  ;
01845 
01846   T_DefMethod               L_defmethod_data                ;
01847   char                     *L_method                        ;
01848 
01849   GEN_DEBUG(1, "C_ProtocolText::analyze_body_method_from_xml() start");
01850 
01851   L_defmethod_xml_list = P_data->get_sub_data() ;
01852 
01853   for(L_defmethod_it  = L_defmethod_xml_list->begin() ;
01854       L_defmethod_it != L_defmethod_xml_list->end() ;
01855       L_defmethod_it++) {
01856 
01857     L_defmethod = *L_defmethod_it ;
01858 
01859     if (strcmp(L_defmethod->get_name(), (char*)"defmethod") == 0) {
01860       L_defmethod_data.m_name = L_defmethod->find_value((char*)"name") ;
01861       if (L_defmethod_data.m_name == NULL) {
01862         GEN_ERROR(E_GEN_FATAL_ERROR, 
01863                   "defmehod name value is mandatory");
01864         L_ret = -1 ;
01865         break ;
01866       }
01867 
01868       L_method = L_defmethod->find_value((char*)"method") ;
01869       if (L_method == NULL) {
01870         GEN_ERROR(E_GEN_FATAL_ERROR, 
01871                   "defmehod method value is mandatory");
01872         L_ret = -1 ;
01873         break ;
01874       } else {
01875         if (strcmp(L_method, (char*)"length") == 0) {
01876           L_defmethod_data.m_method = E_BODY_METHOD_LENGTH ;
01877         } else if (strcmp(L_method , (char*)"parser") == 0 ) {
01878           L_defmethod_data.m_method = E_BODY_METHOD_PARSE ;
01879         } else {
01880           GEN_ERROR(E_GEN_FATAL_ERROR, 
01881                     "Body method for protocol definition must be length or parser");
01882           L_ret = -1 ;
01883           break ;
01884         }
01885       }
01886 
01887       
01888 
01889       L_defmethod_data.m_param = L_defmethod->find_value((char*)"param") ;
01890       if (L_defmethod_data.m_param == NULL) {
01891         GEN_ERROR(E_GEN_FATAL_ERROR, 
01892                   "defmehod param value is mandatory");
01893         L_ret = -1 ;
01894         break ;
01895       }
01896 
01897       if (L_ret == -1) { 
01898         break ; 
01899       } else {
01900         L_defmethod_data.m_default = L_defmethod->find_value((char*)"default") ;
01901         P_def_method_list->push_back(L_defmethod_data);
01902       }
01903 
01904     } // strcmp defmethod
01905   }
01906 
01907   GEN_DEBUG(1, "C_ProtocolText::analyze_body_method_from_xml() end");
01908 
01909   return (L_ret);
01910 }
01911 
01912 
01913 C_MessageText* C_ProtocolText::create(C_ProtocolText *P_protocol,
01914                                       char           *P_header,
01915                                       char           *P_body) {
01916   
01917   // Regexep 
01918   int                      L_result = 0 ;
01919   T_pValueData             L_value_header, L_value_body ;
01920   C_MessageText           *L_msg = NULL ;
01921   int                      L_id = -1 ;
01922 
01923   char                    *L_buffer_header          = NULL   ;
01924   char                    *L_buffer_body            = NULL   ;
01925 
01926   char                    *L_new_buffer_header      = NULL   ;
01927   char                    *L_new_buffer_body        = NULL   ;
01928   unsigned long            L_size_variable_value    = 0      ;
01929 
01930     
01931   GEN_DEBUG(1, "C_ProtocolText::create() start");
01932 
01933   ALLOC_VAR(L_value_header, T_pValueData, sizeof(T_ValueData));
01934   L_value_header->m_type = E_TYPE_STRING ;
01935   L_value_header->m_value.m_val_binary.m_size = 0 ;
01936   L_value_header->m_value.m_val_binary.m_value = NULL ;
01937 
01938   if (P_header != NULL) {
01939 
01940     if (m_filter_function) {
01941       L_buffer_header = (*m_filter_function)(P_header);
01942     } else {
01943       L_buffer_header = P_header ;
01944     }
01945     if (L_buffer_header == NULL) {
01946       L_result = -1 ;
01947     } else {
01948 
01949       if ((strstr(L_buffer_header,(char*)"$(") == NULL) || 
01950           (m_config_value_list->empty())) {
01951         L_new_buffer_header = L_buffer_header ;
01952       } else {
01953         L_size_variable_value = calculate_size(L_buffer_header);
01954         L_new_buffer_header = replace_variable_config(L_buffer_header, L_size_variable_value);
01955       }
01956       
01957       if (L_new_buffer_header != NULL ) {
01958         *L_value_header = valueFromString(L_new_buffer_header, 
01959                                           E_TYPE_STRING , L_result);
01960       } else { 
01961         L_result = -1 ;
01962       }
01963     }
01964   }
01965 
01966   if (L_result == -1) { 
01967     FREE_VAR(L_value_header);
01968     FREE_TABLE(L_buffer_header);
01969     return(NULL); 
01970   }
01971 
01972   ALLOC_VAR(L_value_body, T_pValueData, sizeof(T_ValueData));
01973   L_value_body->m_type = E_TYPE_STRING ;
01974   L_value_body->m_value.m_val_binary.m_size = 0 ;
01975   L_value_body->m_value.m_val_binary.m_value = NULL ;
01976   if (P_body != NULL) {
01977 
01978     if (m_filter_function) {
01979       L_buffer_body = (*m_filter_function)(P_body);
01980     } else {
01981       L_buffer_body = P_body ;
01982     }
01983     if (L_buffer_body == NULL) {
01984       L_result = -1 ;
01985     } else {
01986       if ((strstr(L_buffer_body,(char*)"$(") == NULL) ||
01987           (m_config_value_list->empty())) {
01988         L_new_buffer_body = L_buffer_body ;
01989       } else {
01990         L_size_variable_value = calculate_size(L_buffer_body);
01991         L_new_buffer_body = replace_variable_config(L_buffer_body, L_size_variable_value);
01992       }
01993 
01994       if (L_new_buffer_body != NULL ) {
01995         
01996         *L_value_body = valueFromString(L_new_buffer_body, 
01997                                         E_TYPE_STRING , L_result);
01998       } else { 
01999         L_result = -1 ;
02000       }
02001     }
02002   }
02003 
02004   if (L_result == -1) { 
02005     FREE_VAR(L_value_header);
02006     FREE_VAR(L_value_body);
02007 
02008     FREE_TABLE(L_buffer_header);
02009     FREE_TABLE(L_buffer_body);
02010 
02011     return (NULL); 
02012   } 
02013 
02014   NEW_VAR(L_msg, C_MessageText(P_protocol, 
02015                                L_value_header, L_value_body, &L_id));
02016 
02017   if (L_id == -1) {
02018     DELETE_VAR(L_msg) ;
02019   } 
02020 
02021 
02022   FREE_TABLE(L_buffer_header);
02023   FREE_TABLE(L_buffer_body);
02024 
02025   GEN_DEBUG(1, "C_ProtocolText::create() end");
02026   return (L_msg);
02027 }
02028 
02029 void C_ProtocolText::set_body_separator (char*  P_body_separator) {
02030   m_body_separator = P_body_separator ;
02031   m_body_separator_size = strlen(m_body_separator);
02032 }
02033 
02034 void C_ProtocolText::set_field_separator (char*  P_field_separator) {
02035   m_field_separator = P_field_separator ;
02036   m_field_separator_size = strlen(m_field_separator);
02037 }
02038 
02039 
02040 void C_ProtocolText::set_message_type_field_id (int P_id) {
02041   m_message_type_field_id = P_id ;
02042 }
02043 
02044 void C_ProtocolText::set_session_id_id (int P_id) {
02045   m_session_id_id = P_id ;
02046 }
02047 
02048 
02049 void C_ProtocolText::set_number_methods (int P_nb) {
02050   int L_i ;
02051 
02052   m_nb_methods = P_nb ;
02053   ALLOC_TABLE(m_methods,
02054               T_BodyDecodeData**,
02055               sizeof(T_pBodyDecodeData),
02056               m_nb_methods);
02057   for (L_i = 0 ; L_i < m_nb_methods; L_i++) {
02058     m_methods[L_i] = NULL ;
02059   }
02060   
02061 }
02062 
02063 
02064 void C_ProtocolText::set_body_decode_method (int                     P_index,
02065                                              T_BodyMethodType        P_type,
02066                                              T_BodyDecodeMethod      P_method,
02067                                              void                   *P_param) {
02068   if (m_methods[P_index] == NULL) {
02069     ALLOC_VAR(m_methods[P_index],
02070               T_pBodyDecodeData,
02071               sizeof(T_BodyDecodeData));
02072   }
02073   m_methods[P_index]->m_decode = P_method ;
02074   switch (P_type) {
02075   case E_BODY_METHOD_LENGTH:
02076     (m_methods[P_index]->m_param_decode).m_id =  *((int*)P_param) ;
02077     break ;
02078   case E_BODY_METHOD_PARSE:
02079     (m_methods[P_index]->m_param_decode).m_parser = (T_ParserFunction) P_param ;
02080     break ;
02081   }
02082 }
02083 
02084 
02085 void C_ProtocolText::set_encode_method (int                 P_index,
02086                                         T_BodyMethodType    P_type,
02087                                         T_EncodeMethod      P_method,
02088                                         void               *P_param) {
02089   if (m_methods[P_index] == NULL) {
02090     ALLOC_VAR(m_methods[P_index],
02091               T_pBodyDecodeData,
02092               sizeof(T_BodyDecodeData));
02093   }
02094   m_methods[P_index]->m_encode = P_method ;
02095   switch (P_type) {
02096   case E_BODY_METHOD_LENGTH:
02097     (m_methods[P_index]->m_param_encode).m_id =  *((int*)P_param) ;
02098     break ;
02099   case E_BODY_METHOD_PARSE:
02100     (m_methods[P_index]->m_param_encode).m_parser = (T_ParserFunction) P_param ;
02101     break ;
02102   }
02103 }
02104 
02105 
02106 void C_ProtocolText::destroy_methods () {
02107   int L_i ;
02108   for (L_i = 0 ; L_i < m_nb_methods; L_i++) {
02109     FREE_VAR(m_methods[L_i]);
02110   }
02111   FREE_TABLE(m_methods);
02112   m_nb_methods = 0 ;
02113 }
02114 
02115 void C_ProtocolText::use_open_id () {
02116   m_session_method = &C_MessageText::getSessionFromOpenId ;
02117 }
02118 
02119 
02120 T_ExternalMethod C_ProtocolText::find_method_extern(char *P_name) {
02121 
02122   int L_i ;
02123   T_ExternalMethod                L_ret = NULL               ;
02124   T_DefMethodExternList::iterator L_def_method_extern_it     ;
02125   T_DefMethodExtern               L_defmethod_extern_data    ;
02126 
02127 
02128   if (m_nb_method_external_table > 0 ) {
02129     L_i = 0 ;
02130     for (L_def_method_extern_it = m_def_method_extern_list->begin();
02131          L_def_method_extern_it != m_def_method_extern_list->end();
02132          L_def_method_extern_it++) {
02133       
02134       L_defmethod_extern_data = *L_def_method_extern_it ;
02135       if (strcmp(P_name, L_defmethod_extern_data.m_name) == 0){
02136         L_ret = m_method_external_table[L_i].m_method ;
02137         break ;
02138       }
02139       L_i ++;
02140     }
02141   }
02142   return (L_ret);
02143 }
02144 
02145 int C_ProtocolText::xml_configuration_parameters(C_XmlData *P_data, 
02146                                                  T_ParamDefList *P_paramdef_list) { 
02147 
02148 
02149   int                       L_ret = 0 ;
02150 
02151   T_pXmlData_List           L_paramdef_xml_list = NULL ;
02152   C_XmlData                *L_paramdef = NULL          ;
02153   T_XmlData_List::iterator  L_paramdef_it              ;
02154 
02155   T_ParamDef                L_paramdef_data            ;
02156   bool                      L_found_default = false    ;
02157 
02158   GEN_DEBUG(1, "C_ProtocolText::xml_configuration_parameters() start");
02159 
02160   L_paramdef_xml_list = P_data->get_sub_data() ;
02161 
02162   for(L_paramdef_it  = L_paramdef_xml_list->begin() ;
02163       L_paramdef_it != L_paramdef_xml_list->end() ;
02164       L_paramdef_it++) {
02165 
02166     L_paramdef = *L_paramdef_it ;
02167 
02168     if (strcmp(L_paramdef->get_name(), (char*)"paramdef") == 0) {
02169 
02170       L_found_default = false ;
02171       L_paramdef_data.m_name = L_paramdef->find_value((char*)"name") ;
02172       if (L_paramdef_data.m_name == NULL) {
02173         GEN_ERROR(E_GEN_FATAL_ERROR, 
02174                   "paramdef name value is mandatory");
02175         L_ret = -1 ;
02176         break ;
02177       }
02178 
02179       L_paramdef_data.m_default = L_paramdef->find_value((char*)"default") ;
02180       if (L_paramdef_data.m_default != NULL) {
02181         L_found_default = true ;
02182       } 
02183 
02184       L_paramdef_data.m_mandatory = L_paramdef->find_value((char*)"mandatory") ;
02185 
02186       if ((L_paramdef_data.m_mandatory == NULL) ||
02187           (strcmp(L_paramdef_data.m_mandatory,"false") == 0 )) {
02188         
02189         if (L_found_default == false ) {
02190           GEN_ERROR(E_GEN_FATAL_ERROR, 
02191                     "paramdef mandatory or default value is mandatory");
02192           L_ret = -1 ;
02193         }
02194       }
02195 
02196       if (L_ret == -1) { 
02197         break ; 
02198       } else {
02199         P_paramdef_list->push_back(L_paramdef_data);
02200       }
02201 
02202     } // strcmp paradef
02203   }
02204 
02205   GEN_DEBUG(1, "C_ProtocolText::xml_configuration_parameters() end");
02206   return (L_ret);
02207 }
02208 
02209 
02210 int C_ProtocolText::update_config_params(T_ParamDef& P_config_param_dico,
02211                                          T_pConfigValueList P_config_value_list) {
02212 
02213 
02214   int                          L_ret            = 0     ;
02215   T_ConfigValueList::iterator  L_configValue_it         ;
02216   bool                         L_found          = false ;
02217   T_ConfigValue               L_configValue             ;
02218 
02219   GEN_DEBUG(1, "C_ProtocolText::update_config_params() start");
02220 
02221   if (!P_config_value_list->empty()) {
02222     for (L_configValue_it = P_config_value_list->begin();
02223          L_configValue_it != P_config_value_list->end();
02224          L_configValue_it++) {
02225       if (strcmp(L_configValue_it->m_name, P_config_param_dico.m_name) == 0) {
02226         L_found = true ;
02227         // the config file value is used
02228         break;
02229       }
02230     }
02231   }
02232   
02233   if (L_found == false) {
02234     // ctrl mandatory => error , must be in the config
02235     if ((P_config_param_dico.m_mandatory != NULL) && 
02236         (strcmp(P_config_param_dico.m_mandatory,"true") == 0 )) {
02237       GEN_ERROR(E_GEN_FATAL_ERROR, 
02238                 "paramdef mandatory value is mandatory for ["
02239                 << P_config_param_dico.m_name << "]");
02240       L_ret = -1 ;
02241     } else {
02242       // not mandatory, but a default value is known
02243       // element not found in config list
02244       // Add this element in the P_config_value_list
02245       L_configValue.m_name = P_config_param_dico.m_name ;
02246       L_configValue.m_value = P_config_param_dico.m_default ;
02247       P_config_value_list->push_back(L_configValue);
02248     }
02249   } 
02250  
02251   GEN_DEBUG(1, "C_ProtocolText::update_config_params() end");
02252   return (L_ret);
02253 }
02254 
02255 char* C_ProtocolText::find_config_value(char* P_varible) {
02256   
02257   char* L_value = NULL ;
02258 
02259   T_ConfigValueList::iterator  L_configValue_it         ;
02260   GEN_DEBUG(1, "C_ProtocolText::find_config_value() start");    
02261 
02262   if (!m_config_value_list->empty()) {
02263     for (L_configValue_it = m_config_value_list->begin();
02264          L_configValue_it != m_config_value_list->end();
02265          L_configValue_it++) {
02266       if (strcmp(L_configValue_it->m_name, P_varible) == 0) {
02267         L_value = L_configValue_it->m_value ;
02268         break ;
02269       }
02270     }
02271   }
02272     
02273   GEN_DEBUG(1, "C_ProtocolText::find_config_value() end");    
02274   return (L_value);
02275 
02276 }
02277 
02278 
02279 unsigned long  C_ProtocolText::calculate_size(char* P_varibleString) {
02280 
02281   unsigned long     L_ret                  = 0               ;
02282   unsigned long     L_size_variable_config = 0               ;
02283   unsigned long     L_size_value_config    = 0               ;
02284   unsigned long     L_size_buffer          = 0               ;
02285 
02286   char             *L_ptr                  = P_varibleString ;
02287   char             *L_pos                  = NULL            ;
02288   char             *L_search               = NULL            ;
02289   char             *L_value_conf           = NULL            ;
02290 
02291   GEN_DEBUG(1, "C_ProtocolText::calculate_size() start");
02292 
02293   if ((P_varibleString != NULL) && 
02294       ((L_size_buffer = strlen(P_varibleString)) > 0 )) {
02295     while((L_ptr) && ((L_pos = strstr(L_ptr,(char*)"$(")) != NULL)) {
02296       if ((L_pos + 2) <= (P_varibleString+L_size_buffer)) { 
02297         L_search = search_variable((L_pos + 2)) ;
02298         if (L_search != NULL) {
02299           L_size_variable_config += strlen(L_search);
02300           L_value_conf  = find_config_value(L_search);
02301           if (L_value_conf != NULL) {
02302             L_size_value_config += strlen(L_value_conf);
02303           }
02304           L_ptr = L_pos + strlen(L_search) + 3 ;
02305           FREE_TABLE(L_search);
02306         } 
02307       } else {
02308         L_ptr = NULL ; 
02309       }
02310     } // while
02311     L_ret = L_size_buffer - L_size_variable_config + L_size_value_config ;
02312   }
02313   
02314   GEN_DEBUG(1, "C_ProtocolText::calculate_size() end");
02315   return (L_ret);
02316 }
02317 
02318 char* C_ProtocolText::search_variable(char* P_varibleString) {
02319   
02320   char *L_ptr     = P_varibleString ;
02321   char *L_value   = NULL            ;
02322   int   L_value_size                ;
02323   char *L_pos_d   = NULL            ;
02324   char *L_pos_p   = NULL            ;
02325 
02326   if ((L_ptr == NULL)        ||
02327       ((strlen(L_ptr)) <= 1)) {
02328     return (L_value) ;
02329   } 
02330 
02331   L_pos_p = strchr(L_ptr,')') ;
02332   if (L_pos_p == NULL) {
02333     return (L_value) ;
02334   } else {
02335     L_value_size =  L_pos_p - L_ptr ; 
02336   }
02337 
02338   L_pos_d = strstr(L_ptr,(char*)"$(") ;
02339 
02340   if (L_pos_d != NULL ) {
02341     if ((L_pos_d - L_ptr) < L_value_size) {
02342       return (L_value) ;
02343     } 
02344   }
02345 
02346   ALLOC_TABLE(L_value,
02347               char*, 
02348               sizeof(char),
02349               L_value_size+1);
02350   memcpy(L_value, L_ptr, L_value_size);
02351   L_value[L_value_size] = 0 ;
02352 
02353   return (L_value);
02354 }
02355 
02356 char* C_ProtocolText::replace_variable_config(char* P_buffer, unsigned long P_size_config) {
02357 
02358   size_t    L_size         = 0        ;
02359   size_t    L_size_buffer  = 0        ;
02360   size_t    L_size_end     = 0        ;
02361   size_t    L_size_config  = 0        ;
02362   
02363   char     *L_pos          = NULL     ;
02364   char     *L_ptr          = P_buffer ;
02365   char     *L_result       = NULL     ;
02366   char     *L_new          = NULL     ;
02367   char     *L_search       = NULL     ;
02368   char     *L_value_conf   = NULL     ;
02369 
02370   if ((P_buffer != NULL) && (P_size_config > 0) && 
02371       ((L_size_buffer = strlen(P_buffer)) > 0 )) {
02372     L_size_end = L_size_buffer ;
02373     ALLOC_TABLE(L_result, 
02374                 char*, 
02375                 sizeof(char), 
02376                 P_size_config);
02377 
02378     L_new = L_result ; 
02379 
02380     while((L_ptr) && (L_pos = strstr(L_ptr,(char*)"$(")) != NULL) {
02381       L_size = L_pos - L_ptr ;
02382       if (L_size > 0) {
02383         memcpy(L_new, L_ptr, L_size);
02384         L_new += L_size ;
02385       }
02386       
02387       // test end needed ? for L_ptr
02388       if ((L_pos + 2) <= (P_buffer+L_size_buffer)) { 
02389         L_search = search_variable((L_pos + 2)) ;
02390         if (L_search == NULL) {
02391           *(L_new) = '$' ;
02392           L_size++;
02393           L_new += 1 ;
02394           *(L_new) = '(' ;
02395           L_size++;
02396           L_new += 1 ;
02397           L_size_config = 2 ;
02398         } else {
02399           L_value_conf  = find_config_value(L_search);
02400           if (L_value_conf == NULL) {
02401             L_size_config = strlen(L_search);
02402             memcpy(L_new, (char*)"$(", 2);
02403             memcpy(L_new+2, L_search, L_size_config);
02404             memcpy(L_new+2+strlen(L_search), (char*)")", 1);
02405             L_size_config = strlen(L_search) + 3 ;
02406             L_size += L_size_config ;
02407             L_new  += L_size_config ;
02408           } else {
02409             L_size_config = strlen(L_value_conf) ;
02410             memcpy(L_new, L_value_conf, L_size_config);
02411             L_new  += L_size_config ;
02412             L_size_config = strlen(L_search) + 3 ;
02413             L_size += L_size_config ;
02414           }
02415           FREE_TABLE(L_search);
02416         }
02417         L_size_end -= L_size ;
02418         L_ptr = L_pos + L_size_config ; 
02419       } else { 
02420         *(L_new) = '$' ;
02421         L_size++;
02422         L_new += 1 ;
02423         *(L_new) = '(' ;
02424         L_size++;
02425         L_new += 1 ;
02426         L_ptr = NULL ; 
02427         L_size_end -= L_size ;
02428       }
02429     } // while
02430     
02431     // ctrl the end of buffer
02432     if (L_size_end > 0) {
02433       L_size = L_size_end ;
02434       if (L_size) {
02435         memcpy(L_new, L_ptr, L_size);
02436         L_new += L_size ;
02437         *L_new = '\0' ;
02438       } else {
02439         *L_new = '\0' ;
02440       }
02441     } else {
02442       *L_new = '\0' ;
02443     }
02444   }
02445 
02446   return (L_result);
02447 }
02448 
02449 
02450 
02451 

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