reset timeout if connection is active.
[libt2n] / src / socket_wrapper.hxx
CommitLineData
19facd85
TJ
1/*
2Copyright (C) 2006 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#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
35namespace libt2n
36{
37
9a5d7790
GE
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*/
ffbbf9ab
GE
44class 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
238ad35f
TJ
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
ffbbf9ab
GE
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 : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
e1614a6d 66 max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
ffbbf9ab
GE
67 { }
68
69 BasicSocketWrapper(const std::string& _path,
70 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
71 int _max_retries=socket_client_connection::max_retries_default)
72 : path(_path), connect_timeout_usec(_connect_timeout_usec),
e1614a6d 73 max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
ffbbf9ab
GE
74 { }
75
76 client_connection* get_connection(void);
fb3345ad
GE
77
78 bool connection_established(void)
79 { return (c.get() != NULL); }
2a956e65
GE
80
81 void set_logging(std::ostream *_logstream, log_level_values _log_level);
ffbbf9ab
GE
82};
83
9a5d7790
GE
84/** @brief a wrapper implementing reconnect-then-throw
85
86 This ConnectionWrapper tries to reconnect to the server if something with the connection
87 goes wrong. If even reconnecting max_retries times does not help, an exception is thrown.
88*/
ffbbf9ab
GE
89class ReconnectSocketWrapper : public BasicSocketWrapper
90{
91 public:
92 ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1",
93 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
94 int _max_retries=socket_client_connection::max_retries_default)
95 : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
96 { }
97
98 ReconnectSocketWrapper(const std::string& _path,
99 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
100 int _max_retries=socket_client_connection::max_retries_default)
101 : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
102 { }
103
fb3345ad 104 bool handle(command_client* stubBase, boost::function< void() > f);
ffbbf9ab
GE
105};
106
9a5d7790 107/// a placeholder-client_connection which is closed all the time
fb3345ad
GE
108class dummy_client_connection : public client_connection
109{
110 private:
111 void real_write(const std::string& data)
112 { }
113
114 public:
115 dummy_client_connection()
116 : client_connection()
117 { close(); }
118
119 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
120 { return false; }
121};
ffbbf9ab 122
9a5d7790
GE
123/** @brief a wrapper implementing reconnect-then-ignore
124
125 This ConnectionWrapper tries to reconnect to the server if something with the connection
126 goes wrong. If even reconnecting max_retries times does not help, the complete t2n-call is
127 ignored. The return value of the call will be created with the default constructor.
128*/
ffbbf9ab
GE
129class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
130{
fb3345ad
GE
131 private:
132 dummy_client_connection dc;
133
ffbbf9ab
GE
134 public:
135 ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1",
136 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
137 int _max_retries=socket_client_connection::max_retries_default)
138 : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
139 { }
140
141 ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
142 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
143 int _max_retries=socket_client_connection::max_retries_default)
144 : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
145 { }
146
147 client_connection* get_connection(void);
fb3345ad 148 bool handle(command_client* stubBase, boost::function< void() > f);
ffbbf9ab
GE
149};
150
ffbbf9ab
GE
151}
152
153#endif