Change license from LGPL to GPL version 2 + linking exception. This fixes C++ templat...
[libt2n] / src / socket_wrapper.cpp
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 */
22
23 #include <functional>
24 #include <string>
25
26 #include <socket_wrapper.hxx>
27
28 namespace libt2n
29 {
30
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
41 /// return active connection, create new tcp or unix connection if not existing
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>
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));
52     }
53
54     return c.get();
55 }
56
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)
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
74             return true;
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     }
97
98     return false;
99 }
100
101 /// return active connection, return a dummy-connection if we can't establish one
102 client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void)
103 {
104     client_connection* tmp=BasicSocketWrapper::get_connection();
105
106     if (tmp->is_closed())
107     {
108         // throw away the closed connection...
109         c.reset();
110         // ...return the dummy one instead
111         tmp=&dc;
112     }
113
114     return tmp;
115 }
116
117 /// try to execute the command, may ignore the command if server not available
118 bool ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
119 {
120     if (!connection_established())
121     {
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         }
130     }
131
132     // only try to handle the call if we've got a real connection
133     if (connection_established())
134     {
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         }
148     }
149
150     return false;
151 }
152
153 }