| 1 | /* |
| 2 | Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy |
| 3 | |
| 4 | The software in this package is distributed under the GNU General |
| 5 | Public License version 2 (with a special exception described below). |
| 6 | |
| 7 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 8 | in the file COPYING.GPL. |
| 9 | |
| 10 | As a special exception, if other files instantiate templates or use macros |
| 11 | or inline functions from this file, or you compile this file and link it |
| 12 | with other works to produce a work based on this file, this file |
| 13 | does not by itself cause the resulting work to be covered |
| 14 | by the GNU General Public License. |
| 15 | |
| 16 | However the source code for this file must still be made available |
| 17 | in accordance with section (3) of the GNU General Public License. |
| 18 | |
| 19 | This exception does not invalidate any other reasons why a work based |
| 20 | on this file might be covered by the GNU General Public License. |
| 21 | */ |
| 22 | |
| 23 | #ifndef __LIBT2N_SOCKET_WRAPPER |
| 24 | #define __LIBT2N_SOCKET_WRAPPER |
| 25 | |
| 26 | #include <functional> |
| 27 | #include <string> |
| 28 | |
| 29 | #include <client.hxx> |
| 30 | #include <command_client.hxx> |
| 31 | #include <types.hxx> |
| 32 | #include <client_wrapper.hxx> |
| 33 | #include <socket_client.hxx> |
| 34 | |
| 35 | namespace libt2n |
| 36 | { |
| 37 | |
| 38 | /** @brief a basic implementation of ConnectionWrapper |
| 39 | |
| 40 | This is a basic version of a ConnectionWrapper which does not do any fancy |
| 41 | error handling or anything, it justs executes the regular calls. Use this |
| 42 | wrapper if you only want to use the singleton-feature of T2nSingletonWrapper. |
| 43 | */ |
| 44 | class BasicSocketWrapper : public ConnectionWrapper |
| 45 | { |
| 46 | protected: |
| 47 | socket_type_value socket_type; |
| 48 | |
| 49 | std::string path; |
| 50 | std::string server; |
| 51 | int port; |
| 52 | |
| 53 | long long connect_timeout_usec; |
| 54 | int max_retries; |
| 55 | |
| 56 | std::auto_ptr<socket_client_connection> c; |
| 57 | |
| 58 | // TODO: Mark object as non-copyable as it contains an auto_ptr. |
| 59 | // This will make sure nobody every tries to put this in a STL container |
| 60 | |
| 61 | public: |
| 62 | BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1", |
| 63 | long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, |
| 64 | int _max_retries=socket_client_connection::max_retries_default) |
| 65 | : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec), |
| 66 | max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper() |
| 67 | { } |
| 68 | |
| 69 | BasicSocketWrapper(const std::string& _path, |
| 70 | long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, |
| 71 | int _max_retries=socket_client_connection::max_retries_default) |
| 72 | : path(_path), connect_timeout_usec(_connect_timeout_usec), |
| 73 | max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper() |
| 74 | { } |
| 75 | |
| 76 | client_connection* get_connection(void); |
| 77 | |
| 78 | bool connection_established(void) |
| 79 | { return (c.get() != NULL); } |
| 80 | |
| 81 | void set_logging(std::ostream *_logstream, log_level_values _log_level); |
| 82 | }; |
| 83 | |
| 84 | /** @brief a wrapper implementing reconnect-then-throw |
| 85 | |
| 86 | This ConnectionWrapper tries to reconnect to the server if something with the connection |
| 87 | goes wrong. If even reconnecting max_retries times does not help, an exception is thrown. |
| 88 | */ |
| 89 | class ReconnectSocketWrapper : public BasicSocketWrapper |
| 90 | { |
| 91 | public: |
| 92 | ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1", |
| 93 | long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, |
| 94 | int _max_retries=socket_client_connection::max_retries_default) |
| 95 | : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries) |
| 96 | { } |
| 97 | |
| 98 | ReconnectSocketWrapper(const std::string& _path, |
| 99 | long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, |
| 100 | int _max_retries=socket_client_connection::max_retries_default) |
| 101 | : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries) |
| 102 | { } |
| 103 | |
| 104 | bool handle(command_client* stubBase, boost::function< void() > f); |
| 105 | }; |
| 106 | |
| 107 | /// a placeholder-client_connection which is closed all the time |
| 108 | class dummy_client_connection : public client_connection |
| 109 | { |
| 110 | private: |
| 111 | void real_write(const std::string& data) |
| 112 | { } |
| 113 | |
| 114 | public: |
| 115 | dummy_client_connection() |
| 116 | : client_connection() |
| 117 | { close(); } |
| 118 | |
| 119 | bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL) |
| 120 | { return false; } |
| 121 | }; |
| 122 | |
| 123 | /** @brief a wrapper implementing reconnect-then-ignore |
| 124 | |
| 125 | This ConnectionWrapper tries to reconnect to the server if something with the connection |
| 126 | goes wrong. If even reconnecting max_retries times does not help, the complete t2n-call is |
| 127 | ignored. The return value of the call will be created with the default constructor. |
| 128 | */ |
| 129 | class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper |
| 130 | { |
| 131 | private: |
| 132 | dummy_client_connection dc; |
| 133 | |
| 134 | public: |
| 135 | ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1", |
| 136 | long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, |
| 137 | int _max_retries=socket_client_connection::max_retries_default) |
| 138 | : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries) |
| 139 | { } |
| 140 | |
| 141 | ReconnectIgnoreFailureSocketWrapper(const std::string& _path, |
| 142 | long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, |
| 143 | int _max_retries=socket_client_connection::max_retries_default) |
| 144 | : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries) |
| 145 | { } |
| 146 | |
| 147 | client_connection* get_connection(void); |
| 148 | bool handle(command_client* stubBase, boost::function< void() > f); |
| 149 | }; |
| 150 | |
| 151 | } |
| 152 | |
| 153 | #endif |