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 class BasicSocketWrapper : public ConnectionWrapper
38 socket_type_value socket_type;
44 long long connect_timeout_usec;
47 std::auto_ptr<socket_client_connection> c;
50 BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1",
51 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
52 int _max_retries=socket_client_connection::max_retries_default)
53 : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
54 max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
57 BasicSocketWrapper(const std::string& _path,
58 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
59 int _max_retries=socket_client_connection::max_retries_default)
60 : path(_path), connect_timeout_usec(_connect_timeout_usec),
61 max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
64 client_connection* get_connection(void);
66 bool connection_established(void)
67 { return (c.get() != NULL); }
70 class ReconnectSocketWrapper : public BasicSocketWrapper
73 ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1",
74 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
75 int _max_retries=socket_client_connection::max_retries_default)
76 : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
79 ReconnectSocketWrapper(const std::string& _path,
80 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
81 int _max_retries=socket_client_connection::max_retries_default)
82 : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
85 bool handle(command_client* stubBase, boost::function< void() > f);
88 class dummy_client_connection : public client_connection
91 void real_write(const std::string& data)
95 dummy_client_connection()
99 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
103 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
106 dummy_client_connection dc;
109 ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1",
110 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
111 int _max_retries=socket_client_connection::max_retries_default)
112 : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
115 ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
116 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
117 int _max_retries=socket_client_connection::max_retries_default)
118 : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
121 client_connection* get_connection(void);
122 bool handle(command_client* stubBase, boost::function< void() > f);