socket_wrapper.hxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Gerd v. Egidy                                   *
00003  *   gve@intra2net.com                                                     *
00004  *                                                                         *
00005  *   This library is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License version   *
00007  *   2.1 as published by the Free Software Foundation.                     *
00008  *                                                                         *
00009  *   This library is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU Lesser General Public License for more details.                   *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this program; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00018  ***************************************************************************/
00019 
00020 #ifndef __LIBT2N_SOCKET_WRAPPER
00021 #define __LIBT2N_SOCKET_WRAPPER
00022 
00023 #include <functional>
00024 #include <string>
00025 
00026 #include <client.hxx>
00027 #include <command_client.hxx>
00028 #include <types.hxx>
00029 #include <client_wrapper.hxx>
00030 #include <socket_client.hxx>
00031 
00032 namespace libt2n
00033 {
00034 
00041 class BasicSocketWrapper : public ConnectionWrapper
00042 {
00043     protected:
00044         socket_type_value socket_type;
00045 
00046         std::string path;
00047         std::string server;
00048         int port;
00049 
00050         long long connect_timeout_usec;
00051         int max_retries;
00052 
00053         std::auto_ptr<socket_client_connection> c;
00054 
00055     public:
00056         BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
00057             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00058             int _max_retries=socket_client_connection::max_retries_default)
00059             : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
00060               max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
00061             { }
00062 
00063         BasicSocketWrapper(const std::string& _path,
00064             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00065             int _max_retries=socket_client_connection::max_retries_default)
00066             : path(_path), connect_timeout_usec(_connect_timeout_usec),
00067               max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
00068             { }
00069 
00070         client_connection* get_connection(void);
00071 
00072         bool connection_established(void)
00073             { return (c.get() != NULL); }
00074 
00075         void set_logging(std::ostream *_logstream, log_level_values _log_level);
00076 };
00077 
00083 class ReconnectSocketWrapper : public BasicSocketWrapper
00084 {
00085     public:
00086         ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
00087             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00088             int _max_retries=socket_client_connection::max_retries_default)
00089             : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
00090             { }
00091 
00092         ReconnectSocketWrapper(const std::string& _path,
00093             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00094             int _max_retries=socket_client_connection::max_retries_default)
00095             : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
00096             { }
00097 
00098         bool handle(command_client* stubBase, boost::function< void() > f);
00099 };
00100 
00102 class dummy_client_connection : public client_connection
00103 {
00104     private:
00105         void real_write(const std::string& data)
00106             { }
00107 
00108     public:
00109         dummy_client_connection()
00110             : client_connection()
00111             { close(); }
00112 
00113         bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
00114             { return false; }
00115 };
00116 
00123 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
00124 {
00125     private:
00126         dummy_client_connection dc;
00127 
00128     public:
00129         ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
00130             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00131             int _max_retries=socket_client_connection::max_retries_default)
00132             : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
00133             { }
00134 
00135         ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
00136             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00137             int _max_retries=socket_client_connection::max_retries_default)
00138             : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
00139             { }
00140 
00141         client_connection* get_connection(void);
00142         bool handle(command_client* stubBase, boost::function< void() > f);
00143 };
00144 
00145 }
00146 
00147 #endif

Generated on Fri Sep 26 15:36:59 2008 for libt2n by  doxygen 1.5.6