Intra2net AG

socket_wrapper.hxx

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
00003 
00004 The software in this package is distributed under the GNU General
00005 Public License version 2 (with a special exception described below).
00006 
00007 A copy of GNU General Public License (GPL) is included in this distribution,
00008 in the file COPYING.GPL.
00009 
00010 As a special exception, if other files instantiate templates or use macros
00011 or inline functions from this file, or you compile this file and link it
00012 with other works to produce a work based on this file, this file
00013 does not by itself cause the resulting work to be covered
00014 by the GNU General Public License.
00015 
00016 However the source code for this file must still be made available
00017 in accordance with section (3) of the GNU General Public License.
00018 
00019 This exception does not invalidate any other reasons why a work based
00020 on this file might be covered by the GNU General Public License.
00021 */
00022 
00023 #ifndef __LIBT2N_SOCKET_WRAPPER
00024 #define __LIBT2N_SOCKET_WRAPPER
00025 
00026 #include <functional>
00027 #include <string>
00028 
00029 #include <client.hxx>
00030 #include <command_client.hxx>
00031 #include <types.hxx>
00032 #include <client_wrapper.hxx>
00033 #include <socket_client.hxx>
00034 
00035 namespace libt2n
00036 {
00037 
00044 class BasicSocketWrapper : public ConnectionWrapper
00045 {
00046     protected:
00047         socket_type_value socket_type;
00048 
00049         std::string path;
00050         std::string server;
00051         int port;
00052 
00053         long long connect_timeout_usec;
00054         int max_retries;
00055 
00056         std::auto_ptr<socket_client_connection> c;
00057 
00058         // TODO: Mark object as non-copyable as it contains an auto_ptr.
00059         //       This will make sure nobody every tries to put this in a STL container
00060 
00061     public:
00062         BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
00063             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00064             int _max_retries=socket_client_connection::max_retries_default)
00065             : ConnectionWrapper(),
00066               socket_type(tcp_s),
00067               server(_server),
00068               port(_port),
00069               connect_timeout_usec(_connect_timeout_usec),
00070               max_retries(_max_retries)
00071             { }
00072 
00073         BasicSocketWrapper(const std::string& _path,
00074             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00075             int _max_retries=socket_client_connection::max_retries_default)
00076             : ConnectionWrapper(),
00077               socket_type(unix_s),
00078               path(_path),
00079               connect_timeout_usec(_connect_timeout_usec),
00080               max_retries(_max_retries)
00081             { }
00082 
00083         client_connection* get_connection(void);
00084 
00085         bool connection_established(void)
00086             { return (c.get() != NULL); }
00087 
00088         void set_logging(std::ostream *_logstream, log_level_values _log_level);
00089 };
00090 
00096 class ReconnectSocketWrapper : public BasicSocketWrapper
00097 {
00098     public:
00099         ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
00100             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00101             int _max_retries=socket_client_connection::max_retries_default)
00102             : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
00103             { }
00104 
00105         ReconnectSocketWrapper(const std::string& _path,
00106             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00107             int _max_retries=socket_client_connection::max_retries_default)
00108             : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
00109             { }
00110 
00111         bool handle(command_client* stubBase, boost::function< void() > f);
00112 };
00113 
00115 class dummy_client_connection : public client_connection
00116 {
00117     private:
00118         void real_write(const std::string& data)
00119             { }
00120 
00121     public:
00122         dummy_client_connection()
00123             : client_connection()
00124             { close(); }
00125 
00126         bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
00127             { return false; }
00128 };
00129 
00136 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
00137 {
00138     private:
00139         dummy_client_connection dc;
00140 
00141     public:
00142         ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
00143             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00144             int _max_retries=socket_client_connection::max_retries_default)
00145             : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
00146             { }
00147 
00148         ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
00149             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
00150             int _max_retries=socket_client_connection::max_retries_default)
00151             : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
00152             { }
00153 
00154         client_connection* get_connection(void);
00155         bool handle(command_client* stubBase, boost::function< void() > f);
00156 };
00157 
00158 }
00159 
00160 #endif

Generated on 13 May 2015 by  doxygen 1.6.1
© Intra2net AG 2024 | Legal | Contact