1 /***************************************************************************
2 * Copyright (C) 2008 by Gerd v. Egidy *
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. *
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. *
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 ***************************************************************************/
20 #ifndef __LIBT2N_SOCKET_WRAPPER
21 #define __LIBT2N_SOCKET_WRAPPER
27 #include <command_client.hxx>
29 #include <client_wrapper.hxx>
30 #include <socket_client.hxx>
35 /** @brief a basic implementation of ConnectionWrapper
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.
41 class BasicSocketWrapper : public ConnectionWrapper
44 socket_type_value socket_type;
50 long long connect_timeout_usec;
53 std::auto_ptr<socket_client_connection> c;
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
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),
63 max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
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),
70 max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
73 client_connection* get_connection(void);
75 bool connection_established(void)
76 { return (c.get() != NULL); }
78 void set_logging(std::ostream *_logstream, log_level_values _log_level);
81 /** @brief a wrapper implementing reconnect-then-throw
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.
86 class ReconnectSocketWrapper : public BasicSocketWrapper
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)
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)
101 bool handle(command_client* stubBase, boost::function< void() > f);
104 /// a placeholder-client_connection which is closed all the time
105 class dummy_client_connection : public client_connection
108 void real_write(const std::string& data)
112 dummy_client_connection()
113 : client_connection()
116 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
120 /** @brief a wrapper implementing reconnect-then-ignore
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.
126 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
129 dummy_client_connection dc;
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)
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)
144 client_connection* get_connection(void);
145 bool handle(command_client* stubBase, boost::function< void() > f);