Change gccxml wrapper to work with older bash version. Hopefull this doesn't break...
[libt2n] / src / socket_wrapper.cpp
CommitLineData
19facd85
TJ
1/*
2Copyright (C) 2008 by Intra2net AG - Gerd v. Egidy
3
4The software in this package is distributed under the GNU General
5Public License version 2 (with a special exception described below).
6
7A copy of GNU General Public License (GPL) is included in this distribution,
8in the file COPYING.GPL.
9
10As a special exception, if other files instantiate templates or use macros
11or inline functions from this file, or you compile this file and link it
12with other works to produce a work based on this file, this file
13does not by itself cause the resulting work to be covered
14by the GNU General Public License.
15
16However the source code for this file must still be made available
17in accordance with section (3) of the GNU General Public License.
18
19This exception does not invalidate any other reasons why a work based
20on 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
28namespace libt2n
29{
30
2a956e65
GE
31/// set logging for coming and current connections
32void 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
42client_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 59bool 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 }
90 catch(...)
91 {
92 throw;
93 }
94
95 tries++;
96 }
fb3345ad
GE
97
98 return false;
ffbbf9ab
GE
99}
100
487afb79 101/// return active connection, return a dummy-connection if we can't establish one
ffbbf9ab
GE
102client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void)
103{
fb3345ad 104 client_connection* tmp=BasicSocketWrapper::get_connection();
ffbbf9ab 105
fb3345ad 106 if (tmp->is_closed())
ffbbf9ab 107 {
fb3345ad
GE
108 // throw away the closed connection...
109 c.reset();
110 // ...return the dummy one instead
111 tmp=&dc;
ffbbf9ab
GE
112 }
113
114 return tmp;
115}
116
e162ddf2 117/// try to execute the command, may ignore the command if server not available
fb3345ad 118bool ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
ffbbf9ab 119{
fb3345ad 120 if (!connection_established())
ffbbf9ab 121 {
fb3345ad
GE
122 // dummy connection is in place: try to establish a real one
123 client_connection* tmp=get_connection();
124
125 if (tmp != &dc)
126 {
127 // success! we've got a real connection
128 stubBase->replace_connection(tmp);
129 }
ffbbf9ab 130 }
fb3345ad
GE
131
132 // only try to handle the call if we've got a real connection
133 if (connection_established())
ffbbf9ab 134 {
fb3345ad
GE
135 try
136 {
137 ReconnectSocketWrapper::handle(stubBase,f);
138 return true;
139 }
140 catch(t2n_communication_error &e)
141 {
142 // ignore
143 }
144 catch(...)
145 {
146 throw;
147 }
ffbbf9ab 148 }
fb3345ad
GE
149
150 return false;
ffbbf9ab
GE
151}
152
153}