libt2n: (tomj) documented code problems; have to find out if this is the source of...
[libt2n] / src / socket_wrapper.hxx
CommitLineData
ffbbf9ab
GE
1/***************************************************************************
2 * Copyright (C) 2008 by Gerd v. Egidy *
3 * gve@intra2net.com *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20#ifndef __LIBT2N_SOCKET_WRAPPER
21#define __LIBT2N_SOCKET_WRAPPER
22
23#include <functional>
24#include <string>
25
26#include <client.hxx>
27#include <command_client.hxx>
28#include <types.hxx>
29#include <client_wrapper.hxx>
30#include <socket_client.hxx>
31
32namespace libt2n
33{
34
9a5d7790
GE
35/** @brief a basic implementation of ConnectionWrapper
36
37 This is a basic version of a ConnectionWrapper which does not do any fancy
38 error handling or anything, it justs executes the regular calls. Use this
39 wrapper if you only want to use the singleton-feature of T2nSingletonWrapper.
40*/
ffbbf9ab
GE
41class BasicSocketWrapper : public ConnectionWrapper
42{
43 protected:
44 socket_type_value socket_type;
45
46 std::string path;
47 std::string server;
48 int port;
49
50 long long connect_timeout_usec;
51 int max_retries;
52
53 std::auto_ptr<socket_client_connection> c;
54
238ad35f
TJ
55 // TODO: Mark object as non-copyable as it contains an auto_ptr.
56 // This will make sure nobody every tries to put this in a STL container
57
ffbbf9ab
GE
58 public:
59 BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1",
60 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
61 int _max_retries=socket_client_connection::max_retries_default)
62 : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
e1614a6d 63 max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
ffbbf9ab
GE
64 { }
65
66 BasicSocketWrapper(const std::string& _path,
67 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
68 int _max_retries=socket_client_connection::max_retries_default)
69 : path(_path), connect_timeout_usec(_connect_timeout_usec),
e1614a6d 70 max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
ffbbf9ab
GE
71 { }
72
73 client_connection* get_connection(void);
fb3345ad
GE
74
75 bool connection_established(void)
76 { return (c.get() != NULL); }
2a956e65
GE
77
78 void set_logging(std::ostream *_logstream, log_level_values _log_level);
ffbbf9ab
GE
79};
80
9a5d7790
GE
81/** @brief a wrapper implementing reconnect-then-throw
82
83 This ConnectionWrapper tries to reconnect to the server if something with the connection
84 goes wrong. If even reconnecting max_retries times does not help, an exception is thrown.
85*/
ffbbf9ab
GE
86class ReconnectSocketWrapper : public BasicSocketWrapper
87{
88 public:
89 ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1",
90 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
91 int _max_retries=socket_client_connection::max_retries_default)
92 : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
93 { }
94
95 ReconnectSocketWrapper(const std::string& _path,
96 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
97 int _max_retries=socket_client_connection::max_retries_default)
98 : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
99 { }
100
fb3345ad 101 bool handle(command_client* stubBase, boost::function< void() > f);
ffbbf9ab
GE
102};
103
9a5d7790 104/// a placeholder-client_connection which is closed all the time
fb3345ad
GE
105class dummy_client_connection : public client_connection
106{
107 private:
108 void real_write(const std::string& data)
109 { }
110
111 public:
112 dummy_client_connection()
113 : client_connection()
114 { close(); }
115
116 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
117 { return false; }
118};
ffbbf9ab 119
9a5d7790
GE
120/** @brief a wrapper implementing reconnect-then-ignore
121
122 This ConnectionWrapper tries to reconnect to the server if something with the connection
123 goes wrong. If even reconnecting max_retries times does not help, the complete t2n-call is
124 ignored. The return value of the call will be created with the default constructor.
125*/
ffbbf9ab
GE
126class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
127{
fb3345ad
GE
128 private:
129 dummy_client_connection dc;
130
ffbbf9ab
GE
131 public:
132 ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1",
133 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
134 int _max_retries=socket_client_connection::max_retries_default)
135 : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
136 { }
137
138 ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
139 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
140 int _max_retries=socket_client_connection::max_retries_default)
141 : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
142 { }
143
144 client_connection* get_connection(void);
fb3345ad 145 bool handle(command_client* stubBase, boost::function< void() > f);
ffbbf9ab
GE
146};
147
ffbbf9ab
GE
148}
149
150#endif