2 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
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.
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.
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.
23 #ifndef __LIBT2N_SOCKET_WRAPPER
24 #define __LIBT2N_SOCKET_WRAPPER
30 #include <command_client.hxx>
32 #include <client_wrapper.hxx>
33 #include <socket_client.hxx>
38 /** @brief a basic implementation of ConnectionWrapper
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.
44 class BasicSocketWrapper : public ConnectionWrapper
47 socket_type_value socket_type;
53 long long connect_timeout_usec;
56 std::auto_ptr<socket_client_connection> c;
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
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()
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()
76 client_connection* get_connection(void);
78 bool connection_established(void)
79 { return (c.get() != NULL); }
81 void set_logging(std::ostream *_logstream, log_level_values _log_level);
84 /** @brief a wrapper implementing reconnect-then-throw
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.
89 class ReconnectSocketWrapper : public BasicSocketWrapper
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)
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)
104 bool handle(command_client* stubBase, boost::function< void() > f);
107 /// a placeholder-client_connection which is closed all the time
108 class dummy_client_connection : public client_connection
111 void real_write(const std::string& data)
115 dummy_client_connection()
116 : client_connection()
119 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
123 /** @brief a wrapper implementing reconnect-then-ignore
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.
129 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
132 dummy_client_connection dc;
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)
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)
147 client_connection* get_connection(void);
148 bool handle(command_client* stubBase, boost::function< void() > f);