Commit | Line | Data |
---|---|---|
19facd85 TJ |
1 | /* |
2 | Copyright (C) 2008 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 | */ | |
ffbbf9ab GE |
22 | |
23 | #include <functional> | |
24 | #include <string> | |
25 | ||
26 | #include <socket_wrapper.hxx> | |
27 | ||
28 | namespace libt2n | |
29 | { | |
30 | ||
2a956e65 GE |
31 | /// set logging for coming and current connections |
32 | void BasicSocketWrapper::set_logging(std::ostream *_logstream, log_level_values _log_level) | |
33 | { | |
34 | ConnectionWrapper::set_logging(_logstream,_log_level); | |
35 | ||
36 | if (connection_established()) | |
37 | get_connection()->set_logging(_logstream,_log_level); | |
38 | } | |
39 | ||
40 | ||
487afb79 | 41 | /// return active connection, create new tcp or unix connection if not existing |
ffbbf9ab GE |
42 | client_connection* BasicSocketWrapper::get_connection(void) |
43 | { | |
44 | if (c.get() == NULL) | |
45 | { | |
46 | if (socket_type == tcp_s) | |
47 | c=std::auto_ptr<socket_client_connection> | |
e1614a6d | 48 | (new socket_client_connection(port,server,connect_timeout_usec,max_retries,logstream,log_level)); |
ffbbf9ab GE |
49 | else if (socket_type == unix_s) |
50 | c=std::auto_ptr<socket_client_connection> | |
e1614a6d | 51 | (new socket_client_connection(path,connect_timeout_usec,max_retries,logstream,log_level)); |
ffbbf9ab GE |
52 | } |
53 | ||
54 | return c.get(); | |
55 | } | |
56 | ||
487afb79 GE |
57 | /// try to reconnect max_retries time if we encounter a t2n_communication_error |
58 | /// during execution of the command | |
fb3345ad | 59 | bool ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) |
ffbbf9ab GE |
60 | { |
61 | int tries=0; | |
62 | ||
63 | while(true) | |
64 | { | |
65 | try | |
66 | { | |
67 | // we always reconnect if something went wrong before: | |
68 | // makes sure the buffers are clean of half-sent packets etc. | |
69 | if (tries > 0) | |
70 | c->reconnect(); | |
71 | ||
72 | f(); | |
73 | // we were successful | |
fb3345ad | 74 | return true; |
ffbbf9ab GE |
75 | } |
76 | catch(t2n_connect_error &e) | |
77 | { | |
78 | // reconnect already tries max_tries times to reconnect: we are done if that failed | |
79 | throw(e); | |
80 | } | |
81 | catch(t2n_communication_error &e) | |
82 | { | |
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) | |
86 | throw(e); | |
87 | ||
88 | // otherwise ignore the exception and reconnect in the next iteration | |
89 | } | |
ffbbf9ab GE |
90 | |
91 | tries++; | |
92 | } | |
fb3345ad GE |
93 | |
94 | return false; | |
ffbbf9ab GE |
95 | } |
96 | ||
487afb79 | 97 | /// return active connection, return a dummy-connection if we can't establish one |
ffbbf9ab GE |
98 | client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void) |
99 | { | |
fb3345ad | 100 | client_connection* tmp=BasicSocketWrapper::get_connection(); |
ffbbf9ab | 101 | |
fb3345ad | 102 | if (tmp->is_closed()) |
ffbbf9ab | 103 | { |
fb3345ad GE |
104 | // throw away the closed connection... |
105 | c.reset(); | |
106 | // ...return the dummy one instead | |
107 | tmp=&dc; | |
ffbbf9ab GE |
108 | } |
109 | ||
110 | return tmp; | |
111 | } | |
112 | ||
e162ddf2 | 113 | /// try to execute the command, may ignore the command if server not available |
fb3345ad | 114 | bool ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) |
ffbbf9ab | 115 | { |
fb3345ad | 116 | if (!connection_established()) |
ffbbf9ab | 117 | { |
fb3345ad GE |
118 | // dummy connection is in place: try to establish a real one |
119 | client_connection* tmp=get_connection(); | |
120 | ||
121 | if (tmp != &dc) | |
122 | { | |
123 | // success! we've got a real connection | |
124 | stubBase->replace_connection(tmp); | |
125 | } | |
ffbbf9ab | 126 | } |
fb3345ad GE |
127 | |
128 | // only try to handle the call if we've got a real connection | |
129 | if (connection_established()) | |
ffbbf9ab | 130 | { |
fb3345ad GE |
131 | try |
132 | { | |
133 | ReconnectSocketWrapper::handle(stubBase,f); | |
134 | return true; | |
135 | } | |
136 | catch(t2n_communication_error &e) | |
137 | { | |
138 | // ignore | |
139 | } | |
ffbbf9ab | 140 | } |
fb3345ad GE |
141 | |
142 | return false; | |
ffbbf9ab GE |
143 | } |
144 | ||
145 | } |