hsmsd_cnx_proxy.h

00001 /*   
00002  *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net)
00003  *
00004  *   This file is part of FREESECS.
00005  *
00006  *   FREESECS is free software: you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation, either version 3 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   FREESECS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with FREESECS, see COPYING.
00018  *   If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #ifndef _HSMSD_CNX_PROXY_H
00021 #define _HSMSD_CNX_PROXY_H
00022 
00023 #include <string>
00024 #include <vector>
00025 #include <semaphore.h>
00026 #include <pthread.h>
00027 #include "shared_ptr.h"
00028 #include "event_pump.h"
00029 #include "async_reception.h"
00030 #include "hsmsd_interface.h"
00031 #include "hsmsd_cli.h"
00032 
00033 namespace freesecs
00034 {
00038     class hsmsd_proxy_t
00039     {
00040     public:
00041         hsmsd_proxy_t(const char *cnx_name);
00042         ~hsmsd_proxy_t();
00043 
00044         int set_msg_handler(hsmsd_msg_handler_t);
00045         int set_cnx_state_handler(hsmsd_cnx_state_handler_t);
00046         int set_cnx_error_handler(hsmsd_cnx_error_handler_t);
00047 
00048 
00049         int start();
00050         int stop();
00051         int get_state(hsmsd_cnx_state_t*);
00052         int send_msg(hsmsd_msg_t*);
00053 
00054     public:
00055         int ctrl_rcvd_handler(const size_t&);
00056         int data_rcvd_handler(const size_t&);
00057         int rcv_error_handler(const int&);
00058 
00059     private:
00060         std::string                 _cnx_name;
00061         hsmsd_msg_t                 _rsp_msg_hdr;
00062         hsmsd_msg_t                 _data_msg_hdr;
00063         std::vector<unsigned char>  _msg_data;
00064 
00065     private:
00066         hsmsd_server_rsp_t          _srv_ctl_msg;
00067         hsmsd_server_rsp_t          _srv_data_msg;
00068         hsmsd_server_rsp_t          _srv_msg_pool[hsmsd_server_rsp_t::MAX_RSP_TYPE];
00069         sem_t                       _rsp_sem_pool[hsmsd_server_rsp_t::MAX_RSP_TYPE];
00070 
00071         
00072     private:
00073         int                             _fd_ctl_in;
00074         int                             _fd_ctl_out;
00075         int                             _fd_data_in;
00076         int                             _fd_data_out;
00077         shared_ptr_t<async_reception_t> _ctl_ar;
00078         shared_ptr_t<async_reception_t> _data_ar;
00079         int                             _rstate;
00080 
00081     private:
00082         hsmsd_msg_handler_t         _msg_h;
00083         hsmsd_cnx_state_handler_t   _cnx_state_h;
00084         hsmsd_cnx_error_handler_t   _cnx_error_h;
00085 
00086     private:
00092         class event_base_t
00093         {
00094             public:
00095                 event_base_t(){};
00096                 virtual ~event_base_t(){};
00097                 virtual void operator()(void) const = 0;
00098                 /*{
00099                     printf("event base :(\n");
00100                 };*/
00101         };
00102 
00103         template<typename cb_t, typename arg_t>
00104         class event_to_client_t : public event_base_t
00105         {
00106             public:
00107                 event_to_client_t(cb_t cb, arg_t arg)
00108                 :_cb(cb),_arg(arg){}
00109 
00110                 event_to_client_t(const event_to_client_t& other){_cb = other._cb; _arg = other._arg;}
00111                 event_to_client_t & operator = (const event_to_client_t & other)
00112                 {_cb = other._cb; _arg = other._arg; return *this;}
00113 
00114                 virtual void operator()(void) const 
00115                 {
00116                     _cb(_arg);
00117                 }
00118 
00119             private:
00120                 cb_t _cb;
00121                 arg_t _arg;
00122         };
00123         typedef shared_ptr_t<event_base_t> pevent_base_t;
00124 
00125         class pump_events_to_client_t : public threaded_event_pump_t<pevent_base_t>
00126         {
00127             private:
00128                 virtual int process(const pevent_base_t& e)
00129                 { 
00130                     (*e.get())(); 
00131                     return 0;
00132                 }
00133         }; 
00134         static pump_events_to_client_t client_event_pump;
00135         
00136         typedef event_to_client_t<hsmsd_cnx_state_handler_t,hsmsd_cnx_state_t>  state_event_t;
00137         typedef event_to_client_t<hsmsd_cnx_error_handler_t, int>               error_event_t;
00138         typedef event_to_client_t<hsmsd_msg_handler_t, hsmsd_msg_t*>            msg_event_t;
00139     };
00140 }//namespace
00141 #endif /*_HSMSD_CNX_PROXY_H*/

Generated on Fri Oct 3 15:30:01 2008 for FREESECS hsms by  doxygen 1.5.1