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 : ConnectionWrapper(),
69 connect_timeout_usec(_connect_timeout_usec),
70 max_retries(_max_retries)
73 BasicSocketWrapper(const std::string& _path,
74 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
75 int _max_retries=socket_client_connection::max_retries_default)
76 : ConnectionWrapper(),
79 connect_timeout_usec(_connect_timeout_usec),
80 max_retries(_max_retries)
83 client_connection* get_connection(void);
85 bool connection_established(void)
86 { return (c.get() != NULL); }
88 void set_logging(std::ostream *_logstream, log_level_values _log_level);
91 /** @brief a wrapper implementing reconnect-then-throw
93 This ConnectionWrapper tries to reconnect to the server if something with the connection
94 goes wrong. If even reconnecting max_retries times does not help, an exception is thrown.
96 class ReconnectSocketWrapper : public BasicSocketWrapper
99 ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1",
100 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
101 int _max_retries=socket_client_connection::max_retries_default)
102 : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
105 ReconnectSocketWrapper(const std::string& _path,
106 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
107 int _max_retries=socket_client_connection::max_retries_default)
108 : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
111 bool handle(command_client* stubBase, boost::function< void() > f);
114 /// a placeholder-client_connection which is closed all the time
115 class dummy_client_connection : public client_connection
118 void real_write(const std::string& data)
122 dummy_client_connection()
123 : client_connection()
126 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
130 /** @brief a wrapper implementing reconnect-then-ignore
132 This ConnectionWrapper tries to reconnect to the server if something with the connection
133 goes wrong. If even reconnecting max_retries times does not help, the complete t2n-call is
134 ignored. The return value of the call will be created with the default constructor.
136 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
139 dummy_client_connection dc;
142 ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1",
143 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
144 int _max_retries=socket_client_connection::max_retries_default)
145 : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
148 ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
149 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
150 int _max_retries=socket_client_connection::max_retries_default)
151 : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
154 client_connection* get_connection(void);
155 bool handle(command_client* stubBase, boost::function< void() > f);