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;
56 BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1",
57 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
58 int _max_retries=socket_client_connection::max_retries_default)
59 : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
60 max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
63 BasicSocketWrapper(const std::string& _path,
64 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
65 int _max_retries=socket_client_connection::max_retries_default)
66 : path(_path), connect_timeout_usec(_connect_timeout_usec),
67 max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
70 client_connection* get_connection(void);
72 bool connection_established(void)
73 { return (c.get() != NULL); }
75 void set_logging(std::ostream *_logstream, log_level_values _log_level);
78 /** @brief a wrapper implementing reconnect-then-throw
80 This ConnectionWrapper tries to reconnect to the server if something with the connection
81 goes wrong. If even reconnecting max_retries times does not help, an exception is thrown.
83 class ReconnectSocketWrapper : public BasicSocketWrapper
86 ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1",
87 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
88 int _max_retries=socket_client_connection::max_retries_default)
89 : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
92 ReconnectSocketWrapper(const std::string& _path,
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(_path,_connect_timeout_usec,_max_retries)
98 bool handle(command_client* stubBase, boost::function< void() > f);
101 /// a placeholder-client_connection which is closed all the time
102 class dummy_client_connection : public client_connection
105 void real_write(const std::string& data)
109 dummy_client_connection()
110 : client_connection()
113 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
117 /** @brief a wrapper implementing reconnect-then-ignore
119 This ConnectionWrapper tries to reconnect to the server if something with the connection
120 goes wrong. If even reconnecting max_retries times does not help, the complete t2n-call is
121 ignored. The return value of the call will be created with the default constructor.
123 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
126 dummy_client_connection dc;
129 ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1",
130 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
131 int _max_retries=socket_client_connection::max_retries_default)
132 : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
135 ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
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(_path,_connect_timeout_usec,_max_retries)
141 client_connection* get_connection(void);
142 bool handle(command_client* stubBase, boost::function< void() > f);