2 Copyright (C) 2008 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.
26 #include <socket_wrapper.hxx>
31 /// set logging for coming and current connections
32 void BasicSocketWrapper::set_logging(std::ostream *_logstream, log_level_values _log_level)
34 ConnectionWrapper::set_logging(_logstream,_log_level);
36 if (connection_established())
37 get_connection()->set_logging(_logstream,_log_level);
41 /// return active connection, create new tcp or unix connection if not existing
42 client_connection* BasicSocketWrapper::get_connection(void)
46 if (socket_type == tcp_s)
47 c=std::auto_ptr<socket_client_connection>
48 (new socket_client_connection(port,server,connect_timeout_usec,max_retries,logstream,log_level));
49 else if (socket_type == unix_s)
50 c=std::auto_ptr<socket_client_connection>
51 (new socket_client_connection(path,connect_timeout_usec,max_retries,logstream,log_level));
57 /// try to reconnect max_retries time if we encounter a t2n_communication_error
58 /// during execution of the command
59 bool ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
67 // we always reconnect if something went wrong before:
68 // makes sure the buffers are clean of half-sent packets etc.
76 catch(t2n_connect_error &e)
78 // reconnect already tries max_tries times to reconnect: we are done if that failed
81 catch(t2n_communication_error &e)
83 // aborts the loop with an exception after max_retries iterations
84 // retries means that the first try doesn't count: use >
85 if (tries > max_retries)
88 // otherwise ignore the exception and reconnect in the next iteration
97 /// return active connection, return a dummy-connection if we can't establish one
98 client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void)
100 client_connection* tmp=BasicSocketWrapper::get_connection();
102 if (tmp->is_closed())
104 // throw away the closed connection...
106 // ...return the dummy one instead
113 /// try to execute the command, may ignore the command if server not available
114 bool ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
116 if (!connection_established())
118 // dummy connection is in place: try to establish a real one
119 client_connection* tmp=get_connection();
123 // success! we've got a real connection
124 stubBase->replace_connection(tmp);
128 // only try to handle the call if we've got a real connection
129 if (connection_established())
133 ReconnectSocketWrapper::handle(stubBase,f);
136 catch(t2n_communication_error &e)