Fix 'occurred' typo
[libt2n] / src / socket_wrapper.hxx
1 /*
2 Copyright (C) 2006 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 #ifndef __LIBT2N_SOCKET_WRAPPER
24 #define __LIBT2N_SOCKET_WRAPPER
25
26 #include <functional>
27 #include <string>
28
29 #include <client.hxx>
30 #include <command_client.hxx>
31 #include <types.hxx>
32 #include <client_wrapper.hxx>
33 #include <socket_client.hxx>
34
35 namespace libt2n
36 {
37
38 /** @brief a basic implementation of ConnectionWrapper
39
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.
43 */
44 class BasicSocketWrapper : public ConnectionWrapper
45 {
46     protected:
47         socket_type_value socket_type;
48
49         std::string path;
50         std::string server;
51         int port;
52
53         long long connect_timeout_usec;
54         int max_retries;
55
56         std::auto_ptr<socket_client_connection> c;
57
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
60
61     public:
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(),
66               socket_type(tcp_s),
67               server(_server),
68               port(_port),
69               connect_timeout_usec(_connect_timeout_usec),
70               max_retries(_max_retries)
71             { }
72
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(),
77               socket_type(unix_s),
78               path(_path),
79               connect_timeout_usec(_connect_timeout_usec),
80               max_retries(_max_retries)
81             { }
82
83         client_connection* get_connection(void);
84
85         bool connection_established(void)
86             { return (c.get() != NULL); }
87
88         void set_logging(std::ostream *_logstream, log_level_values _log_level);
89 };
90
91 /** @brief a wrapper implementing reconnect-then-throw
92
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.
95 */
96 class ReconnectSocketWrapper : public BasicSocketWrapper
97 {
98     public:
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)
103             { }
104
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)
109             { }
110
111         bool handle(command_client* stubBase, boost::function< void() > f);
112 };
113
114 /// a placeholder-client_connection which is closed all the time
115 class dummy_client_connection : public client_connection
116 {
117     private:
118         void real_write(const std::string& data)
119             { }
120
121     public:
122         dummy_client_connection()
123             : client_connection()
124             { close(); }
125
126         bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
127             { return false; }
128 };
129
130 /** @brief a wrapper implementing reconnect-then-ignore
131
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.
135 */
136 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
137 {
138     private:
139         dummy_client_connection dc;
140
141     public:
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)
146             { }
147
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)
152             { }
153
154         client_connection* get_connection(void);
155         bool handle(command_client* stubBase, boost::function< void() > f);
156 };
157
158 }
159
160 #endif