Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_RemoteControl.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_RemoteControl.hpp"
00021 #include "C_TransportControl.hpp"
00022 
00023 
00024 #include "Utils.hpp"
00025 #include "GeneratorTrace.hpp"
00026 #include "C_MessageText.hpp"
00027 
00028 #include "ProtocolData.hpp"
00029 #include <regex.h>
00030 
00031 #include "C_Generator.hpp"
00032 #include "C_SemaphoreTimed.hpp"
00033 
00034 #include "dlfcn_t.hpp"
00035 
00036 #include <cerrno>  // for errno definition
00037 
00038 #include <cstdlib>
00039 #include <pthread.h> // for sched_yield()
00040 
00041 
00042 void _gen_log_error_control(char *P_data) {
00043   GEN_ERROR(E_GEN_FATAL_ERROR, P_data);
00044 }
00045 
00046 void _gen_log_info_control(char *P_data) {
00047   GEN_LOG_EVENT_FORCE(P_data);
00048 }
00049 
00050 
00051 class C_RampControl : public C_TaskControl {
00052 
00053  public:
00054 
00055   C_RampControl(C_Generator *P_gen) ;
00056   ~C_RampControl() ;
00057   
00058   void  init (unsigned long P_duration,
00059               unsigned long P_current_rate,
00060               unsigned long P_sub_rate,
00061               bool          P_increase);
00062 
00063 private :
00064   
00065   C_SemaphoreTimed *m_sem ;
00066   unsigned long m_duration ;
00067   unsigned long m_current_rate ;
00068   unsigned long m_sub_rate ;
00069   bool m_increase ;
00070   C_Generator *m_gen;
00071 
00072   T_GeneratorError TaskProcedure () ;
00073   T_GeneratorError InitProcedure () ;
00074   T_GeneratorError EndProcedure () ;
00075   T_GeneratorError StoppingProcedure () ;
00076   T_GeneratorError ForcedStoppingProcedure() ;
00077 } ;
00078 
00079 C_RampControl::C_RampControl(C_Generator *P_gen) {
00080   m_sem = NULL ;
00081   m_duration = 0;
00082   m_current_rate = 0;
00083   m_sub_rate = 0;
00084   m_increase = false;
00085   m_gen = P_gen;
00086 }
00087 
00088 C_RampControl::~C_RampControl() {
00089   DELETE_VAR(m_sem);
00090 }
00091 
00092 
00093 
00094 void C_RampControl::init(unsigned long P_duration,
00095                          unsigned long P_current_rate,
00096                          unsigned long P_sub_rate,
00097                          bool          P_increase) {
00098   m_duration = P_duration;
00099   m_current_rate = P_current_rate;
00100   m_sub_rate = P_sub_rate;
00101   m_increase = P_increase;
00102   NEW_VAR(m_sem, C_SemaphoreTimed(1));
00103   m_sem -> P();
00104   C_TaskControl::init() ;
00105 }
00106 
00107 T_GeneratorError C_RampControl::ForcedStoppingProcedure() {
00108   return (E_GEN_NO_ERROR);
00109 }
00110 
00111 T_GeneratorError C_RampControl::EndProcedure() {
00112 
00113   delete this ;
00114   return (E_GEN_NO_ERROR);
00115 }
00116 
00117 T_GeneratorError C_RampControl::StoppingProcedure() {
00118 
00119   M_state = C_TaskControl::E_STATE_STOPPED ;
00120   m_sem->V();
00121 
00122   return (E_GEN_NO_ERROR);
00123 }
00124 
00125 T_GeneratorError C_RampControl::InitProcedure() {
00126   return (E_GEN_NO_ERROR);
00127 }
00128 
00129 T_GeneratorError C_RampControl::TaskProcedure() {
00130 
00131   if (m_increase) {
00132     m_current_rate += m_sub_rate ;
00133   } else {
00134     m_current_rate -= m_sub_rate ;
00135   }  
00136 
00137   m_duration-- ;
00138   m_gen->change_call_rate(E_GEN_OP_SET_VALUE, m_current_rate);
00139 
00140   if (m_duration == 0) stop() ;
00141 
00142   sched_yield () ;
00143   m_sem -> P() ;
00144 
00145   return (E_GEN_NO_ERROR);
00146 }
00147 
00148 
00149 C_RemoteControl::C_RemoteControl(C_Generator      * P_gen,
00150                                  C_ProtocolControl* P_protocol_control,
00151                                  char              *P_address):C_TaskControl(){
00152   m_gen = P_gen ;
00153   m_call_select = &select ;
00154   m_events = NULL ;
00155   m_protocol_ctrl  = P_protocol_control ;
00156   m_transport = NULL ;
00157 
00158 
00159   ALLOC_TABLE(m_address,
00160               char*,
00161               sizeof(char),
00162               strlen(P_address)+1);
00163   strcpy(m_address, P_address);
00164 
00165   NEW_VAR(m_msg_remote_list, T_RcvMsgCtxtList());
00166   m_protocol_frame = NULL ;
00167   m_protocol_text  = NULL ;
00168   m_stat = NULL ;
00169 
00170 }
00171 
00172 C_RemoteControl::~C_RemoteControl(){
00173   DELETE_TABLE(m_events);
00174   m_protocol_ctrl = NULL ;
00175   DELETE_VAR(m_msg_remote_list);
00176   FREE_TABLE(m_address);
00177   m_protocol_frame = NULL ;
00178   m_protocol_text  = NULL ;
00179   m_stat = NULL ;
00180 
00181 }
00182 
00183 void  C_RemoteControl::init() {
00184   GEN_DEBUG(0, "C_RemoteControl::init() start");
00185   C_TaskControl::init() ;
00186   m_stat = C_GeneratorStats::instance() ;
00187 
00188   GEN_DEBUG(0, "C_RemoteControl::init() end");
00189 }
00190 
00191 T_GeneratorError C_RemoteControl::InitProcedure() {
00192   T_GeneratorError L_error_code = E_GEN_NO_ERROR ;
00193   int              L_ret ;
00194   int              L_open_id ;
00195   C_TransportControl::T_TransportContext  L_context ;
00196   T_SelectDef              L_select ;
00197 
00198   T_OpenStatus   L_status  ;
00199 
00200 
00201   GEN_DEBUG(0, "C_RemoteControl::InitProcedure() start");
00202   m_max_event_nb = 10; 
00203   NEW_TABLE(m_events, C_TransportEvent, m_max_event_nb);
00204   L_ret =   m_protocol_ctrl->get_protocol_id((char*)"cmd-seagull-protocol") ;
00205   if (L_ret != -1) {
00206     m_protocol_frame = m_protocol_ctrl->get_protocol(L_ret);
00207     m_protocol_text  = dynamic_cast<C_ProtocolText*> (m_protocol_frame);
00208 
00209     m_command_id = m_protocol_text->find_field_id((char*)"type");
00210     m_uri_id = m_protocol_text->find_field_id((char*)"uri");
00211 
00212   } else {
00213     L_error_code= E_GEN_FATAL_ERROR;
00214   }
00215   
00216   // create transport dlopen transport
00217   
00218   L_context.m_lib_handle 
00219     = dlopen((char*)"libtrans_ip.so", RTLD_LAZY);
00220   if (L_context.m_lib_handle == NULL) {
00221     GEN_ERROR(E_GEN_FATAL_ERROR, 
00222               "Unable to open library file [" 
00223               << (char*)"libtrans_ip.so"
00224               << "] error [" << dlerror() << "]");
00225     L_error_code = E_GEN_FATAL_ERROR;
00226   }
00227   
00228   if (L_error_code == E_GEN_NO_ERROR) {
00229     
00230     L_context.m_ext_create
00231       = (T_TransportCreateInstance) dlsym
00232       (L_context.m_lib_handle, 
00233        (char*)"create_cipio_instance");
00234     
00235     if (L_context.m_ext_create == NULL) {
00236       GEN_ERROR(E_GEN_FATAL_ERROR, "Symbol [" << dlerror() << "]");
00237       L_error_code = E_GEN_FATAL_ERROR;
00238     }
00239     
00240     if (L_error_code == E_GEN_NO_ERROR) {
00241       L_context.m_ext_delete
00242         = (T_TransportDeleteInstance) dlsym
00243         (L_context.m_lib_handle, 
00244          (char*)"delete_cipio_instance");
00245       
00246       if (L_context.m_ext_delete == NULL) {
00247         GEN_ERROR(E_GEN_FATAL_ERROR, "Symbol [" << dlerror() << "]");
00248         L_error_code = E_GEN_FATAL_ERROR;
00249       }
00250     }
00251     
00252     if (L_error_code == E_GEN_NO_ERROR) {
00253       L_context.m_instance = (*(L_context.m_ext_create))() ;
00254       m_transport = L_context.m_instance ;
00255       if (m_transport == NULL) {
00256         GEN_ERROR(E_GEN_FATAL_ERROR, "Transport instance not created");
00257         L_error_code = E_GEN_FATAL_ERROR ;
00258       } else {
00259         L_select = m_transport->select_definition() ;
00260         if (L_select != NULL) {
00261           m_call_select = L_select;
00262         }
00263       }
00264     }
00265   }
00266 
00267   if (L_error_code == E_GEN_NO_ERROR) {
00268     if (m_transport->init((char*)"type=tcp", 
00269                           _gen_log_error_control, 
00270                           _gen_log_info_control) != 0) {
00271       L_error_code = E_GEN_FATAL_ERROR ;
00272     }
00273 
00274     L_open_id = m_transport->open(10,m_address,
00275                                   &L_status,
00276                                   m_protocol_frame) ;
00277     if (L_status != E_OPEN_OK) {
00278       L_error_code = E_GEN_FATAL_ERROR ;
00279     }
00280   }
00281 
00282  GEN_DEBUG(0, "C_RemoteControl::doInit() end");
00283   return (L_error_code);
00284 }
00285 
00286 T_GeneratorError C_RemoteControl::TaskProcedure() {
00287   T_GeneratorError L_error_code ;
00288   
00289   while (M_state != C_TaskControl::E_STATE_STOPPED ) {
00290     L_error_code = receiveControl() ;
00291     if (L_error_code != E_GEN_NO_ERROR) {
00292       M_state = C_TaskControl::E_STATE_STOPPED ;
00293     } else {
00294       messageReceivedControl ();
00295     }
00296   }
00297   return (L_error_code);
00298 }
00299 
00300 T_GeneratorError C_RemoteControl::EndProcedure() {
00301 
00302   // close the instances of transport 
00303   if (!m_msg_remote_list->empty()) {
00304     m_msg_remote_list->erase(m_msg_remote_list->begin(),
00305                                m_msg_remote_list->end());
00306   }
00307 
00308   m_transport->close() ;
00309 
00310   return (E_GEN_NO_ERROR);
00311 }
00312 
00313 T_GeneratorError C_RemoteControl::StoppingProcedure() {
00314   M_state = C_TaskControl::E_STATE_STOPPED ;
00315   return (E_GEN_NO_ERROR);
00316 }
00317 
00318 T_GeneratorError C_RemoteControl::ForcedStoppingProcedure() {
00319   return (E_GEN_NO_ERROR);
00320 }
00321 
00322 
00323 T_GeneratorError C_RemoteControl::receiveControl () {
00324 
00325   fd_set                    L_ReadMask             ;
00326   fd_set                    L_WriteMask            ;
00327   fd_set                    L_ExceptionMask        ;
00328   int                       L_n = 0                ;
00329 
00330   /* Number of Fd popped in the select */
00331   int                       L_MaxFd = 0            ; 
00332   int                       L_error = 0            ;
00333 
00334   struct timeval            L_TimeOut              ;
00335   struct timeval           *L_pTimeOut=&L_TimeOut  ;
00336 
00337   T_ReceiveMsgContext       L_currentRcvCtxt       ;
00338 
00339   size_t                    L_nb_event             ;
00340   size_t                    L_i                    ;
00341 
00342 
00343   T_pC_TransportEvent       L_event_occured        ;
00344   int                       L_event_id             ;
00345 
00346   GEN_DEBUG(1, "C_RemoteControl::receiveControl() start");
00347   
00348   L_TimeOut.tv_sec = 1;
00349   L_TimeOut.tv_usec = 0;
00350   
00351   /* Init masks for the select */
00352   FD_ZERO(&L_ReadMask);
00353   FD_ZERO(&L_WriteMask);
00354   FD_ZERO(&L_ExceptionMask);
00355   
00356   L_MaxFd = 0 ;
00357   
00358   L_MaxFd = m_transport->pre_select 
00359     ( L_MaxFd, 
00360       &L_ReadMask, 
00361       &L_WriteMask, 
00362       &L_ExceptionMask, 
00363       L_pTimeOut,
00364       NULL,
00365       0) ;
00366   
00367   
00368   if (L_MaxFd >= 1) {
00369     
00370     // select system call
00371     L_n = (*m_call_select) (L_MaxFd+1,
00372                             &L_ReadMask,
00373                             &L_WriteMask,
00374                             &L_ExceptionMask,
00375                             L_pTimeOut);
00376     
00377   }
00378   
00379   // select error cases
00380   if (L_n < 0) {
00381     switch (errno) {
00382     case EINTR :
00383       /*
00384        * NOTHING TO DO, AND MUST DO
00385        * SELECT HAS BEEN INTERRUPTED (EINTR) BY A SIGNAL
00386        */
00387       break;
00388     default :
00389       
00390       GEN_ERROR(E_GEN_FATAL_ERROR, 
00391                 "select failed " << strerror(errno));
00392       break;
00393     }
00394   }
00395 
00396   // if L_n == 0 => nothing to do => no event => just timeout 
00397   GEN_DEBUG(1, "select return = " << L_n);
00398 
00399   // select popped with events
00400   if (L_n > 0) {
00401     L_nb_event = m_max_event_nb ; 
00402     L_error = m_transport->post_select
00403       (L_n, &L_ReadMask,&L_WriteMask, &L_ExceptionMask,
00404        m_events,
00405        (size_t*)&L_nb_event);
00406     
00407     if (L_error <0) {
00408       GEN_ERROR(E_GEN_FATAL_ERROR, "Post select (" << L_error << ")");
00409     } else { // L_error >= 0
00410       
00411       GEN_DEBUG(1, "C_RemoteControl::receiveControl() nb events = " << L_nb_event);
00412       
00413       for (L_i = 0 ; L_i < L_nb_event ; L_i++) {
00414         L_event_occured = &m_events [L_i];
00415         L_event_id      = L_event_occured->m_id ;
00416         
00417         switch (L_event_occured->m_type) {
00418           
00419         case C_TransportEvent::E_TRANS_RECEIVED: {
00420           
00421           GEN_DEBUG(1, "C_RemoteControl::receiveControl() event");
00422           GEN_DEBUG(1, 
00423                     "C_RemoteControl::receiveControl() E_TRANS_RECEIVED id ["
00424                     << L_event_id << "]");
00425           while ((m_transport
00426                   ->get_message(L_event_occured->m_id, &L_currentRcvCtxt)) == true) {
00427             m_msg_remote_list -> push_back (L_currentRcvCtxt) ;
00428           }
00429           break ;
00430         }
00431         
00432         case C_TransportEvent::E_TRANS_CLOSED: {
00433           GEN_DEBUG(1, "C_RemoteControl::receiveControl() E_TRANS_CLOSED id["
00434                     << L_event_id << "]");
00435           break ;
00436         }
00437         
00438         case C_TransportEvent::E_TRANS_CONNECTION: {
00439           GEN_DEBUG(1, 
00440                     "C_ReadControl::receiveControl() E_TRANS_CONNECTION id["
00441                     << L_event_id << "]");
00442           break ;
00443         }
00444         
00445         case C_TransportEvent::E_TRANS_OPEN: {
00446           GEN_DEBUG(1, 
00447                     "C_ReadControl::receiveControl() E_TRANS_OPEN id ["
00448                     << L_event_id << "]");
00449         }
00450         
00451         default :
00452           break;
00453         } // switch L_event_occured
00454       } // for L_i
00455     } // if L_error
00456   } // if L_n > 0
00457 
00458   GEN_DEBUG(1, "C_RemoteControl::receiveControl() end");
00459   return (E_GEN_NO_ERROR);
00460   
00461 }
00462 
00463 
00464 void C_RemoteControl::messageReceivedControl () {
00465   
00466 
00467   int                 L_nbMsg, L_i ;
00468   
00469   C_MessageFrame     *L_msg = NULL ; 
00470   T_ReceiveMsgContext L_rcvCtxt ;
00471 
00472   C_MessageFrame     *L_msg_send  = NULL  ;
00473 
00474   L_nbMsg = m_msg_remote_list -> size();
00475 
00476   for(L_i=0; L_i < L_nbMsg; L_i++) {
00477 
00478     L_rcvCtxt = *(m_msg_remote_list -> begin()) ;
00479     m_msg_remote_list -> erase (m_msg_remote_list->begin()) ;
00480 
00481     L_msg = L_rcvCtxt.m_msg ;
00482     L_msg_send = analyze_command(L_msg);
00483 
00484     if (L_msg_send != NULL) {
00485       m_transport->send_message(L_rcvCtxt.m_response, L_msg_send);
00486       DELETE_VAR(L_msg_send);
00487     }
00488     
00489     DELETE_VAR(L_msg);
00490   }
00491 }
00492 
00493 char *C_RemoteControl::resultOK() {
00494   char *L_result = NULL ;
00495   ALLOC_TABLE(L_result,
00496               char*,
00497               sizeof(char),
00498               50);
00499   sprintf(L_result, "%s", "HTTP/1.1 200 OK\r\n");
00500   return (L_result);
00501 }
00502 
00503 char *C_RemoteControl::resultKO() {
00504   char *L_result = NULL ;
00505   ALLOC_TABLE(L_result,
00506               char*,
00507               sizeof(char),
00508               50);
00509   sprintf(L_result, "%s", "HTTP/1.1 400 Bad Request\r\n");
00510   return (L_result);
00511 }
00512 
00513 char* C_RemoteControl::dump_stat() {
00514 
00515   return(m_stat->dumpCounters()) ;
00516 
00517 }
00518 
00519 void C_RemoteControl::rate(unsigned long P_value) {
00520   m_gen->change_call_rate(E_GEN_OP_SET_VALUE, P_value);
00521 }
00522 
00523 void C_RemoteControl::createRampThread(unsigned long P_duration,
00524                                        unsigned long P_current_rate,
00525                                        unsigned long P_sub_rate,
00526                                        bool          P_increase) {
00527   // create thread
00528   pthread_t              *L_rampThread = NULL;
00529   C_RampControl          *L_rampCtrl ;
00530 
00531   NEW_VAR(L_rampCtrl, C_RampControl(m_gen));
00532   L_rampCtrl->init(P_duration,
00533                    P_current_rate,
00534                    P_sub_rate,
00535                    P_increase);
00536   
00537   L_rampThread = start_thread_control(L_rampCtrl);
00538 }
00539 
00540 void C_RemoteControl::quit() {
00541   m_gen->stop() ;
00542 }
00543 
00544 void C_RemoteControl::pause() {
00545   m_gen->pause_traffic() ;
00546 }
00547 
00548 void C_RemoteControl::resume() {
00549   m_gen->restart_traffic() ;
00550 }
00551 
00552 void C_RemoteControl::ramp(unsigned long P_value, unsigned long P_duration) {
00553 
00554   unsigned long L_current_rate ;
00555   unsigned long L_diff_rate ;
00556   unsigned long L_sub_rate ;
00557   bool          L_increase = true ;
00558 
00559   if (P_duration == 0) {
00560     rate(P_value);
00561   } else {
00562 
00563     L_current_rate = m_gen->get_call_rate() ;
00564     
00565     if (P_value > L_current_rate) {
00566       L_diff_rate = P_value - L_current_rate ;
00567       L_sub_rate = L_diff_rate / P_duration ;
00568     } else {
00569       L_increase = false ;
00570       L_diff_rate = L_current_rate - P_value ;
00571       L_sub_rate = L_diff_rate / P_duration ;
00572     }
00573 
00574     if (P_value != L_current_rate) {
00575       createRampThread(P_duration,L_current_rate,L_sub_rate,L_increase);
00576     }
00577   }
00578 }
00579 
00580 char *C_RemoteControl::find_directory(char *P_buf,char *P_dir) {
00581 
00582   char *L_value = NULL ;
00583 
00584   regex_t    L_reg_expr ;
00585   int        L_status ;
00586   char       L_buffer[100];
00587   regmatch_t L_pmatch[3] ;
00588 
00589   string_t   L_string = "" ;
00590   
00591   L_string  = "([[:blank:]]*" ;
00592   L_string += P_dir ;
00593   L_string += "[[:blank:]]*)";
00594 
00595   L_status = regcomp (&L_reg_expr, 
00596                       L_string.c_str(),
00597                       REG_EXTENDED) ;
00598 
00599   if (L_status != 0) {
00600     regerror(L_status, &L_reg_expr, L_buffer, 100);
00601     regfree (&L_reg_expr) ;
00602 
00603   } else {
00604     L_status = regexec (&L_reg_expr, P_buf, 2, L_pmatch, 0) ;
00605     regfree (&L_reg_expr) ;
00606     if (L_status == 0) {
00607       L_value =  &P_buf[L_pmatch[1].rm_eo] ;
00608     } 
00609   }
00610 
00611   // position pas forcement /
00612   return (L_value);
00613 }
00614 
00615 char *C_RemoteControl::find_file(char *P_buf,char *P_dir) {
00616   char *L_value = NULL ;
00617 
00618   regex_t    L_reg_expr ;
00619   int        L_status ;
00620   char       L_buffer[100];
00621   regmatch_t L_pmatch[3] ;
00622   //  size_t     L_size = 0 ;
00623 
00624   string_t   L_string = "" ;
00625   string_t   L_string_end = "" ;
00626   string_t   L_string_tmp = "" ;
00627   
00628   L_string  = "([[:blank:]]*" ;
00629   L_string += P_dir ;
00630   L_string += "[[:blank:]]*";
00631 
00632   L_string_tmp = L_string + "\?)";
00633   L_string_end = L_string + "$)";
00634 
00635   // case tmp
00636   L_status = regcomp (&L_reg_expr, 
00637                       L_string_tmp.c_str(),
00638                       REG_EXTENDED) ;
00639 
00640   if (L_status != 0) {
00641     regerror(L_status, &L_reg_expr, L_buffer, 100);
00642     regfree (&L_reg_expr) ;
00643   } else {
00644     L_status = regexec (&L_reg_expr, P_buf, 2, L_pmatch, 0) ;
00645     regfree (&L_reg_expr) ;
00646     if (L_status == 0) {
00647       L_value =  &P_buf[L_pmatch[1].rm_eo] ;
00648     } else {
00649       // case end 
00650       L_status = regcomp (&L_reg_expr, 
00651                           L_string_end.c_str(),
00652                           REG_EXTENDED) ;
00653       
00654       if (L_status != 0) {
00655         regerror(L_status, &L_reg_expr, L_buffer, 100);
00656         regfree (&L_reg_expr) ;
00657       } else {
00658         L_status = regexec (&L_reg_expr, P_buf, 2, L_pmatch, 0) ;
00659         regfree (&L_reg_expr) ;
00660         if (L_status == 0) {
00661           L_value =  &P_buf[L_pmatch[1].rm_eo] ;
00662         }
00663       }
00664     }
00665   }
00666   return(L_value);
00667 }
00668 
00669 char* C_RemoteControl::find_value (char *P_buf, char *P_dir) {
00670 
00671   char *L_value = NULL ;
00672 
00673   regex_t    L_reg_expr ;
00674   int        L_status ;
00675   char       L_buffer[100];
00676   regmatch_t L_pmatch[3] ;
00677   size_t     L_size = 0 ;
00678 
00679   string_t   L_string = "" ;
00680 
00681 
00682   L_string  = "([[:blank:]&]*" ;
00683   L_string += P_dir ;
00684   L_string += "[^[:blank:]=]*[[:blank:]]*=[[:blank:]]*)([^&]*)";
00685   
00686   if ((P_buf[0] == '?') && (P_buf+1)) { 
00687     P_buf++;
00688     
00689     L_status = regcomp (&L_reg_expr, 
00690                         L_string.c_str(),
00691                         REG_EXTENDED) ;
00692     
00693     if (L_status != 0) {
00694       regerror(L_status, &L_reg_expr, L_buffer, 100);
00695       regfree (&L_reg_expr) ;
00696       
00697     } else {
00698       
00699       L_status = regexec (&L_reg_expr, P_buf, 3, L_pmatch, 0) ;
00700       regfree (&L_reg_expr) ;
00701       
00702       if (L_status == 0) {
00703         
00704         L_size = L_pmatch[2].rm_eo - L_pmatch[2].rm_so ;
00705         ALLOC_TABLE(L_value, char*, sizeof(char), L_size+1);
00706         memcpy(L_value, &(P_buf[L_pmatch[2].rm_so]), L_size);
00707         L_value[L_size]='\0' ;
00708       } 
00709     }
00710   }
00711 
00712   return (L_value);
00713 }
00714 
00715 char* C_RemoteControl::decode_put_uri(char *P_uri) {
00716 
00717   char *L_result = NULL ;
00718   char *L_ptr = P_uri ;
00719   
00720   L_ptr = find_directory(L_ptr,(char*)"seagull");
00721   if (L_ptr) {
00722     L_ptr = find_directory(L_ptr,(char*)"command");
00723     if (L_ptr) {
00724       L_result = decode_uri(L_ptr);
00725     } 
00726   }
00727   return (L_result);
00728 }
00729 
00730 char* C_RemoteControl::decode_uri(char *P_uri) {
00731 
00732   char *L_result = NULL ;
00733   char *L_ptr = P_uri ;
00734   char *L_ptr_value = NULL ;
00735 
00736   unsigned long L_value = 0 ;
00737   unsigned long L_duration = 0 ;
00738   char *L_end_str = NULL ;
00739   char *L_file ;  
00740 
00741 
00742   L_file = find_file(L_ptr,(char*)"rate");
00743   if (L_file) {
00744     L_ptr_value = find_value(L_file,(char*)"value");
00745     if (L_ptr_value) {
00746       L_value = strtoul_f(L_ptr_value, &L_end_str,10) ;
00747       if (L_end_str[0] != '\0') { // not a number
00748       } else {
00749         rate(L_value);
00750         L_result = resultOK() ;
00751       }
00752       FREE_TABLE(L_ptr_value);
00753     }
00754     return (L_result);
00755   }  
00756     
00757   L_file = find_file(L_ptr, (char*)"ramp");
00758   if (L_file) {
00759     L_ptr_value = find_value(L_file, (char*)"value");
00760     if (L_ptr_value) {
00761       L_value = strtoul_f(L_ptr_value, &L_end_str,10) ;
00762       if (L_end_str[0] != '\0') { // not a number
00763         FREE_TABLE(L_ptr_value);
00764       } else {
00765         FREE_TABLE(L_ptr_value);
00766         L_ptr_value = find_value(L_file, (char*)"duration");
00767         if (L_ptr_value) {
00768           L_duration = strtoul_f(L_ptr_value, &L_end_str,10) ;
00769           if (L_end_str[0] != '\0') { // not a number
00770             FREE_TABLE(L_ptr_value);
00771           } else {
00772             ramp(L_value, L_duration);
00773             FREE_TABLE(L_ptr_value);
00774             L_result = resultOK() ;
00775           }
00776         }
00777       }
00778     } // find value
00779     return (L_result);
00780   } // find_file for ramp 
00781   
00782   L_file = find_file(L_ptr,(char*)"stop");
00783   if (L_file) {
00784     L_result = resultOK() ;
00785     quit();
00786     return (L_result);
00787   }  
00788 
00789   L_file = find_file(L_ptr,(char*)"pause");
00790   if (L_file) {
00791     L_result = resultOK() ;
00792     pause();
00793     return (L_result);
00794   }  
00795 
00796   L_file = find_file(L_ptr,(char*)"resume");
00797   if (L_file) {
00798     L_result = resultOK() ;
00799     resume();
00800     return (L_result);
00801   }  
00802 
00803   return (L_result);
00804 }
00805 
00806 
00807 char* C_RemoteControl::decode_get_uri(char *P_uri, char **P_result_data) {
00808   char *L_result        = NULL  ;
00809   char *L_ptr           = P_uri ;
00810   char *L_result_ptr    = NULL  ;
00811 
00812   L_ptr = find_directory(L_ptr,(char*)"seagull");
00813   if (L_ptr) {
00814     L_result_ptr = find_directory(L_ptr,(char*)"counters");
00815     if (L_result_ptr) {
00816       L_ptr = L_result_ptr ;
00817       L_result_ptr = find_file(L_ptr,(char*)"all");
00818       if (L_result_ptr) {
00819         (*P_result_data) = dump_stat();
00820         L_result = resultOK();
00821       }
00822     } else {
00823       L_result_ptr = find_directory(L_ptr,(char*)"command");
00824       if (L_result_ptr) {
00825         L_result = decode_uri(L_result_ptr);
00826       }
00827     }
00828   }
00829   return (L_result);
00830 }
00831 
00832 
00833 char * C_RemoteControl::execute_command(char *P_cmd, char *P_uri, 
00834                                         char **P_result_data) {
00835 
00836   char *L_result = NULL ;
00837   
00838   if (P_cmd != NULL) {
00839 
00840     if (strcmp(P_cmd, (char*)"PUT")==0) {
00841       L_result = decode_put_uri (P_uri) ;
00842     } else if (strcmp(P_cmd, (char*)"GET")==0) {
00843       L_result = decode_get_uri (P_uri,P_result_data) ;
00844     } 
00845 
00846   }
00847   if (L_result == NULL) {
00848     L_result = resultKO() ;
00849   }
00850   
00851   return (L_result);
00852 }
00853 
00854 C_MessageFrame* C_RemoteControl::analyze_command(C_MessageFrame *P_msg) {
00855 
00856   char          *L_result = NULL ;
00857   char          *L_result_data = NULL ;
00858   C_MessageText *L_msg_send  = NULL  ;
00859   char          *L_uri = NULL ;
00860   char          *L_cmd = NULL ;
00861   bool           L_continue = false ;
00862 
00863   T_ValueData   L_cmd_data, L_uri_data ;
00864 
00865   L_cmd_data.m_type = E_TYPE_NUMBER ;
00866   L_uri_data.m_type = E_TYPE_NUMBER ;
00867 
00868   L_continue = P_msg->get_field_value(m_command_id,
00869                                       0,0,&L_cmd_data); 
00870   if (L_continue) {
00871     L_cmd = create_string(L_cmd_data) ;
00872     resetMemory(L_cmd_data);
00873     L_continue = P_msg->get_field_value(m_uri_id,
00874                                         0,0,&L_uri_data); 
00875   }
00876 
00877   if (L_continue) {
00878     L_uri = create_string(L_uri_data) ;
00879     resetMemory(L_uri_data);
00880     L_result = execute_command(L_cmd, L_uri, &L_result_data);
00881     FREE_TABLE(L_cmd);
00882     FREE_TABLE(L_uri);
00883   }
00884   if (L_result != NULL) {
00885     L_msg_send = m_protocol_text -> create (m_protocol_text,
00886                                             L_result,
00887                                             (L_result_data == NULL) ? 
00888                                             (char*)"Command Success" :
00889                                             L_result_data) ;
00890   }
00891 
00892   return (L_msg_send);
00893 
00894 }
00895 
00896 
00897 
00898 
00899 // end of file
00900 
00901 
00902 
00903 

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